开发者

Android title bar size

开发者 https://www.devze.com 2022-12-13 09:12 出处:网络
How can I g开发者_JS百科et the size of the title bar of an activity and the size of the system bar?I did that using my root layout, the problem is that you can\'t obtain this info (width and height) i

How can I g开发者_JS百科et the size of the title bar of an activity and the size of the system bar?


I did that using my root layout, the problem is that you can't obtain this info (width and height) in your onCreate/onStart/onResume methods when they are running by the first time.

Any moment after that, the size can be retrieved from your root layout (LinearLayout/FrameLayout/TableLayout/etc), in my case I was using FrameLayout:

FrameLayout f = (FrameLayout) findViewById(R.id.layout_root);
Log.d(TAG, "width " + f.getMeasuredWidth() );
Log.d(TAG, "height " + f.getMeasuredHeight() );

or:

FrameLayout f = (FrameLayout) findViewById(R.id.layout_root);
Log.d(TAG, "width " + f.getRight() );
Log.d(TAG, "height " + f.getBottom() );

Now that you already have the height of the activity, to know the height of the system bar is only subtract the full screen height from the height of the activity.

How to get height of the screen is explained here: Get screen dimensions in pixels

Edited from here --------------------------------------------------------

I found a way to get this info on the onCreted method, but only if your have more then one activity. On my case I have a main activity that calls second activity.

On main_activity before I call the second_activity, by a new Itent, I can add extra info for the second_activity. Here is the code you need on the first activity:

Intent i = new Intent(this, SecondActivity.class);
i.putExtra("width", f.getMeasuredWidth());
i.putExtra("height", f.getMeasuredHeight());
startActivity(i);

After that on your second activity you can retrieve this info on the onCreate method of your second activity doing that:

int width = getIntent().getExtras().getInt("width");
int height = getIntent().getExtras().getInt("height");
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号