【问题标题】:Android layout multiple screen sizes and densitiesAndroid 布局多种屏幕尺寸和密度
【发布时间】:2014-01-15 05:26:18
【问题描述】:

我有以下布局。我在 ldpi、mdpi、hdpi 和 xhdpi 适当的文件夹中有图像。我不明白为什么布局不能缩放到适当的大小。我添加了 ScrollView,因此至少您可以滚动查看整个布局,但这不是预期的效果。我希望所有内容在不滚动的情况下以各种大小和密度可见。我已经阅读了有关支持多种尺寸和密度的 Android 文档,但我不明白从哪里开始。我正在使用“adb shell am display-density”和“adb shell am display-size”命令在我的 Nexus 10 上模拟不同的设备。提前致谢。

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<RelativeLayout 
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/bg" >

<View
  android:id="@+id/viewSpacer0"
  android:layout_width="1dp"
  android:layout_height="7dip"
  android:layout_centerHorizontal="true" />

<LinearLayout
  android:id="@+id/linearLayoutLine1"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_centerHorizontal="true"
  android:orientation="horizontal"
  android:layout_below="@+id/viewSpacer0" >

<ImageView
  android:id="@+id/imageLogo"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:scaleType="fitCenter"
  android:src="@drawable/logo_large" />

<View
  android:id="@+id/viewSpacer1"
  android:layout_width="20dp"
  android:layout_height="1dip" />

<ImageView
  android:id="@+id/imageView50Best"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:adjustViewBounds="true"
  android:src="@drawable/fifty_best_logo" />

</LinearLayout>

<View
  android:id="@+id/viewSpacer2"
  android:layout_width="1dp"
  android:layout_height="10dip"
  android:layout_below="@+id/linearLayoutLine1"
  android:layout_centerHorizontal="true" />

<TableLayout
  android:id="@+id/tableLayoutHeader"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_below="@+id/viewSpacer2"
  android:layout_centerHorizontal="true"
  android:background="@color/transparent" >

<TableRow
  android:id="@+id/tableRowPhoneNumberLabels"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content" 
  android:gravity="center" >

<TextView
  android:id="@+id/textViewHospital"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="Hospital"
  android:textSize="20sp"
  android:textColor="@color/white" />

<View
  android:id="@+id/viewSpacerBetweenLabels"
  android:layout_width="15dp"
  android:layout_height="1dip"
  android:layout_below="@+id/tableLayoutHeader"
  android:layout_centerHorizontal="true" />

<TextView
  android:id="@+id/textViewEmergencyRoom"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="Emergency Room"
  android:textSize="20sp"
  android:textColor="@color/white" />

</TableRow>

<TableRow
  android:id="@+id/tableRowPhoneNumbers"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content" 
  android:gravity="center" >

<TextView
  android:id="@+id/textViewHospitalSwitchboard"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="(xxx) xxx-xxxx"
  android:textSize="20sp"
  android:textColor="@color/white" />

<View
  android:id="@+id/viewSpacerBetweenPhoneNumbers"
  android:layout_width="15dp"
  android:layout_height="1dip"
  android:layout_below="@+id/tableLayoutHeader"
  android:layout_centerHorizontal="true" />

<TextView
  android:id="@+id/textViewEmergencyPhone"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="(xxx) xxx-xxxx"
  android:textSize="20sp"
  android:textColor="@color/white" />

</TableRow>

</TableLayout>

<View
  android:id="@+id/viewSpacer3"
  android:layout_width="1dp"
  android:layout_height="15dip"
  android:layout_below="@+id/tableLayoutHeader"
  android:layout_centerHorizontal="true" />

<TableLayout
  android:id="@+id/tableLayoutMenu"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_below="@+id/viewSpacer3"
  android:layout_centerHorizontal="true"
  android:background="@color/transparent" >

<TableRow
  android:id="@+id/tableRow1"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content" 
  android:gravity="center" >

<ImageButton
  android:id="@+id/imageDrSearch"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:background="@color/transparent"
  android:padding="@dimen/padding_xsmall"
  android:src="@drawable/icon_dr_search" />

<ImageButton
  android:id="@+id/imageLocations"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:background="@color/transparent"
  android:padding="@dimen/padding_xsmall"
  android:src="@drawable/icon_locations" />

</TableRow>

<TableRow
  android:id="@+id/tableRow2"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content" 
  android:gravity="center" >

<ImageButton
  android:id="@+id/imageSendCard"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:background="@color/transparent"
  android:padding="@dimen/padding_xsmall"
  android:src="@drawable/icon_send_card" />

<ImageButton
  android:id="@+id/imageServices"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:background="@color/transparent"
  android:padding="@dimen/padding_xsmall"
  android:src="@drawable/icon_services" />

</TableRow>

<TableRow
  android:id="@+id/tableRow3"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content" 
  android:gravity="center" >

<ImageButton
  android:id="@+id/ImageHealthEncyclopedia"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:background="@color/transparent"
  android:padding="@dimen/padding_xsmall"
  android:src="@drawable/icon_health_encyclopedia" />

<ImageButton
  android:id="@+id/imageClassesEvents"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:background="@color/transparent"
  android:padding="@dimen/padding_xsmall"
  android:src="@drawable/icon_events_classes" />

</TableRow>

<TableRow
  android:id="@+id/tableRow4"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content" 
  android:gravity="center" >

<ImageButton
  android:id="@+id/imageCareers"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:background="@color/transparent"
  android:padding="@dimen/padding_xsmall"
  android:src="@drawable/icon_careers" />

<ImageButton
  android:id="@+id/imageNewsroom"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:background="@color/transparent"
  android:padding="@dimen/padding_xsmall"
  android:src="@drawable/icon_newsroom" />

</TableRow>

</TableLayout>

<View
  android:id="@+id/viewSpacer4"
  android:layout_width="1dp"
  android:layout_height="5dip"
  android:layout_below="@+id/tableLayoutMenu"
  android:layout_centerHorizontal="true" />

<LinearLayout
  android:id="@+id/linearLayoutLine3"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_centerHorizontal="true"
  android:layout_below="@+id/viewSpacer4"
  android:background="@color/lightgrey" >

<ImageButton
  android:id="@+id/imageViewWebsite"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:background="@color/transparent"
  android:padding="@dimen/padding_medium"
  android:src="@drawable/www_small" />

<ImageButton
  android:id="@+id/imageViewFacebook"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:background="@color/transparent"
  android:padding="@dimen/padding_medium"
  android:src="@drawable/facebook_small" />

<ImageButton
  android:id="@+id/imageViewTwitter"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:background="@color/transparent"
  android:padding="@dimen/padding_medium"
  android:src="@drawable/twitter_small" />

</LinearLayout>

</RelativeLayout>

</ScrollView>

根据 Glee 的建议更新:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/bg" >

<View
  android:id="@+id/viewSpacer0"
  android:layout_width="1dp"
  android:layout_height="7dip"
  android:layout_centerHorizontal="true" />

<LinearLayout
  android:id="@+id/linearLayoutLine1"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_centerHorizontal="true"
  android:orientation="horizontal"
  android:layout_below="@+id/viewSpacer0" >

<ImageView
  android:id="@+id/imageLogo"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_weight="2"
  android:scaleType="center"
  android:src="@drawable/logo_large" />

<View
  android:id="@+id/viewSpacer1"
  android:layout_weight="1"
  android:layout_width="20dp"
  android:layout_height="1dip" />

<ImageView
  android:id="@+id/imageView50Best"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_weight="2"
  android:adjustViewBounds="true"
  android:src="@drawable/fifty_best_logo" />

</LinearLayout>

<TableLayout
  android:id="@+id/tableLayoutHeader"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_below="@+id/linearLayoutLine1"
  android:layout_centerHorizontal="true"
  android:background="@color/transparent" >

<TableRow
  android:id="@+id/tableRowPhoneNumberLabels"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content" 
  android:gravity="center" >

