我很失望。我刚刚完成了基于 360dp 的“普通屏幕”项目,但是当我尝试在 Motorola Atrix 中运行时,我大吃一惊。 Motorola Atrix是360dp而不是320dp,因为他的宽度是540px。现在我正在努力寻找要解决的问题。如何创建 360dp 布局?
我尝试了所有这些:
- res/values-sw360dp
- res/layout-sw360dp
主.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:orientation="vertical"
android:background="#DFEFF1">
<Button
android:background="#AAAA11"
android:id="@+id/button1"
android:layout_width="@dimen/default_width"
android:layout_height="@dimen/default_height"
android:text="SOME TEXT 1"
/>
<Button
android:background="#FFAA11"
android:id="@+id/button2"
android:layout_width="@dimen/default_width"
android:layout_height="@dimen/default_height"
android:text="SOME TEXT 2"
android:layout_alignParentRight="true" />
</RelativeLayout>
res/values/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="default_width">160dp</dimen>
<dimen name="default_height">160dp</dimen>
</resources>
摩托罗拉 Atrix
三星 Galaxy SII
最佳答案
通常在设计布局时,您希望避免规划特定的像素大小。如果您想要根据像素分离所有布局,那么您几乎必须为每个设备提供一个布局(世界上有这么多设备具有不同尺寸的屏幕)。通常您需要为几个不同的尺寸类别提供布局资源。 layout-small
、layout-normal
、layout-large
等。如果您提供了这些并且您的布局构建良好,那么它应该可以很好地扩展到不同尺寸的设备。
当您在较大尺寸的设备上运行时,您的布局是否有特定的错误?也许如果您发布,我可以帮助您尝试解决它,而无需按像素大小分隔布局。
Supporting Multiple Screens在开发人员文档中有很多关于如何构建您的应用程序以便它们能够很好地扩展的重要信息。
编辑:
解决您的问题的一种潜在方法是不使用静态 dp 值作为宽度,而是允许按钮增长以占用任意多的空间(水平)以填满屏幕的宽度。您可以使用 layout_weight 并将宽度设置为 fill_parent
。我现在无法访问 Eclipse,所以我无法测试,但我认为肯定还有一种方法可以在没有线性布局的情况下获得这种效果,但这是我想到的第一种方法。
<?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="#DFEFF1">
<LinearLayout
android:id="@+id/buttonRow"
android:orientation="horizontal"
android:layout_height="@dimen/default_height"
android:layout_width="fill_parent">
<Button
android:background="#AAAA11"
android:id="@+id/button1"
android:layout_width="0"
android:layout_height="@dimen/default_height"
android:text="SOME TEXT 1"
android:layout_weight="1"
/>
<Button
android:background="#FFAA11"
android:id="@+id/button2"
android:layout_width="0"
android:layout_height="@dimen/default_height"
android:text="SOME TEXT 2"
android:layout_weight="1" />
</LinearLayout>
</RelativeLayout>
关于android - 如何为 320dp 和 360dp 创建布局?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11092965/