我一直在 Galaxy 5 Media Player
上测试我的大部分布局,并且所有布局都在查看我如何在常规 layout
文件夹中设计它们。我使用的是 SDK 10
,因此我不使用 layout-sw600dp
类型文件夹,而是使用 /layout
、/layout -大
等。
虽然我的布局在 Galaxy 5 播放器上看起来不错,但当我在将要使用的其他设备 Lenovo Ideapad A1
上尝试它们时, View 不正确。我在 /layout-large
中为 7 英寸平板电脑创建了一个新的 xml 文件,但现在预览中的内容与我的设备上的内容不匹配。
我使用的是 RelativeLayout
,其中除了 8 个按钮之外什么都没有。声明的第一个按钮与底部对齐,然后每个后续按钮都放置在其下方按钮的上方,并设置了 marginBottom
参数。
这是原始 View 的 xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/v2"
android:gravity="center_horizontal"
android:orientation="vertical" >
<Button
android:id="@+id/exitButton"
android:layout_width="350dp"
android:layout_height="60dp"
android:layout_marginBottom="36dp"
android:layout_alignParentBottom="true"
android:textColor="@android:color/white"
android:background="@drawable/sel_button_round"
android:text="@string/main_exit" />
<Button
android:id="@+id/findDeviceButton"
android:layout_width="350dp"
android:layout_height="60dp"
android:layout_marginBottom="18dp"
android:layout_above="@id/exitButton"
android:textColor="@android:color/white"
android:background="@drawable/sel_button_round"
android:text="@string/main_find_device" />
<Button
android:id="@+id/cebusButton"
android:layout_width="350dp"
android:layout_height="60dp"
android:layout_marginBottom="18dp"
android:layout_above="@id/findDeviceButton"
android:textColor="@android:color/white"
android:background="@drawable/sel_button_round"
android:text="@string/main_cebus" />
<Button
android:id="@+id/operationsButton"
android:layout_width="350dp"
android:layout_height="60dp"
android:layout_marginBottom="18dp"
android:layout_above="@id/cebusButton"
android:textColor="@android:color/white"
android:background="@drawable/sel_button_round"
android:text="@string/main_operations" />
<Button
android:id="@+id/monitorButton"
android:layout_width="350dp"
android:layout_height="60dp"
android:layout_marginBottom="18dp"
android:layout_above="@id/operationsButton"
android:textColor="@android:color/white"
android:background="@drawable/sel_button_round"
android:text="@string/main_monitor" />
<Button
android:id="@+id/wavesButton"
android:layout_width="350dp"
android:layout_height="60dp"
android:layout_marginBottom="18dp"
android:layout_above="@id/monitorButton"
android:textColor="@android:color/white"
android:background="@drawable/sel_button_round"
android:text="@string/main_waves" />
<Button
android:id="@+id/statusButton"
android:layout_width="350dp"
android:layout_height="60dp"
android:layout_marginBottom="18dp"
android:layout_above="@id/wavesButton"
android:textColor="@android:color/white"
android:background="@drawable/sel_button_round"
android:text="@string/main_status" />
<Button
android:id="@+id/more_parametersButton"
android:layout_width="350dp"
android:layout_height="60dp"
android:layout_marginBottom="18dp"
android:layout_above="@id/statusButton"
android:textColor="@android:color/white"
android:background="@drawable/sel_button_round"
android:text="@string/main_parameters" />
</RelativeLayout>
当我在 7 英寸设备上运行此程序时,按钮非常大,超出了屏幕顶部。但是,在 Eclipse
中预览时,按钮都很好地贴合在屏幕上。为什么预览显示的 View 与我预期的不同?
编辑:
我已经调整了 XML 文件,使其可以在我的平板电脑上运行。尽管现在预览看起来更糟,但以下 XML 文件会在我的设备上生成我想要的结果。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/v2"
android:gravity="center_horizontal"
android:orientation="vertical" >
<Button
android:id="@+id/exitButton"
android:layout_width="300dp"
android:layout_height="50dp"
android:layout_marginBottom="30dp"
android:layout_alignParentBottom="true"
android:textColor="@android:color/white"
android:background="@drawable/sel_button_round"
android:text="@string/main_exit" />
<Button
android:id="@+id/findDeviceButton"
android:layout_width="300dp"
android:layout_height="50dp"
android:layout_marginBottom="15dp"
android:layout_above="@id/exitButton"
android:textColor="@android:color/white"
android:background="@drawable/sel_button_round"
android:text="@string/main_find_device" />
<Button
android:id="@+id/cebusButton"
android:layout_width="300dp"
android:layout_height="50dp"
android:layout_marginBottom="15dp"
android:layout_above="@id/findDeviceButton"
android:textColor="@android:color/white"
android:background="@drawable/sel_button_round"
android:text="@string/main_cebus" />
<Button
android:id="@+id/operationsButton"
android:layout_width="300dp"
android:layout_height="50dp"
android:layout_marginBottom="15dp"
android:layout_above="@id/cebusButton"
android:textColor="@android:color/white"
android:background="@drawable/sel_button_round"
android:text="@string/main_operations" />
<Button
android:id="@+id/monitorButton"
android:layout_width="300dp"
android:layout_height="50dp"
android:layout_marginBottom="15dp"
android:layout_above="@id/operationsButton"
android:textColor="@android:color/white"
android:background="@drawable/sel_button_round"
android:text="@string/main_monitor" />
<Button
android:id="@+id/wavesButton"
android:layout_width="300dp"
android:layout_height="50dp"
android:layout_marginBottom="15dp"
android:layout_above="@id/monitorButton"
android:textColor="@android:color/white"
android:background="@drawable/sel_button_round"
android:text="@string/main_waves" />
<Button
android:id="@+id/statusButton"
android:layout_width="300dp"
android:layout_height="50dp"
android:layout_marginBottom="15dp"
android:layout_above="@id/wavesButton"
android:textColor="@android:color/white"
android:background="@drawable/sel_button_round"
android:text="@string/main_status" />
<Button
android:id="@+id/more_parametersButton"
android:layout_width="300dp"
android:layout_height="50dp"
android:layout_marginBottom="15dp"
android:layout_above="@id/statusButton"
android:textColor="@android:color/white"
android:background="@drawable/sel_button_round"
android:text="@string/main_parameters" />
</RelativeLayout>
最佳答案
我认为这不是您的布局问题,请检查屏幕兼容性
如果您的应用程序仅与小屏幕和普通屏幕兼容,无论屏幕密度如何,那么您必须指定八个不同的元素,因为每种屏幕尺寸都有四种不同的密度配置
<compatible-screens>
<screen android:screenSize=["small" | "normal" | "large" | "xlarge"]
android:screenDensity=["ldpi" | "mdpi" | "hdpi" | "xhdpi"] />
...
有关更多信息,请查看此 LINK
以下是有关如何确保应用程序在不同屏幕上正确显示的快速 list :
在 XML 布局文件中指定尺寸时使用 wrap_content、fill_parent 或 dp 单位
请勿在应用程序代码中使用硬编码像素值
不要使用 AbsoluteLayout(已弃用)
为不同的屏幕密度提供替代位图可绘制元素
Android 系统通过两种方式帮助您的应用程序实现密度无关性:
系统根据当前屏幕缩放 dp 单位 密度
系统将可绘制资源缩放到适当的大小,基于 根据当前屏幕密度(如有必要)
有关更多信息,请查看此 LINK
关于android - 我在大屏幕设备上的布局与大屏幕预览不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14442250/