我是 android 编程的初学者。首先,我需要 2 台设备手机 (480x800) 和平板电脑 (1280x800) 支持我的应用。
我读了这篇文章并尝试一步一步地做。 Supporting Multiple Screens
所以我创建了带有布局的文件夹:
res/layout/firstPage.xml//this is by default phone layout port and land
res/layout-sw720dp/firstPage.xml//this is tablet port layout
res/layout-sw720dp-land/firstPage.xml//this is tablet land layout
我的 xml 看起来像这样:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_gravity="center_vertical"
android:baselineAligned="false"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:text="@string/button1" />
<Button
android:id="@+id/button2"
android:text="@string/button2" />
<Button
android:id="@+id/button3"
android:text="@string/button3" />
</LinearLayout>
我不提供有关按钮的更多信息,因为在每个布局中它们都是不同的。
在我的 AndroidManifest.xml 中有这一行:
<supports-screens android:anyDensity="true" android:normalScreens="true" android:resizeable="true" android:smallScreens="true" android:largeScreens="true"/>
如果我将 tru 设置为这个 anyDensity="true",也许我不需要为其他人设置 true
所以我想我做了我应该做的一切。但是当我想在平板电脑上测试时,它会默认显示手机布局。 那我错过了什么? 或者我还需要做一些编码?
编辑:
我想我明白为什么... 从 Android 3.2(API 级别 13)开始,上述尺寸组已弃用,您应该改为使用 swdp 配置限定符来定义布局资源所需的最小可用宽度。
我的表是 3.1,为什么找不到布局? 但是如果我想支持 2.1 到 4 怎么办?
最佳答案
解决方法:
您应该遵循:Best Practise
尝试针对不同的屏幕尺寸使用不同的布局:
就像:
res/layout/my_layout.xml // layout for normal screen size ("default")res/layout-small/my_layout.xml // layout for small screen size
res/layout-large/my_layout.xml // layout for large screen size
res/layout-xlarge/my_layout.xml // layout for extra large screen size
res/layout-xlarge-land/my_layout.xml // layout for extra large in landscape orientation
根据设备的不同屏幕尺寸使用不同尺寸的图像:
就像:
res/drawable-mdpi/my_icon.png // bitmap for medium density
res/drawable-hdpi/my_icon.png // bitmap for high density
res/drawable-xhdpi/my_icon.png // bitmap for extra high density
仍然,如果它不能解决您的问题,那么请为各个不同布局的图像使用静态大小。
根据我的观点和文档,它自己解释说您应该必须遵循某些步骤才能做到这一点。
希望对您有所帮助。如果没有,请告诉我您在管理布局方面应该停止的地方。
谢谢。
关于java - 支持多屏(手机和平板),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9290888/