Java 应用程序不适合所有显示大小

Java 应用程序不适合所有显示大小,java,android,tablet,Java,Android,Tablet,我正在构建一个Android应用程序,它是为Galaxy Tab设计的,按照规格,屏幕大小是1024x600,但是如果我尝试添加这个大小的组件,左侧和底部会被切断 我添加了以下代码以查看差异 WindowManager mWMgr = (WindowManager) getApplicationContext() .getSystemService(Context.WINDOW_SERVICE); int width = mWMgr.getDefaultDisplay().getWidt

我正在构建一个Android应用程序,它是为Galaxy Tab设计的,按照规格,屏幕大小是1024x600,但是如果我尝试添加这个大小的组件,左侧和底部会被切断

我添加了以下代码以查看差异

  WindowManager mWMgr = (WindowManager) getApplicationContext()
.getSystemService(Context.WINDOW_SERVICE);
  int width = mWMgr.getDefaultDisplay().getWidth();
  int height = mWMgr.getDefaultDisplay().getHeight();
  Toast.makeText(getApplicationContext(),
    "[system][w=" + width + ":h=" + height + "]", Toast.LENGTH_LONG).show();

  width = getWindowManager().getDefaultDisplay().getWidth();
  height = getWindowManager().getDefaultDisplay().getHeight();
  Toast.makeText(getApplicationContext(),
    "[this][w=" + width + ":h=" + height + "]", Toast.LENGTH_LONG).show();
运行时,显示: [系统][w=600:h=1024] [这][w=400:h=683]

为什么会这样

如果需要,我添加代码,将组件放到屏幕上

这是我的layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView android:id="@+id/image01" android:layout_width="match_parent"
        android:layout_height="match_parent" android:scaleType="fitXY" />
</LinearLayout>
在这种情况下,我在ImageView上绘制的图像将缩放到屏幕大小400x683

我测试了:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="600px"
    android:layout_height="1024px">
    <ImageView android:id="@+id/image01" android:layout_width="match_parent"
        android:layout_height="match_parent" android:scaleType="fitXY" />
</LinearLayout>

通过这种方式,图像的左侧和底部将从屏幕上剪掉。

在清单中指定相应的值是否有帮助:

<supports-screens android:smallScreens=["true" | "false"] 
                  android:normalScreens=["true" | "false"] 
                  android:largeScreens=["true" | "false"] 
                  android:anyDensity=["true" | "false"] />

请参阅。

在清单中指定相应的值是否有帮助:

<supports-screens android:smallScreens=["true" | "false"] 
                  android:normalScreens=["true" | "false"] 
                  android:largeScreens=["true" | "false"] 
                  android:anyDensity=["true" | "false"] />

请参阅。

您的XML中有什么? 为视图填充父内容还是换行内容?你需要第一个取决于你在做什么


编辑:在顶部,Thorsten Dittmars回答…

您的XML中有什么? 为视图填充父内容还是换行内容?你需要第一个取决于你在做什么


编辑:在顶部Thorsten Dittmars回答…

不要使用Display.getWidth/Height。这些返回显示的原始大小。这不是你实际拥有的空间。例如,状态栏将沿某个轴将实际空间减少一些未定义的量。不要认为使用FLAG_全屏会让你免于这种情况,因为一台设备有一个包含后退和主页按钮的状态栏是完全合理的,因此你无法摆脱它


调整屏幕大小的正确方法是通过查看系统。这里最简单的事情就是创建一个实现onSizeChanged的View子类,并将其与setContentView结合起来。布局过程完成后,将使用视图的可用空间调用onSizeChanged。每当空间发生变化时(由于方向变化、显示变化、显示IME等原因),都会调用此函数。

不要使用display.getWidth/Height。这些返回显示的原始大小。这不是你实际拥有的空间。例如,状态栏将沿某个轴将实际空间减少一些未定义的量。不要认为使用FLAG_全屏会让你免于这种情况,因为一台设备有一个包含后退和主页按钮的状态栏是完全合理的,因此你无法摆脱它


调整屏幕大小的正确方法是通过查看系统。这里最简单的事情就是创建一个实现onSizeChanged的View子类,并将其与setContentView结合起来。布局过程完成后,将使用视图的可用空间调用onSizeChanged。每当空间发生变化时(由于方向变化、显示变化、显示IME等原因),都会调用此函数。

在没有全屏的情况下,我看到标题栏在垂直轴上假设为20px,但我看不出这些组件获得200x600像素屏幕的理由,即使在全屏模式下也是如此。我会尝试你的方法,看看会发生什么。没有全屏,我看到标题栏在垂直轴上假设为20px,但我看不出有理由让这些组件在我的屏幕上显示200x600像素,即使在全屏模式下也是如此。我会试着让你接近,看看会发生什么。