Android Activity 全屏

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

废话少说,上代码!

[java] view plain copy print ?
  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.     } 
public class TestAgent extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); this.requestWindowFeature(Window.FEATURE_NO_TITLE); this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); } }

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

[java] view plain copy print ?
  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
Caused by: android.util.AndroidRuntimeException: requestFeature() must be called before adding content at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:172) at android.app.Activity.requestWindowFeature(Activity.java:2719) 意思是 requestFeature 必须在初始化content内容前。我们来看下 API 的说明:

[html] view plain copy print ?
  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. 
public boolean requestFeature (int featureId) Since: API Level 1 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全屏了,但是图片没有全部拉伸

[html] view plain copy print ?
  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> 
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ImageView android:id="@+id/imageView1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:src="@drawable/welcome"/> </LinearLayout> 其实也和简单在ImageView加上:    android:scaleType="fitXY"    就可以了。 效果图如下:(其实已经看不出来了,看起来就是一张图片)

Android Activity 全屏_第1张图片

 

设置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 全屏)