我开发了显示两张图片的活动。我们的想法是将它们水平放置,将它们放在屏幕的中心。无论他们的尺寸如何,他们都应该:
我创建了以下布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="center">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginEnd="4dp"
android:layout_weight="1">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:background="@android:color/holo_blue_light">
<ImageView
android:id="@+id/image_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:src="@drawable/ic_test_the_universe_three" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="LEFT" />
</RelativeLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_marginStart="4dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:background="@android:color/holo_orange_light">
<ImageView
android:id="@+id/image_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_test_the_universe_two" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RIGHT"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"/>
</RelativeLayout>
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
当我放置高度大于屏幕一半的图像时,ImageView
的宽度会大于图像的宽度。当图像的宽度大于屏幕的一半时ImageView
的高度太大。
这是截图。 ImageView
尺寸以背景颜色着色。
我该如何解决?如何使ImageView
的宽度与图像的宽度相同?
答案 0 :(得分:1)
用户似乎已经使用约束布局解决了问题。
Constraint Layout允许您使用平面视图层次结构创建大型复杂布局(无嵌套视图组)。它与RelativeLayout类似,因为所有视图都是根据兄弟视图和父布局之间的关系布局的,但它比RelativeLayout更灵活,更易于使用Android Studio的布局编辑器。
答案 1 :(得分:0)
您可以在代码下方的LinearLayout .try中使用权重。
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal"
android:weightSum="3">
<ImageView
android:id="@+id/iv_attach_1"
android:layout_width="0dp"
android:layout_height="80dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:contentDescription="@string/image"
android:src="@drawable/ic_attach_image" />
<ImageView
android:id="@+id/iv_attach_2"
android:layout_width="0dp"
android:layout_height="80dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:contentDescription="@string/image"
android:src="@drawable/ic_attach_image" />
<ImageView
android:id="@+id/iv_attach_3"
android:layout_width="0dp"
android:layout_height="80dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:contentDescription="@string/image"
android:src="@drawable/ic_attach_image" />
</LinearLayout>