`
apm7s
  • 浏览: 6368 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Android中 屏幕设置相关:全屏、居中、横竖、自适应

阅读更多
  在实际的应用程序开发中,我们有时需要把 Activity 设置成全屏显示,一般情况下,可以通过两种方式来设置全屏显示效果。其一,通过在代码中可以设置,其二,通过manifest配置文件来设置全屏。

       其一:在代码中设置(如下)
 public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
          
        //设置无标题  
        requestWindowFeature(Window.FEATURE_NO_TITLE);  
        //设置全屏  
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,   
                WindowManager.LayoutParams.FLAG_FULLSCREEN);  
          
        setContentView(R.layout.main);  
}  

   但要注意的是:在代码中设置的话,设置无标题和设置全屏的两段代码要放置在 setContentView(R.layout.main); 这段代码的前面。要不然会报错。

       其二:在manifest配置文件中设置

<?xml version="1.0" encoding="utf-8"?>  
<manifest xmlns:android="http://schemas.android.com/apk/res/android"  
      package="com.andyidea"  
      android:versionCode="1"  
      android:versionName="1.0">  
    <uses-sdk android:minSdkVersion="8" />  
    <application android:icon="@drawable/icon" android:label="@string/app_name">  
        <activity android:name=".login.LoginActivity"   
                  android:theme="@android:style/android.NoTitleBar.Fullscreen"  
                  android:label="@string/app_name">  
            <intent-filter>  
                <action android:name="android.intent.action.MAIN" />  
                <category android:name="android.intent.category.LAUNCHER" />  
            </intent-filter>  
        </activity>  
    </application>  
</manifest>  

在相应的Activity中节点中添加属性:       android:theme="@android:style/Theme.NoTitleBar.Fullscreen" 即可以设置某个Activity全屏显示。若设置成 android:theme="@android:style/Theme.NoTitleBar" 即是只是设置成无标题状态。


补充:
1.
垂直居中:
   android:layout_centerVertical="true"
水平居中:
   android:layout_centerHorizontal="true"
2.
hideStatusbarAndTitlebar()隐藏statusbar和titlebar.
private void hideStatusbarAndTitlebar() {
    final Window win = getWindow();
    // No Statusbar
    win.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    // No Titlebar
    requestWindowFeature(Window.FEATURE_NO_TITLE);
}

3.设置屏幕显示模式ScreenOrientation.
   在activity里设置android:screenOrientation的值。
    android:screenOrientation的属性有以下值:
unspecified(默 认值,由系统判断状态自动切换),The default value. The system chooses the orientation. The policy it uses, and therefore the choices made in specific contexts, may differ from device to device.
landscape,横屏
portrait,竖屏
user(用户当前设置的orientation值),The user's current preferred orientation.
behind(下一个要显示的Activity的orientation值),The same orientation as the activity that's immediately beneath it in the activity stack.
sensor(传 感器的方向),The orientation determined by a physical orientation sensor. The orientation of the display depends on how the user is holding the device; it changes when the user rotates the device.
nosensor(不 使用传感器,这个效果差不多等于unspecified).An orientation determined without reference to a physical orientation sensor. The sensor is ignored, so the display will not rotate based on how the user moves the device. Except for this distinction, the system chooses the orientation using the same policy as for the "unspecified" setting.
4.水平/垂直居中的方法.
  设置parent的android:gravity为"center"。
5.获得当前屏幕宽高的方法.
Display display = getWindowManager().getDefaultDisplay();
Config.screenWidth = display.getWidth();
Config.screenHeight = display.getHeight();
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics