为什么我会得到这种高度值差异?
Why I get this difference of height values?
我的屏幕是 1080x1920。绿线高度为 1794,红线 - 63(状态栏)+ 84(标题栏)= 147,紫线 - 1584。为什么我在紫线加红线后得不到绿线高度?
紫线表示相对布局的高度,我在 onWindowFocusChanged()
:
中用这段代码找到了它
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.myRelativeLayout);
Log.i(TAG, "Relative layout height: "+String.valueOf(relativeLayout.getHeight()));
红线表示标题栏和状态栏的高度,我在 onWindowFocusChanged()
:
中用这段代码找到了它
Rect rectangle = new Rect();
Window window = getWindow();
window.getDecorView().getWindowVisibleDisplayFrame(rectangle);
int statusBarHeight = rectangle.top;
int contentViewTop = window.findViewById(Window.ID_ANDROID_CONTENT).getTop();
int titleBarHeight= contentViewTop - statusBarHeight;
Log.i(TAG, "StatusBar Height= " + statusBarHeight + " , TitleBar Height = " + titleBarHeight);
绿线应该代表整个程序的高度?我在 onWindowFocusChanged()
:
中用这段代码找到了它
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布局文件内容:
<?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.Whosebugquestion.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();
希望对您有所帮助。
我的屏幕是 1080x1920。绿线高度为 1794,红线 - 63(状态栏)+ 84(标题栏)= 147,紫线 - 1584。为什么我在紫线加红线后得不到绿线高度?
紫线表示相对布局的高度,我在 onWindowFocusChanged()
:
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.myRelativeLayout);
Log.i(TAG, "Relative layout height: "+String.valueOf(relativeLayout.getHeight()));
红线表示标题栏和状态栏的高度,我在 onWindowFocusChanged()
:
Rect rectangle = new Rect();
Window window = getWindow();
window.getDecorView().getWindowVisibleDisplayFrame(rectangle);
int statusBarHeight = rectangle.top;
int contentViewTop = window.findViewById(Window.ID_ANDROID_CONTENT).getTop();
int titleBarHeight= contentViewTop - statusBarHeight;
Log.i(TAG, "StatusBar Height= " + statusBarHeight + " , TitleBar Height = " + titleBarHeight);
绿线应该代表整个程序的高度?我在 onWindowFocusChanged()
:
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布局文件内容:
<?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.Whosebugquestion.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();
希望对您有所帮助。