屏幕高度与实际屏幕高度不匹配

时间:2012-07-10 07:41:48

标签: java android

解决:

正如佐尔坦所写,我没有考虑状态栏和标题栏(他们都是25个身高)

我在我的Activity中的onClickListener方法中使用了以下代码,并将结果转发到我的Service类并将其打印出来:

Rect rectgle = new Rect();
Window window = getWindow();
window.getDecorView().getWindowVisibleDisplayFrame(rectgle);
int StatusBarHeight = rectgle.top;
int contentViewTop = window.findViewById(Window.ID_ANDROID_CONTENT).getTop();
int TitleBarHeight = contentViewTop - StatusBarHeight;

serviceIntent.putExtra("Titlebarheight", Integer.toString(TitleBarHeight));
serviceIntent.putExtra("Statusbarheight", Integer.toString(StatusBarHeight));

----------------------------------------------- ------------------------------------------

我正在尝试在我的 main.xml 中设置我的ImageView的布局高度,但是当我将layout_height设置为270dp(或px)时,它会从上到下填满整个屏幕,但我的屏幕尺寸实际上是320x240(HTC magic - Android 1.5) - 所以它不应该从上到下填满屏幕!

活动内的代码:

Display display = getWindowManager().getDefaultDisplay();
Log.i("Screen height", Integer.toString(display.getHeight())); // Returns 320
Log.i("Screen width", Integer.toString(display.getWidth())); // Returns 240

main.xml中的代码:

<ImageView
    android:id="@+id/bitmapImageView"
    android:layout_height="270dp" // Fills up the whole screen from top to bottom
    android:layout_width="50dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:background="#FFFFFF"
    android:src="@drawable/ic_launcher" />

为什么会这样?我该如何解决?

使用270作为布局高度时的示例:

使用240作为布局高度时的示例:

2 个答案:

答案 0 :(得分:2)

请参阅:Screen Resolution of androidscale-independent pixel

基本上,dp(与显示无关的像素)在所有显示器上的大小不同。根据你的决议1dp =〜.75px

要执行全屏活动,请参阅:Fullscreen Activity in Android?

具体来说,在该活动的Android清单中设置:android:theme="@android:style/Theme.NoTitleBar.Fullscreen"

答案 1 :(得分:1)

状态栏的高度和应用程序的标题栏似乎与ImageView的宽度大致相同,从您的XML判断为50px。

然后50px + 270px = 320px,这是屏幕的总高度(包括状态和标题栏)。