Android开发几种常用关于屏幕操作!
1.获取屏幕的width和height:
常用的获取屏幕长和宽的方法有如下3种:
<pre name="code" class="java">
private String getWindowDisplayMetrix() { DisplayMetrics wdm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(wdm); width=wdm.widthPixels; height=wdm.heightPixels; return "width" + "=" + width + "..." + "height" +"="+ height; }
private String getWindowDisplayMetrix2() { DisplayMetrics wdm = getResources().getDisplayMetrics(); width= wdm.widthPixels; height=wdm.heightPixels; return "width" + "=" + width + "..." + "height" + "=" + height; }
private String getWindowDisplayMetrix3() { WindowManager manager = (WindowManager) MainActivity.this.getSystemService(Context.WINDOW_SERVICE); Display display = manager.getDefaultDisplay(); width=display.getWidth(); height=display.getHeight(); return "width" + "=" + width + "..." + "height" +height; }
让他们分别显示在TextView中:
tv1.setText(getWindowDisplayMetrix()); tv2.setText(getWindowDisplayMetrix2()); tv3.setText(getWindowDisplayMetrix3());
结果如下,所以上面3中方法获取的结果是一样的。
2.设置手机全屏
2.1最常用的设置全屏方法如下:
@Override protected 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.activity_main); }使用上述方法注意如下点:
1.requestWindowFeature()方法一定要在setContentView()前面,否则会报错。
2.2 AndroidManifest中配置:
<activity android:name=".MainActivity" android:label="@string/app_name" android:theme="@android:style/Theme.NoTitleBar.Fullscreen" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
利用Android提供的内置的Theme就可以了。
2.3在styles中配置自己的Theme:
在valus文件夹下的styles.xml中配置如下:
<resources> <style name="MyTheme_NoTitle_FullScreen"> <item name="android:windowNoTitle">true</item> <item name="android:windowFullscreen">true</item> </style> </resources>
在AndroidManifest中引用自定义的Theme:
<activity android:name=".MainActivity" android:label="@string/app_name" android:theme="@style/MyTheme_NoTitle_FullScreen" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
3.设置屏幕显示方向:
3.1Java代码中配置:
setContentView(R.layout.activity_main); //设置竖屏 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); //设置横屏 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); }
3.2AndroidManifest中配置:
<activity android:name=".MainActivity" android:label="@string/app_name" android:screenOrientation="landscape" android:theme="@style/MyTheme_NoTitle_FullScreen" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
注:
当Java代码和AndroidManifest中同时配置了,显示只会按Java代码中配置的来。
题外话:
当不定义横竖屏情况下,这个app会随着手机的转向设置变化而变化,而我们对于横竖屏切换肯定要做相应的事情,可以向如下方法做:
if(getResources().getConfiguration().orientation==ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE){ //处理横屏下要做的事 }else{ //处理竖屏下要做的事情 }
Ps: 虽然接触Android有一段时间了,但是这是第一次写博客,如有不正确的地方,欢迎指出
!主要是为自己的学习做下记录,如果能对你有点帮助,我也很高兴。

希望能点个赞!这会给我莫大的鼓励哦
!