<TextView
  android:id="@+id/textViewHospital"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="Hospital"
  android:layout_weight="2"
  android:textSize="20sp"
  android:textColor="@color/white" />

<View
  android:id="@+id/viewSpacerBetweenLabels"
  android:layout_width="15dp"
  android:layout_height="1dip"
  android:layout_weight="1"
  android:layout_below="@+id/tableLayoutHeader" />

<TextView
  android:id="@+id/textViewEmergencyRoom"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="Emergency Room"
  android:layout_weight="2"
  android:textSize="20sp"
  android:textColor="@color/white" />

</TableRow>

<TableRow
  android:id="@+id/tableRowPhoneNumbers"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content" 
  android:gravity="center" >

<TextView
  android:id="@+id/textViewHospitalSwitchboard"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="(xxx) xxx-xxxx"
  android:textSize="20sp"
  android:layout_weight="2"
  android:textColor="@color/white" />

<View
  android:id="@+id/viewSpacerBetweenPhoneNumbers"
  android:layout_width="15dp"
  android:layout_height="1dip"
  android:layout_below="@+id/tableLayoutHeader"
  android:layout_weight="1"
  android:layout_centerHorizontal="true" />

<TextView
  android:id="@+id/textViewEmergencyPhone"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="(xxx) xxx-xxxx"
  android:textSize="20sp"
  android:layout_weight="2"
  android:textColor="@color/white" />

</TableRow>

</TableLayout>

<View
  android:id="@+id/viewSpacer3"
  android:layout_width="1dp"
  android:layout_height="15dip"
  android:layout_below="@+id/tableLayoutHeader"
  android:layout_centerHorizontal="true" />

<TableLayout
  android:id="@+id/tableLayoutMenu"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_below="@+id/viewSpacer3"
  android:layout_centerHorizontal="true"
  android:background="@color/transparent" >

<TableRow
  android:id="@+id/tableRow1"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content" 
  android:gravity="center" >

<ImageButton
  android:id="@+id/imageDrSearch"
  android:background="@color/transparent"
  android:padding="@dimen/padding_xsmall"
  android:scaleType="center"
  android:layout_weight="1"
  android:src="@drawable/icon_dr_search" />

<ImageButton
  android:id="@+id/imageLocations"
  android:background="@color/transparent"
  android:padding="@dimen/padding_xsmall"
  android:scaleType="center"
  android:layout_weight="1"
  android:src="@drawable/icon_locations" />

</TableRow>

<TableRow
  android:id="@+id/tableRow2"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content" 
  android:gravity="center" >

<ImageButton
  android:id="@+id/imageSendCard"
  android:background="@color/transparent"
  android:padding="@dimen/padding_xsmall"
  android:scaleType="center"
  android:layout_weight="1"
  android:src="@drawable/icon_send_card" />

<ImageButton
  android:id="@+id/imageServices"
  android:background="@color/transparent"
  android:padding="@dimen/padding_xsmall"
  android:scaleType="center"
  android:layout_weight="1"
  android:src="@drawable/icon_services" />

</TableRow>

<TableRow
  android:id="@+id/tableRow3"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content" 
  android:gravity="center" >

<ImageButton
  android:id="@+id/ImageHealthEncyclopedia"
  android:background="@color/transparent"
  android:padding="@dimen/padding_xsmall"
  android:scaleType="center"
  android:layout_weight="1"
  android:src="@drawable/icon_health_encyclopedia" />

<ImageButton
  android:id="@+id/imageClassesEvents"
  android:background="@color/transparent"
  android:padding="@dimen/padding_xsmall"
  android:scaleType="center"
  android:layout_weight="1"
  android:src="@drawable/icon_events_classes" />

</TableRow>

<TableRow
  android:id="@+id/tableRow4"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content" 
  android:gravity="center" >

<ImageButton
  android:id="@+id/imageCareers"
  android:background="@color/transparent"
  android:padding="@dimen/padding_xsmall"
  android:scaleType="center"
  android:layout_weight="1"
  android:src="@drawable/icon_careers" />

