Android Activity 全屏

本文介绍如何在Android应用中实现全屏布局,包括代码示例及常见问题解决方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

用了几天的SINA 微博后,感觉他的布局不错,首先是首页全屏图片突出产品预览,感觉不错自己也来试验一把,就一个简单全屏幕实现过程还真是有很多坑,特记录下来希望对大家有帮助!

废话少说,上代码!

  1. public class TestAgentextends Activity { 
  2.     @Override 
  3.     public void onCreate(Bundle savedInstanceState) { 
  4.         super.onCreate(savedInstanceState); 
  5.         setContentView(R.layout.main); 
  6.         this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
  7.        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 
  8.     } 

  1. <pre> 
实现全屏其实主要就最后两行代码,但是程序竟然错误,信息如下:

  1. Caused by: android.util.AndroidRuntimeException: requestFeature() must be called before adding content 
  2.      at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:172
  3.      at android.app.Activity.requestWindowFeature(Activity.java:2719
意思是 requestFeature 必须在初始化content内容前。我们来看下 API 的说明:

  1. public boolean requestFeature (int featureId) 
  2.  
  3. Since: API Level 1 
  4. Enable extended screen features. This must be called before setContentView(). May be called as many times as desired as long as it is before setContentView(). If not called, no extended features will be available. You can not turn off a feature once it is requested. You canot use other title features with FEATURE_CUSTOM_TITLE. 
好吧,调整代码调用顺序,把 setContentView() 放到最下面。

怎么样,运行成功了,激动,激动。 我使用的是一个ImageView 来作为首页的图片全屏展示,但是发现Activity全屏了,但是图片没有全部拉伸

  1. <?xmlversion="1.0"encoding="utf-8"?> 
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:orientation="vertical" 
  4.     android:layout_width="fill_parent" 
  5.     android:layout_height="fill_parent" 
  6.     > 
  7.     <ImageView 
  8.         android:id="@+id/imageView1" 
  9.         android:layout_width="fill_parent" 
  10.         android:layout_height="fill_parent" 
  11.         android:src="@drawable/welcome"/> 
  12. </LinearLayout> 
其实也和简单在ImageView加上:    android:scaleType="fitXY"    就可以了。 效果图如下:(其实已经看不出来了,看起来就是一张图片)



 

设置android全屏模式有两种方法,一种是在程序代码中设置,另一种是配置manifest.xml文件,推荐使用第二种方式。

    在manifest.xml文件中<application>和<activity>标签中都有android:theme属性

    只需要添加下面的xml代码就好了

www.2cto.com

1  android:theme="@android:style/Theme.NoTitleBar.Fullscreen"

  例如

     下面的代码使得ActivityDemoActivity显示为全屏模式

<activity android:name=".ActivityDemoActivity"            android:label="@string/app_name"            android:theme="@android:style/Theme.NoTitleBar.Fullscreen"                              >

而下面的写法则整个应用中所有都是全屏模式

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"        package="uni.activity"      

android:versionCode="1"      

android:versionName="1.0">    

<uses-sdk android:minSdkVersion="7" />      

<application android:icon="@drawable/icon" android:label="@string/app_name"                     android:theme="@android:style/Theme.NoTitleBar.Fullscreen">                               

<activity android:name=".ActivityDemoActivity"

android:label="@string/app_name" >            

<intent-filter>                

<action android:name="android.intent.action.MAIN" />                

<category android:name="android.intent.category.LAUNCHER" />              </intent-filter>       

</activity>           

<activity android:name=".Activity01"                    android:label="@string/app_name">        

</activity>    

</application>

</manifest>

 


Android中,想要实现Activity全屏显示,通常需要通过覆盖`onCreate()`、`onStart()`, `onResume()`等生命周期方法,并设置一些属性来达到目的。以下是大致步骤: 1. **修改主题**: 使用一个支持全屏模式的主题,例如`Theme.NoTitleBar.Fullscreen` 或者自定义一个主题,将`<item name="android:windowFullscreen">true</item>`添加到`styles.xml`文件中。 ```xml <style name="FullScreenTheme" parent="Theme.AppCompat"> <item name="android:windowNoTitle">true</item> <item name="android:windowActionBar">false</item> <item name="android:windowFullscreen">true</item> </style> ``` 2. **创建Activity并应用主题**: 在创建Activity时,将其主题设置为你刚才定义的全屏主题。例如: ```java public class FullScreenActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_full_screen); this.setTheme(R.style.FullScreenTheme); // 使用全屏主题 } } ``` 3. **去除状态栏** (仅限API 21及以上): 如果你还想移除设备上部的状态栏,可以使用沉浸式设计(Translucent Status Bar),添加以下代码: ```java getWindow().setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS, WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); ``` 4. **处理返回键**: 全屏模式下可能需要自定义处理返回键的行为,比如关闭Activity。可以在`onBackPressed()`方法里实现。 注意:以上操作可能会导致某些系统功能不可见,如导航栏、硬件菜单等,所以确保在实际项目中权衡是否真的需要全屏显示。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值