我使用操作栏(起源于 android,而非 ABS)并将我的 View 相对放置在根布局 (RelativeLayout
) 上。为了计算屏幕上的相对位置,我使用了 View 的 getParent().getHeight()
/getParent().getWidth()
方法。使用普通的 ActionBar 它工作正常。我得到父级的真实高度( Activity 高度 - 操作栏高度,例如 800 像素显示屏上的 690 像素)。但是,如果我使用 splitActionBarWhenNarrow
选项并显示底部栏,我将获得与父级相同的高度(例如,在 800 像素的屏幕上再次显示 690 像素)。因此,我的一些以编程方式定位的 vie 位于底部操作栏下方。
如果没有操作栏,如何获得父项的真实高度?
编辑:
我在 Activity 的 onWindowFocusChanged
方法中读取父宽度和高度。
最佳答案
顶部和底部操作栏的高度相同。据我所知,您不能以编程方式请求操作栏的大小。 This帖子给出了一个示例,说明如何使用样式设置操作栏高度以使其保持一致。
判断action bar是否被拆分,需要手动计算。根据 the design guidelines (靠近底部)
How many actions will fit in the main action bar? Action bar capacity is controlled by the following rules:
Action buttons in the main action bar may not occupy more than 50% of the bar's width. Action buttons on bottom action bars can use the entire width.
The screen width in density-independent pixels (dp) determine the number of items that will fit in the main action bar:
smaller than 360 dp = 2 icons
360-499 dp = 3 icons
500-599 dp = 4 icons
600 dp and larger = 5 icons
例子:
所以你想做这样的事情。
DisplayMetrics displayMetrics = new DisplayMetrics();
Display display = getWindowManager().getDefaultDisplay();
display.getMetrics(displayMetrics);
float density = displayMetrics.density;
// width in dp
float width = (displayMetrics.widthPixels / density);
// I will assume you have 3 action bar icons
if (width < 360) {
// 2 menu items fit.
// Calculate action bar height with 2*(actionbar height)
// to accommodate the bottom action bar.
} else {
// 3+ menu items fit
// Calculate action bar height with 1*(actionbar height).
}
关于Android:拆分ActionBar真实父级高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15706616/