【发布时间】:2018-01-15 19:42:57
【问题描述】:
我更喜欢 Swift 而不是 Android,但我正在努力学习它。
在 Swift 中,我使用了带有约束的自动布局,这让我可以很容易地进行布局。我似乎无法使用 Android XML 实现我想要的。
我正在创建一个自定义列表视图行。我希望该行与图像(128px)的高度相同,不多也不少。然后我希望标题居中并在标题下方,每张带有文字的 2 张图片。
- 使用相对布局,我将图像设置为 128x128。
- 场景标题居中对齐,主画面左侧 30dp 图片。
- 其他 2 个图像和文本视图始终位于标题下方
- 与标题开头对齐的第一个“SceneOptionImage”
- 其他视图在 'sceneOptionImage' 之后排成一行
我以为我拥有它,因为在较小的设备预览上看起来不错。但是当我提高屏幕分辨率时,就好像行的高度变小了。
4.0英寸屏幕:
6.0英寸屏幕:
那么为什么行的高度似乎发生了变化?
我能得到一些指示吗?谢谢。
这是完整的 XML 代码供参考:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="128px"
android:orientation="vertical">
<ImageView
android:id="@+id/sceneImage"
android:layout_width="128px"
android:layout_height="128px"
android:layout_margin="0px"
app:srcCompat="@mipmap/clipboard"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<TextView
android:id="@+id/sceneName"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="20dp"
android:text="Scene Title"
android:layout_toEndOf="@id/sceneImage"
android:layout_marginLeft="30dp"
android:gravity="center"/>
<ImageView
android:id="@+id/sceneOptionImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@mipmap/forest"
android:layout_below="@+id/sceneName"
android:layout_alignStart="@+id/sceneName"
android:layout_marginTop="18dp" />
<TextView
android:id="@+id/sceneOptionText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_toEndOf="@+id/sceneOptionImage"
android:layout_below="@id/sceneName"
android:layout_marginTop="20dp"
android:text="TextView" />
<ImageView
android:id="@+id/sceneColourImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="70dp"
app:srcCompat="@mipmap/forest"
android:layout_below="@+id/sceneName"
android:layout_alignStart="@+id/sceneOptionText"
android:layout_marginTop="18dp" />
<TextView
android:id="@+id/sceneColourText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_toEndOf="@+id/sceneColourImage"
android:layout_below="@id/sceneName"
android:layout_marginTop="20dp"
android:text="TextView" />
</RelativeLayout>
【问题讨论】:
-
使用
dp而不是px -
也许你应该试试ConstraintLayour
-
这很容易解决。谢谢。你有没有机会解释为什么所有设备的 dp 都是一样的,而 px 不是?
-
您应该为每种类型的屏幕设置布局文件,并始终将 dp 用于 android:layout_width 和 android:layout_height。布局渲染可能会根据屏幕密度和屏幕宽度而改变。
标签: java android xml android-layout android-relativelayout