简体   繁体   中英

match_parent when parent is wrap_content?

I'm pretty new to Android development. I'm trying to add a colored rectangle to fill the height of a CardView . Here's my XML.

<android.support.v7.widget.CardView
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:id="@+id/card_view"
        android:layout_gravity="center_horizontal"
        android:layout_width="match_parent"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_height="wrap_content" >

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

            <View android:id="@+id/color_bar"
                android:layout_width="50dp"
                android:layout_height="match_parent"
                android:background="#673AB7" />

             ...

        </RelativeLayout>
    </android.support.v7.widget.CardView>

However, the bar is not appearing in the card. It doesn't seem to be getting the height of the card for match_parent (If I hard code in the height in dp, it displays). Is this due to the fact the card's height is set to wrap_content or is it something entirely different? I'm on API 16.

EDIT: Here's what I'm trying to achieve:

该图显示了我要实现的目标

However this was done with hard coded dp values and doesn't scale to devices. I'm trying to make this bar adapt to the card size.

Maybe nest in a LinearLayout and use weight ?

<android.support.v7.widget.CarView
    ...>

    <LinearLayout
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:orientation="horizontal">
         <View
             android:layout_width="0dp"
             android:layout_height="match_parent"
             android:layout_weight="10"
             android:background="#67BA37"/>
         <RelativeLayout
             android:layout_width="0dp"
             android:layout_height="match_parent"
             android:layout_weight="100"
             ...>
             ...
          </RelativeLayout>
    </LinearLayout>
</android.support.v7.widget.CardView>

Not very flat but seems to work

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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