[英]Android XML: Layout row changes on device change
我比Android更喜欢Swift,但是我正在尝试学习它。
使用Swift时,我使用了带有约束的自动布局,这使我可以非常轻松地进行布局。 我似乎无法使用Android XML实现我想要的功能。
我正在创建一个自定义列表视图行。 我希望该行与图像的高度相同(128px)不多或少。 然后,我希望标题居中,并在标题下方,每个文本带有2张图像。
我以为我拥有它,因为在较小的设备预览中看起来还不错。 但是当我提高屏幕分辨率时,好像行的高度变小了。
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>
解决您的问题的方法是重量和。 这将允许您在不同的屏幕尺寸上管理布局
<LinearLayout 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="128dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:orientation="vertical">
<LinearLayout
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Scene Title"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal">
<LinearLayout
android:gravity="center"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text View"/>
</LinearLayout>
<LinearLayout
android:gravity="center"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text View"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.