Android 如何使多个视图具有相同的宽度?

Android 如何使多个视图具有相同的宽度?,android,layout,view,size,width,Android,Layout,View,Size,Width,作为更大UI的一部分,我有一个RelativeLayout,其中有三个视图,我希望它们具有相同的宽度。这三个视图是这样“堆叠”在一起的 ImageView TextView ImageView 我尝试过各种方法,比如将Viewsandroid:layout\u width设置为“wrap\u content”,将android:layout\u width设置为“0px”和android:layou weight设置为所建议的“1”,并将视图s设置为LinearLayout,但没有成功 如何使这

作为更大UI的一部分,我有一个
RelativeLayout
,其中有三个视图,我希望它们具有相同的宽度。这三个视图是这样“堆叠”在一起的

  • ImageView
  • TextView
  • ImageView
  • 我尝试过各种方法,比如将
    View
    s
    android:layout\u width
    设置为“wrap\u content”,将
    android:layout\u width
    设置为“0px”和
    android:layou weight
    设置为所建议的“1”,并将
    视图
    s设置为
    LinearLayout
    ,但没有成功

    如何使这三个视图的宽度相同

    布局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>
    
    
    
    您是否尝试将android:layout\u width=“fill\u parent”设置为两个图像视图,就像设置TextView一样?如果希望这三个视图的宽度为最宽的宽度,可以将所有三个视图包装在一个具有android:layout_width=“wrap_content”的线性布局中。

    您可以通过设置android:scaleType=“”使图像视图填充父视图的宽度在ImageView中,但如果文本没有填满屏幕宽度,并且您希望图像宽度与文本宽度完全匹配,则必须通过编程获得文本视图宽度,然后将ImageView的宽度设置为该宽度。

    这非常简单,请参见下文。“魔法”很简单。将父视图设置为包裹内容,子视图设置为填充父视图。然后,较小的子视图总是设置为最大子视图的大小

    <?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>
    
    
    

    您显示的是相对定位,而不是线性布局?我在此
    相对定位下还有几个
    视图
    ,它们使用相对定位。我尝试了您的两个建议,但都不起作用。文本溢出了左右两侧的图像,我遇到了相反的情况。我希望文本视图的宽度不要超过ImageView的宽度。文本视图设置为包装内容时,比图像视图宽得多。所以我必须在代码中设置它们的宽度。在RelativeLayout中有一个布局匹配宽度项会很好。