xml文件中设置:
要实现这个目的,只需要在AndroidManifest.xml里声明Activity的时候加上一个属性:Android:screenOrientation,属性取值landscape为固定横屏、portrait为固定纵屏幕。
[html] view plain copy
- <!-- android:screenOrientation="portrait" 竖屏
- android:screenOrientation="landscape " 横屏
- android:screenOrientation="unspecified" 未指明方向
- -->
- <activity
- android:label="@string/app_name"
- android:name=".LockTheScreenActivity"
- android:screenOrientation="portrait"
- ></activity>
b)代码实现如下:
[java] view plain copy
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);// 横屏
- //setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) 竖屏
- setContentView(R.layout.main);
- }
屏幕会自动切换时,默认状态的应用程序,会重新调用onCreate,相当于重新启动了一次应用程序。同时,layout可能因为横屏带来不能合理适配的问题。为了解决旋屏和键盘切换引起的程序重启问题,还需要增加一个属性:android:configChanges。这个属性可以理解为一个监听器,它将拦截旋屏和键盘切换事件,阻止程序重启而变为回调onConfigurationChanged方法。这里常用的属性取值为:keyboardHidden|orientation。
去标题
[java] view plain copy
- <span style="font-size:14px;">public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- this.requestWindowFeature(Window.FEATURE_NO_TITLE);
- }</span>
全屏
[java] view plain copy
- <span style="font-size:14px;">public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
- }</span>
转载:http://blog.csdn.net/chaoyu168/article/details/51005765
时间: 2024-10-07 20:51:06