【问题标题】:How to use "full" of wrap_content, created by other views如何使用由其他视图创建的“完整”的 wrap_content
【发布时间】:2016-03-19 00:13:51
【问题描述】:

目前我正在开发一个精美的用户界面。现在我来了:

我卡在左侧的“profile_layout”上,里面有松饼。 对于右侧的两个栏,我使用 wrap_content。

但现在我坚持让 profile_layout 使用两个栏的“wrap_content”的全部高度。

有什么办法解决这个问题吗?

这是我的代码:

<RelativeLayout
android:id="@+id/top_bar"
android:layout_width="match_parent"
android:background="@color/colorPrimaryDark"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:padding="8dp"
xmlns:app="http://schemas.android.com/tools">

<LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@null"
    >

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@null"
        >
        <android.support.v7.widget.CardView
            android:id="@+id/profile_layout"
            android:layout_width="100dp"
            android:layout_height="wrap_content"
            android:layout_margin="4dp"
            card_view:cardCornerRadius="2dp"
            card_view:cardElevation="2dp"
            card_view:cardBackgroundColor="#ffffff"
            >

            <com.pkmmte.view.CircularImageView
                android:layout_width="80dp"
                android:layout_height="80dp"
                android:src="@drawable/muffin"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="20dp"
                android:background="@null"
                app:border="true"
                app:border_color="#EEEEEE"
                app:border_width="4dp"
                app:shadow="true"
                />

            <TextView
                android:textColor="#212121"
                android:layout_gravity="bottom|center_horizontal"
                android:layout_marginBottom="10dp"
                android:text="Muffin"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@null"
                />

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


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@null"
        >
        <android.support.v7.widget.CardView
            android:id="@+id/hp_bar_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="4dp"
            android:layout_marginBottom="4dp"
            card_view:cardCornerRadius="2dp"
            card_view:cardElevation="2dp"
            card_view:cardBackgroundColor="#ffffff"
            >
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:background="@null"
                android:padding="3dp"
                >
                <TextView
                    android:id="@+id/hp_text"
                    android:textColor="#212121"
                    android:textSize="11sp"
                    android:text="HP"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="2dp"
                    android:layout_marginLeft="2dp"
                    android:background="@null"
                    />
                <ProgressBar
                    android:id="@+id/hp_bar"
                    android:layout_width="match_parent"
                    style="?android:attr/progressBarStyleHorizontal"
                    android:layout_height="20dp"
                    android:progress="43"
                    android:progressDrawable="@drawable/hp_bar"
                    android:background="@null"
                    />
                <TextView
                    android:id="@+id/hp_currently"
                    android:textColor="#212121"
                    android:textSize="13sp"
                    android:text="43/100"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="2dp"
                    android:layout_marginLeft="2dp"
                    android:background="@null"
                    />
            </LinearLayout>
        </android.support.v7.widget.CardView>
        <android.support.v7.widget.CardView
            android:id="@+id/mp_bar_layout"
            android:layout_below="@id/hp_bar_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="4dp"
            android:layout_marginBottom="4dp"
            card_view:cardCornerRadius="2dp"
            card_view:cardElevation="2dp"
            card_view:cardBackgroundColor="#ffffff"
            >
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:background="@null"
                android:padding="3dp"
                >
                <TextView
                    android:id="@+id/mp_text"
                    android:textColor="#212121"
                    android:textSize="11sp"
                    android:text="MP"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="2dp"
                    android:layout_marginLeft="2dp"
                    android:background="@null"
                    />
                <ProgressBar
                    android:id="@+id/mp_bar"
                    android:layout_width="match_parent"
                    style="?android:attr/progressBarStyleHorizontal"
                    android:layout_height="20dp"
                    android:progress="17"
                    android:progressDrawable="@drawable/mp_bar"
                    android:background="@null"
                    />
                <TextView
                    android:id="@+id/mp_currently"
                    android:textColor="#212121"
                    android:textSize="13sp"
                    android:text="17/100"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="2dp"
                    android:layout_marginLeft="2dp"
                    android:background="@null"
                    />
            </LinearLayout>
        </android.support.v7.widget.CardView>
    </RelativeLayout>
</LinearLayout>

