免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2496 | 回复: 1
打印 上一主题 下一主题

Android中 屏幕设置相关:全屏、居中、横竖、自适应 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-12-11 16:47 |只看该作者 |倒序浏览
Android中 屏幕设置相关:全屏、居中、横竖、自适应














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

       其一:在代码中设置(如下)
Java代码
  1. public void onCreate(Bundle savedInstanceState) {     
  2.         super.onCreate(savedInstanceState);     
  3.             
  4.         //设置无标题     
  5.         requestWindowFeature(Window.FEATURE_NO_TITLE);     
  6.         //设置全屏     
  7.         getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,      
  8.                 WindowManager.LayoutParams.FLAG_FULLSCREEN);     
  9.             
  10.         setContentView(R.layout.main);     
  11. }   

  12. public void onCreate(Bundle savedInstanceState) {  
  13.         super.onCreate(savedInstanceState);  
  14.          
  15.         //设置无标题  
  16.         requestWindowFeature(Window.FEATURE_NO_TITLE);  
  17.         //设置全屏  
  18.         getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,   
  19.                 WindowManager.LayoutParams.FLAG_FULLSCREEN);  
  20.          
  21.         setContentView(R.layout.main);  
  22. }  
复制代码
但要注意的是:在代码中设置的话,设置无标题和设置全屏的两段代码要放置在 setContentView(R.layout.main); 这段代码的前面。要不然会报错。

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

Java代码
  1. <?xml version="1.0" encoding="utf-8"?>     
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"     
  3.       package="com.andyidea"     
  4.       android:versionCode="1"     
  5.       android:versionName="1.0">     
  6.     <uses-sdk android:minSdkVersion="8" />     
  7.     <application android:icon="@drawable/icon" android:label="@string/app_name">     
  8.         <activity android:name=".login.LoginActivity"      
  9.                   android:theme="@android:style/android.NoTitleBar.Fullscreen"     
  10.                   android:label="@string/app_name">     
  11.             <intent-filter>     
  12.                 <action android:name="android.intent.action.MAIN" />     
  13.                 <category android:name="android.intent.category.LAUNCHER" />     
  14.             </intent-filter>     
  15.         </activity>     
  16.     </application>     
  17. </manifest>   

  18. <?xml version="1.0" encoding="utf-8"?>  
  19. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  20.       package="com.andyidea"  
  21.       android:versionCode="1"  
  22.       android:versionName="1.0">  
  23.     <uses-sdk android:minSdkVersion="8" />  
  24.     <application android:icon="@drawable/icon" android:label="@string/app_name">  
  25.         <activity android:name=".login.LoginActivity"   
  26.                   android:theme="@android:style/android.NoTitleBar.Fullscreen"  
  27.                   android:label="@string/app_name">  
  28.             <intent-filter>  
  29.                 <action android:name="android.intent.action.MAIN" />  
  30.                 <category android:name="android.intent.category.LAUNCHER" />  
  31.             </intent-filter>  
  32.         </activity>  
  33.     </application>  
  34. </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.
Java代码
  1. private void hideStatusbarAndTitlebar() {   
  2.     final Window win = getWindow();   
  3.     // No Statusbar   
  4.     win.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,   
  5.             WindowManager.LayoutParams.FLAG_FULLSCREEN);   
  6.     // No Titlebar   
  7.     requestWindowFeature(Window.FEATURE_NO_TITLE);   
  8. }  

  9. private void hideStatusbarAndTitlebar() {
  10.     final Window win = getWindow();
  11.     // No Statusbar
  12.     win.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
  13.             WindowManager.LayoutParams.FLAG_FULLSCREEN);
  14.     // No Titlebar
  15.     requestWindowFeature(Window.FEATURE_NO_TITLE);
  16. }
复制代码
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.获得当前屏幕宽高的方法.
Java代码
  1. Display display = getWindowManager().getDefaultDisplay();   
  2. Config.screenWidth = display.getWidth();   
  3. Config.screenHeight = display.getHeight();
复制代码

论坛徽章:
0
2 [报告]
发表于 2011-12-23 22:05 |只看该作者
谢谢分享  希望于楼主多多交流
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP