Android Activity horizontal screen, vertical screen, full screen

Android the phone screen orientation change can make the application along with the appropriate changes, here is a brief introduction screen operation a little skill. Always include horizontal (vertical) screen, full screen, change the screen orientation on destruction Activity time.

 

1, the screen is always in landscape or portrait

For some games, we might expect the screen is always horizontal screen, then only need in your AndroidManifest.xml set the Activity Properties

Android : screenOrientation = "Landscape" always represent horizontal screen,

android: screenOrientation = "portrait" represents always portrait, the following examples are always horizontal screen

Java code  Collection Code
  1. <activity android:name="ViewStatusDetailActivity"  
  2.     android:screenOrientation="landscape"  
  3.         android:label="@string/app_name">  
  4.         </activity>  

android: Other values screenOrientation see screenOrientation

 

2, Activity Full Screen

Two configurations,

One is in AndroidManifest.xml set the Activity properties as follows:

Java code  Collection Code
  1. <activity android:name="ViewStatusDetailActivity"  
  2.     android:theme="@android:style/Theme.NoTitleBar.Fullscreen"  
  3.     android:label="@string/app_name">  
  4.     </activity>  

 android: theme = "@ android: style / Theme.NoTitleBar.Fullscreen" represents the full-screen Activity

 

The second code is provided

Java code  Collection Code
  1. public void onCreate(Bundle savedInstanceState) {   
  2.     super .onCreate (savedInstanceState);   
  3.     // No title     
  4.     requestWindowFeature(Window.FEATURE_NO_TITLE);     
  5.     //full screen     
  6.     getWindow().setFlags(WindowManager.LayoutParams. FLAG_FULLSCREEN ,       
  7.         WindowManager.LayoutParams. FLAG_FULLSCREEN);            
  8.     setContentView(R.layout.main);   
  9.     }   
  10. }   

 Wherein requestWindowFeature and getWindow (). SetFlags must be used together, and in front setContentView

 

 

3, change the screen orientation to solve the problem of reconstruction of destroyed Activity

当屏幕方向改变时,经常发现刚输入的文字被清空了、imageView图片不存在了,或是网络数据重新获取,其实是Activity会被销毁,重新调用OnCreate构建,如何防止这种情况呢,分为两步:

3.1 在AndroidManifest.xml中对Activity属性进行设置,如下:

Xml代码  Collection Code
  1. <activity android:name="ViewStatusActivity"  
  2.                   android:configChanges="orientation|keyboardHidden"  
  3.                   android:label="@string/app_name"  
  4.                   android:theme ="@style/update_status_style">  
  5.         </activity>  

android:configChanges="orientation|keyboardHidden"表示改变界面方向和隐藏键盘

具体android:configChanges见configChanges

 

 

3.2 重载onConfigurationChanged方法,此方法会在屏幕方向改变时被调用如下:

Java代码  Collection Code
  1. @Override  
  2. public void onConfigurationChanged(Configuration newConfig) {  
  3.   
  4.     super.onConfigurationChanged(newConfig);  
  5.   
  6.     if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {  
  7.         // Add horizontal screen code to be processed  
  8.     } else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {  
  9.         // adding the code to be processed portrait  
  10.     }  
Published 25 original articles · won praise 4 · views 20000 +

Guess you like

Origin blog.csdn.net/zhou8400/article/details/75386713