1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > android 状态栏 标题栏 屏幕高度

android 状态栏 标题栏 屏幕高度

时间:2024-01-02 09:45:29

相关推荐

android 状态栏 标题栏 屏幕高度

1.获取状态栏高度:

decorView是window中的最顶层view,可以从window中获取到decorView,然后decorView有个getWindowVisibleDisplayFrame方法可以获取到程序显示的区域,包括标题栏,但不包括状态栏。

于是,我们就可以算出状态栏的高度了。

Rect frame = new Rect(); getWindow().getDecorView().getWindowVisibleDisplayFrame(frame); int statusBarHeight = frame.top;

2.获取标题栏高度:

getWindow().findViewById(Window.ID_ANDROID_CONTENT)这个方法获取到的view就是程序不包括标题栏的部分,

然后就可以知道标题栏的高度了。

1 int contentTop = getWindow().findViewById(Window.ID_ANDROID_CONTENT).getTop(); 2 //statusBarHeight是上面所求的状态栏的高度 3 int titleBarHeight = contentTop - statusBarHeight

3.获取屏幕高度

1 WindowManager windowManager = getWindowManager(); 2 Display display = windowManager.getDefaultDisplay(); 3 screenWidth = display.getWidth(); 4 screenHeight = display.getHeight();

1 DisplayMetrics dm = new DisplayMetrics(); 2 this.getWindowManager().getDefaultDisplay().getMetrics(dm);//this指当前activity 3 screenWidth =dm.widthPixels; 4 screenHeight =dm.heightPixels;

以上两种方法在屏幕未显示的时候,还是处于0的状态,即要在setContentView调用之后才有效。

设置为无标题 requestWindowFeature(Window.FEATURE_NO_TITLE);

设置为全屏模式

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

设置为横屏setRequesteOrientation(ActivityInfo.SCREEN_ORIENTATION_LADSCAPE);

在开发中我们经常需要把我们的应用设置为全屏,这里我所知道的有俩中方法,一中是在代码中设置,另一种方法是在配置文件里改!

1 public void onCreate(Bundle savedInstanceState) {2super.onCreate(savedInstanceState);3//去除title 4 requestWindowFeature(Window.FEATURE_NO_TITLE); 5 //去掉Activity上面的状态栏 6 getWindow().setFlags(WindowManager.LayoutParams. FLAG_FULLSCREEN , 7WindowManager.LayoutParams. FLAG_FULLSCREEN);89setContentView(R.layout.main); 10 }

1<application android:icon="@drawable/icon" android:label="@string/app_name">2<activity android:name=".OpenGl_Lesson1"3android:theme="@android:style/Theme.NoTitleBar.Fullscreen"4android:label="@string/app_name">5 <intent-filter>6 <action android:name="android.intent.action.MAIN" />7 <category android:name="android.intent.category.LAUNCHER" />8 </intent-filter>9</activity> 10</application>

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。