作为更大 UI 的一部分,我有一个 RelativeLayout
,其中包含三个 View ,我希望它们具有相同的宽度。三个 View 像这样“堆叠”在一起
ImageView
TextView
ImageView
我尝试了各种方法,例如将 View
的 android:layout_width
设置为“wrap_content”,将 android:layout_width
设置为"0px"和 android:layout_weight
到 "1",如 this answer 所建议,并将 View
放置在 LinearLayout
中,但没有成功。
如何让这三个 View 的宽度相同?
布局 xml 的相关部分:
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="0px"
android:layout_weight="1">
<ImageView
android:layout_above="@string/window_text"
android:layout_below="@string/eighteen_id"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:paddingTop="5dp"
android:layout_alignParentRight="true"
android:paddingBottom="5dp"
android:src="@drawable/line1"
android:id="@string/line_1_id"/>
<TextView
android:layout_above="@string/line_2_id"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:gravity="right"
android:layout_centerVertical="true"
android:textColor="#000000"
android:id="@string/window_text"/>
<ImageView
android:layout_above="@string/top_row_id"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:paddingTop="5dp"
android:paddingBottom="10dp"
android:src="@drawable/line1"
android:id="@string/line_2_id"/>
</RelativeLayout>
最佳答案
这个很简单,看下面。 “魔法”很简单。将父 View 设置为 wrap_content,将 subview 设置为填充父 View 。较小的 subview 总是设置为最大 subview 的大小。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="@+id/bottom_row"
android:orientation="vertical"
android:layout_alignParentBottom="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="@+id/view1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="123" />
<Button
android:id="@+id/view2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="more text" />
<Button
android:id="@+id/view3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="even more text" />
</LinearLayout>
</RelativeLayout>
关于android - 如何使多个 View 具有相同的宽度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4708109/