【问题讨论】:

    标签: android android-linearlayout android-wrap-content


    【解决方案1】:

    将包含 muffin-layout_height 的卡片视图更改为 match_parent。如果不能解决请评论

    编辑:好的,经过更多思考后,我发现相对布局在这里引起了问题。在这里您可以完全删除相对布局或将其替换为线性布局。最后要解决您的问题,cardview 的高度必须为 match_parent,并且其直接父级(封闭标记)必须是高度为 wrap_content 的相对布局

    【讨论】:

    • 您是否编辑了正确的卡片视图?里面有松饼图像视图的那个?
    • 是的,卡片视图延伸到整个屏幕。
    • 刚刚删除了relativelayout并将cardview高度设置为match_parent,就可以了,非常感谢! :D
    • 检查这个问题-stackoverflow.com/questions/18303410/… 它有另一种解决方案,以防您绝对必须将相对布局作为父级
    【解决方案2】:

    你必须像这样使用线性布局:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:background="@drawable/card_background_selector"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"> <!-- You may give a definite value too -->
    
        <ImageView
            android:padding="@dimen/icon_padding_large"
            android:src="@mipmap/ic_launcher"
            android:layout_weight="3"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:id="@+id/appIcon" />
    
        <LinearLayout
            android:padding="@dimen/icon_padding_large"
            android:orientation="vertical"
            android:layout_weight="7"
            android:layout_width="0dp"
            android:layout_height="wrap_content">
    
            <TextView
                android:singleLine="true"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="New Text"
                android:id="@+id/appName" />
    
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:text="Small Text"
                android:id="@+id/appDeveloper" />
        </LinearLayout>
    </LinearLayout>
    

    【讨论】:

      【解决方案3】:

      LinearLayout 替换为RelativeLayout,并将第一个子元素的顶部和底部与第二个子元素对齐。当然,设置第二个孩子toRightOf 第一个孩子。然后,android:layout_centerVertical="true" 第一个孩子的 CardView。

      <RelativeLayout
          xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:app="http://schemas.android.com/tools"
          xmlns:card_view="http://schemas.android.com/apk/res-auto"
          android:id="@+id/top_bar"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:padding="8dp">
      
          <RelativeLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:background="@null">
      
              <RelativeLayout
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:background="@null"
      
                  android:id="@+id/column_1"
                  android:layout_alignBottom="@+id/column_2"
                  android:layout_alignTop="@+id/column_2"
                  >
      
                  <android.support.v7.widget.CardView
                      android:id="@+id/profile_layout"
                      android:layout_width="100dp"
                      android:layout_height="wrap_content"
                      android:layout_margin="4dp"
                      card_view:cardBackgroundColor="#ffffff"
                      card_view:cardCornerRadius="2dp"
                      card_view:cardElevation="2dp"
      
                      android:layout_centerVertical="true"
                      >
      
                      <com.pkmmte.view.CircularImageView
                          android:layout_width="80dp"
                          android:layout_height="80dp"
                          android:layout_gravity="center_horizontal"
                          android:layout_marginTop="20dp"
                          android:background="@null"
                          android:src="@drawable/muffin"
                          app:border="true"
                          app:border_color="#EEEEEE"
                          app:border_width="4dp"
                          app:shadow="true" />
      
                      <TextView
                          android:layout_width="wrap_content"
                          android:layout_height="wrap_content"
                          android:layout_gravity="bottom|center_horizontal"
                          android:layout_marginBottom="10dp"
                          android:background="@null"
                          android:text="Muffin"
                          android:textColor="#212121" />
      
                  </android.support.v7.widget.CardView>
              </RelativeLayout>
      
      
              <RelativeLayout
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:background="@null"
      
                  android:id="@+id/column_2"
                  android:layout_toRightOf="@id/column_1"
                  >
      
                  <android.support.v7.widget.CardView
                      android:id="@+id/hp_bar_layout"
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:layout_marginBottom="4dp"
                      android:layout_marginTop="4dp"
                      card_view:cardBackgroundColor="#ffffff"
                      card_view:cardCornerRadius="2dp"
                      card_view:cardElevation="2dp">
      
                      <LinearLayout
                          android:layout_width="match_parent"
                          android:layout_height="wrap_content"
                          android:background="@null"
                          android:orientation="vertical"
                          android:padding="3dp">
      
                          <TextView
                              android:id="@+id/hp_text"
                              android:layout_width="wrap_content"
                              android:layout_height="wrap_content"
                              android:layout_marginBottom="2dp"
                              android:layout_marginLeft="2dp"
                              android:background="@null"
                              android:text="HP"
                              android:textColor="#212121"
                              android:textSize="11sp" />
      
                          <ProgressBar
                              android:id="@+id/hp_bar"
                              style="?android:attr/progressBarStyleHorizontal"
                              android:layout_width="match_parent"
                              android:layout_height="20dp"
                              android:background="@null"
                              android:progress="43"
                              android:progressDrawable="@drawable/hp_bar" />
      
                          <TextView
                              android:id="@+id/hp_currently"
                              android:layout_width="wrap_content"
                              android:layout_height="wrap_content"
                              android:layout_marginLeft="2dp"
                              android:layout_marginTop="2dp"
                              android:background="@null"
                              android:text="43/100"
                              android:textColor="#212121"
                              android:textSize="13sp" />
                      </LinearLayout>
                  </android.support.v7.widget.CardView>
      
                  <android.support.v7.widget.CardView
                      android:id="@+id/mp_bar_layout"
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:layout_below="@id/hp_bar_layout"
                      android:layout_marginBottom="4dp"
                      android:layout_marginTop="4dp"
                      card_view:cardBackgroundColor="#ffffff"
                      card_view:cardCornerRadius="2dp"
                      card_view:cardElevation="2dp">
      
                      <RelativeLayout
                          android:layout_width="match_parent"
                          android:layout_height="wrap_content"
                          android:background="@null"
                          android:orientation="vertical"
                          android:padding="3dp">
      
                          <TextView
                              android:id="@+id/mp_text"
                              android:layout_width="wrap_content"
                              android:layout_height="wrap_content"
                              android:layout_marginBottom="2dp"
                              android:layout_marginLeft="2dp"
                              android:background="@null"
                              android:text="MP"
                              android:textColor="#212121"
                              android:textSize="11sp" />
      
                          <ProgressBar
                              android:id="@+id/mp_bar"
                              style="?android:attr/progressBarStyleHorizontal"
                              android:layout_width="match_parent"
                              android:layout_height="20dp"
                              android:background="@null"
                              android:progress="17"
                              android:progressDrawable="@drawable/mp_bar" />
      
                          <TextView
                              android:id="@+id/mp_currently"
                              android:layout_width="wrap_content"
                              android:layout_height="wrap_content"
                              android:layout_marginLeft="2dp"
                              android:layout_marginTop="2dp"
                              android:background="@null"
                              android:text="17/100"
                              android:textColor="#212121"
                              android:textSize="13sp" />
                      </RelativeLayout>
                  </android.support.v7.widget.CardView>
              </RelativeLayout>
          </RelativeLayout>
      </RelativeLayout>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-10-24
        • 2021-10-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多