我开发 Android 应用程序已经一年多了,现在已经取得了成功,但有一件事对我来说仍然是个问题: 如何设计结合 TextView、ImageView 和 Button 的布局,同时保留不同屏幕尺寸上每个元素之间的关系。
我想构建一个这样的布局:
它是针对 ListView 的,所以使用了很多这样的布局。该应用程序针对智能手机和平板电脑具有不同的布局。
因此,右侧的橙色按钮应始终有 3 行文本,但仍应具有最大宽度,左侧的图像应与右侧的按钮具有相同的高度。中间的 3 行文本应尽可能多地占据空间。星形图像应与其右侧的文本具有相同的高度。
我已经成功地使用 TableLayout 构建了一个类似的测试布局,以下是 AndroidStudio 的预览: Nexus S
Nexus 6
在 Nexus S 屏幕尺寸上,布局还可以,但在更大的屏幕上就很难看了。按钮太小,左边的图像也太小。
这是我的测试布局的代码:
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="1"
>
<TableRow android:orientation="horizontal"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/iv1"
android:layout_width="80dp"
android:layout_height="match_parent"
android:contentDescription="@string/dummy"
android:padding="@dimen/raster4dp"
android:scaleType="fitXY"
android:src="@drawable/avatar_1" />
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="some nice text here"
android:layout_alignParentLeft="true"
android:textAppearance="?android:attr/textAppearanceMedium"
/>
<ImageView
android:id="@+id/iv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tv1"
android:src="@drawable/ic_male_user_bw"
android:layout_alignParentLeft="true"
/>
<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="more nice text"
android:layout_toRightOf="@id/iv2"
android:layout_alignBottom="@id/iv2"
android:textAppearance="?android:attr/textAppearanceMedium"
/>
<ImageView
android:id="@+id/iv3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/iv2"
android:layout_alignParentLeft="true"
android:src="@drawable/ic_male_user_bw"
/>
<TextView
android:id="@+id/tv3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="more nice text"
android:layout_toRightOf="@id/iv3"
android:layout_alignBottom="@id/iv3"
android:textAppearance="?android:attr/textAppearanceMedium"
/>
</RelativeLayout>
<Button
android:id="@+id/btn1"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:background="@drawable/button_dropshadow_black_xml"
android:text="Line1\nLine2\nLine3"
android:textColor="@android:color/white" />
</TableRow>
</TableLayout>
所以希望我的问题不太愚蠢,但我在理解如何解决这个问题时遇到了一些问题。目前我在按钮和左侧图像上使用固定 80dp 的宽度。我认为这并不是 Android 上的工作方式。 我需要在这里使用什么尺寸,什么样的布局?
我知道开发者网站上有关sreensizes等的部分( https://developer.android.com/guide/practices/screens_support.html ),但对我来说没有多大帮助。
感谢您的帮助! :)
最佳答案
每个终端都有不同的尺寸。如果您放置一个大小为 80dp 的按钮,当其他终端屏幕较大时,该按钮将比您进行测试的终端屏幕更小。
你应该使用WEIGHT
。
| | | |
|_____________|_________________________|____________|
0.3 0.5 0.2 0.3 + 0.5 + 0.2 = 1 <-Weight.
阅读this ,会有帮助。 另外there's a question和你的类似,也检查一下。
关于android - 尝试了解 Android 布局、图像大小和关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26522384/