简体   繁体   English

如何设置RelativeLayout的宽度与同级View相同?

[英]How to set RelativeLayout's width same as sibling View?

So i have layout like this: 所以我有这样的布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:gravity="center">

    <RelativeLayout
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:orientation="horizontal">

        <ImageView
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:src="@drawable/advantage_1" />
        <ImageView
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:src="@drawable/advantage_2" />
    </RelativeLayout>

    <ImageView
        android:id="@+id/lower_image_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/line" />
</LinearLayout>

Because of aligning views inside the RelativeLayout it fills all the screen. 由于在RelativeLayout内部对齐视图,因此它会填满整个屏幕。 How to set RelativeLayout's width same as lower ImageView(lower_image_view)? 如何设置RelativeLayout的宽度与较低的ImageView(lower_image_view)相同?

Try like this. 尝试这样。 The main idea is you put your RelativeLayout and the ImageView inside another RelativeLayout . 主要思想是将RelativeLayoutImageView放在另一个RelativeLayout Then set android:layout_alignLeft="@+id/lower_image_view" and android:layout_alignRight="@+id/lower_image_view" of the child RelativeLayout . 然后设置android:layout_alignLeft="@+id/lower_image_view"android:layout_alignRight="@+id/lower_image_view"孩子的RelativeLayout This will achieve the width of the ImageView . 这将实现ImageView的宽度。 You may want to modify this to achieve your desired result, but you get the idea :) 您可能需要修改此设置以实现所需的结果,但是您知道了:)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:gravity="center">

    <RelativeLayout
        android:id="@+id/parentRelativeLayout"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content">

        <RelativeLayout
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_alignLeft="@+id/lower_image_view"
            android:layout_alignRight="@+id/lower_image_view"
            android:orientation="horizontal">

            <ImageView
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_centerVertical="true"
                android:src="@drawable/ic_launcher" />

            <ImageView
                android:layout_height="wrap_content"
                android:layout_width="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:src="@drawable/ic_launcher" />
        </RelativeLayout>

        <ImageView
            android:id="@+id/lower_image_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/ic_launcher" />
    </RelativeLayout>
</LinearLayout>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM