Android 为什么我会得到这个高度值的差异?
我的屏幕是1080x1920。绿线的高度是1794,红线-63(状态栏)+84(标题栏)=147,紫线-1584。为什么我不通过在紫色线的基础上加上红色线来得到绿线的高度 紫色线表示相对布局的高度,我在Android 为什么我会得到这个高度值的差异?,android,android-layout,android-actionbar,android-titlebar,android-statusbar,Android,Android Layout,Android Actionbar,Android Titlebar,Android Statusbar,我的屏幕是1080x1920。绿线的高度是1794,红线-63(状态栏)+84(标题栏)=147,紫线-1584。为什么我不通过在紫色线的基础上加上红色线来得到绿线的高度 紫色线表示相对布局的高度,我在onWindowFocusChanged()中找到它: 红线表示标题栏和状态栏的高度,我在onWindowFocusChanged()中找到它: 绿线代表整个程序的高度?我在onWindowFocusChanged()的中找到了这段代码: XML布局文件内容: <?xml version=
onWindowFocusChanged()
中找到它:
红线表示标题栏和状态栏的高度,我在onWindowFocusChanged()
中找到它:
绿线代表整个程序的高度?我在onWindowFocusChanged()的中找到了这段代码:
XML布局文件内容:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="lt.wilkas.stackoverflowquestion.MainActivity"
android:id="@+id/myRelativeLayout">
</RelativeLayout>
您的标题栏高度不正确。如您所见,标题栏的高度在视觉上显示为状态栏高度的两倍以上。因此,请使用不同的方法来获得标题栏的高度
你可以这样得到:
int whiteParentHeight = 0;
int titlebarHeight = 0;
public int getTitleBarHeight() {
whiteParent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
//noinspection deprecation
whiteParent.getViewTreeObserver().removeGlobalOnLayoutListener(this);
} else {
whiteParent.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
whiteParentHeight = whiteParent.getHeight();
titlebarHeight = whiteParentHeight - getStatusBarHeight();
}
});
}
public int getStatusBarHeight() {
int result = 0;
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = getResources().getDimensionPixelSize(resourceId);
}
return result;
}
希望这能对你有所帮助。也许要一些填充物?阴影填充?也许吧,但它们适用于哪里?我试图解决这个问题,但坐标中的这些不一致让我感到困惑:(此处的白色父对象是什么?它是主要的相对对象吗?我得到的状态栏为63,但状态栏为0。状态栏高度将相同,我们在此处计算标题栏高度标题栏高度值保持为0 OK。这是您根据所选主题的默认标题栏。您可以通过更改t来删除此标题heme(例如NoActionBar或No TitleBar)并在布局xml文件中创建自己的标题栏。这将帮助您获得应用程序的写入维度。
Display mdisp = getWindowManager().getDefaultDisplay();
Log.i(TAG, "mdisp: "+String.valueOf(mdisp));
Point mdispSize = new Point();
mdisp.getSize(mdispSize);
int maxX = mdispSize.x;
int maxY = mdispSize.y;
Log.i(TAG, "Max X, Y: "+String.valueOf(maxX)+", "+String.valueOf(maxY));
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="lt.wilkas.stackoverflowquestion.MainActivity"
android:id="@+id/myRelativeLayout">
</RelativeLayout>
int whiteParentHeight = 0;
int titlebarHeight = 0;
public int getTitleBarHeight() {
whiteParent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
//noinspection deprecation
whiteParent.getViewTreeObserver().removeGlobalOnLayoutListener(this);
} else {
whiteParent.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
whiteParentHeight = whiteParent.getHeight();
titlebarHeight = whiteParentHeight - getStatusBarHeight();
}
});
}
public int getStatusBarHeight() {
int result = 0;
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
result = getResources().getDimensionPixelSize(resourceId);
}
return result;
}
final TypedArray styledAttributes = getContext().getTheme().obtainStyledAttributes(
new int[] { android.R.attr.actionBarSize });
mActionBarSize = (int) styledAttributes.getDimension(0, 0);
styledAttributes.recycle();