【问题标题】:match_parent when parent is wrap_content?match_parent 当父母是 wrap_content 时?
【发布时间】:2015-04-02 22:06:56
【问题描述】:

我对 Android 开发还很陌生。我正在尝试添加一个彩色矩形来填充CardView 的高度。这是我的 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>

但是,该栏没有出现在卡片中。它似乎没有得到match_parent 的卡片高度(如果我在 dp 中的高度中硬编码,它会显示)。这是因为卡片的高度设置为wrap_content,还是完全不同?我使用的是 API 16。

编辑:这是我想要实现的目标:

但是,这是使用硬编码的 dp 值完成的,并且无法扩展到设备。我正在尝试让这个栏适应卡片的大小。

【问题讨论】:

  • 您不能指定冲突的布局参数。如果 child 有 match_parent,而 parent 有 wrap_content,则有一个循环。
  • @Zielony 我明白了。这就说得通了。我将如何以另一种方式实现我的目标?
  • 我猜你可以把 c​​olor_bar 的高度放在 dp(比如 50dp)和 RelativeLayout 的高度作为 wrap_content。它应该工作
  • @bipin 问题是我也有文本(因此是 wrap_content),并且栏无法缩放以适应,这意味着在许多设备上,栏要么太长要么太短。跨度>
  • 如果你想要一个靠近 CardView 的 View(栏),只需交换容器:RelativeLayout 将包含 CardView 和 View,一个靠近另一个。

标签: android xml material-design


【解决方案1】:

也许嵌套在LinearLayout 中并使用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>

不是很平坦,但似乎可以工作

【讨论】:

    猜你喜欢
    • 2017-07-04
    • 1970-01-01
    • 2017-11-27
    • 1970-01-01
    • 1970-01-01
    • 2017-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多