<ImageButton
  android:id="@+id/imageNewsroom"
  android:background="@color/transparent"
  android:padding="@dimen/padding_xsmall"
  android:scaleType="center"
  android:layout_weight="1"
  android:src="@drawable/icon_newsroom" />

</TableRow>

</TableLayout>

<View
  android:id="@+id/viewSpacer4"
  android:layout_width="1dp"
  android:layout_height="5dip"
  android:layout_below="@+id/tableLayoutMenu"
  android:layout_centerHorizontal="true" />

<LinearLayout
  android:id="@+id/linearLayoutLine3"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_centerHorizontal="true"
  android:layout_below="@+id/viewSpacer4"
  android:background="@color/lightgrey" >

<ImageButton
  android:id="@+id/imageViewWebsite"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:background="@color/transparent"
  android:padding="@dimen/padding_medium"
  android:layout_weight="5"
  android:src="@drawable/www_small" />

<ImageButton
  android:id="@+id/imageViewFacebook"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:background="@color/transparent"
  android:padding="@dimen/padding_medium"
  android:layout_weight="5"
  android:src="@drawable/facebook_small" />

<ImageButton
  android:id="@+id/imageViewTwitter"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:background="@color/transparent"
  android:padding="@dimen/padding_medium"
  android:layout_weight="5"
  android:src="@drawable/twitter_small" />

</LinearLayout>

【问题讨论】:

  • 也许你的资源/drawable-xxx 上的 img 没有正确缩放:/,我在我的一个项目中也发现了这个问题,今天我正在测试这个 photoshop 插件 (cutandslice.me ) 帮助我解决资源大小问题
  • 您可能还想检查运行时是否返回了正确的屏幕尺寸和 DPI 值,以防您的 adb 命令被忽略。
  • @TheIT - 感谢您的评论。我该怎么做呢?运行 adb 命令后屏幕大小正在发生变化。

标签: android layout


【解决方案1】:

*dpi 文件夹中的不同资源只考虑屏幕密度,而不考虑屏幕大小。您的图像资源仍然具有一定的固有尺寸:它们可能太大或太小,以至于您希望它们适应的空间取决于屏幕尺寸。有根据屏幕大小(小、正常、大、超大)提供资源的选项,但这些文件夹仅将屏幕划分为存储桶。每个存储桶仍然有不同的屏幕尺寸,它们只是靠得更近了。因此,不可能为每个屏幕提供完全正确的资源大小。相反,您需要告诉您的布局以适当地缩放事物。例如,现在,使用android:layout_height="wrap_content",,您正在告诉您的顶级RelativeLayout,您希望它尽可能长,即使它延伸到屏幕外。相反,您应该删除滚动视图并使用android:layout_height="match_parent" 将其固定到屏幕大小。在 LinearLayouts 内部,使用 android:layout_weight 定义哪些对象占用空白空间。在 ImageViews 上,使用android:scaleType 来定义图像的缩放方式。

【讨论】:

  • 非常感谢您的建议。我一直在玩布局。有关我的更改,请参阅上面的帖子。它已经有所改善,但我还差得很远。还有其他建议吗?
  • 这有什么问题?您可能希望将相对布局更改为线性布局,并为其所有子级使用权重。对于一些较小的屏幕,您也可能有太多东西。
  • 小型和普通屏幕(在developer.android.com/guide/practices/screens_support.html 上定义为“表 3”)的底部和侧面被切断。一些大屏幕尺寸的底部被切断。如何使布局在所有尺寸上都完全可见?
  • 在使用高分辨率图像时,您的某些图像视图可能太大,因为您仍然为图像视图及其封闭布局设置了 wrap_content。尝试使用较小的图像。您还可以尝试将顶部布局设置为线性布局,然后将子线性布局设置为 match_parent 和 layout_weight。最后,尝试从顶层布局开始,一次添加一个。这样你就知道是什么搞砸了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多