代码之家  ›  专栏  ›  技术社区  ›  kostik

android->将ImageView的高度设置为父级的动态高度

  •  0
  • kostik  · 技术社区  · 4 年前

    单元格的高度取决于文本的长度。

    ImageView的高度必须与父视图的高度相同。 令人惊讶的是,要完成这项任务真的很难。

    这是没有ImageView的结果布局 enter image description here

    正如您所看到的,单元格的高度取决于文本的长度。

    enter image description here

    <?xml version="1.0" encoding="utf-8"?>
    
    <androidx.cardview.widget.CardView
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/cardview"
        app:cardBackgroundColor="#FF6B6B"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginRight="16dp"
        android:layout_marginLeft="16dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:cardCornerRadius="8dp">
    
        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <ImageView
                android:src="@drawable/universe1"
                android:id="@+id/background"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:scaleType="centerCrop">
            </ImageView>
            <LinearLayout
                android:id="@+id/layout_text"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">
                <TextView
                    android:layout_marginLeft="8dp"
                    android:textColorLink="@color/white"
                    android:layout_marginBottom="8dp"
                    android:id="@+id/text"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="2dp"
                    android:ellipsize="end"
                    android:fontFamily="@font/compact_text_regular"
                    android:maxLines="4"
                    android:text="This sounded nonsense to Alice, so she said nothing, but set off at her being blah blah This sounded nonsense to Alice, so she said nothing, but set off at her being blah blah"
                    android:textColor="@color/white"
                    android:textSize="14sp"
                    android:lineSpacingExtra="4dp" />
    
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">
    
                    <LinearLayout
                        android:visibility="gone"
                        android:id="@+id/like_icon_wrap"
                        android:clickable="true"
                        android:layout_gravity="center"
                        android:layout_width="40dp"
                        android:layout_height="30dp"
                        android:orientation="horizontal">
    
                        <ImageView
                            android:layout_marginLeft="8dp"
                            android:id="@+id/like_icon"
                            android:layout_gravity="center_vertical"
                            android:layout_width="16dp"
                            android:layout_height="16dp"
                            app:srcCompat="@drawable/heart_white" />
    
                        <TextView
                            android:id="@+id/likes_count"
                            android:layout_gravity="center"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:fontFamily="@font/compact_text_regular"
                            android:textColor="@color/white"
                            android:textSize="16sp"
                            android:text="0" />
                    </LinearLayout>
    
                    <TextView
                        android:layout_marginRight="8dp"
                        android:layout_gravity="center"
                        android:id="@+id/more"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:fontFamily="sans-serif"
                        android:textColor="@color/colorPrimary"
                        android:gravity="right"
                        android:text="show more"
                        android:textSize="12sp"
                        android:visibility="invisible" />
                </LinearLayout>
            </LinearLayout>
        </FrameLayout>
    </androidx.cardview.widget.CardView>
    

    我还试图从recyclerview适配器设置图像的高度,但没有成功。

    1 回复  |  直到 4 年前
        1
  •  2
  •   Mohammed Alaa    4 年前

    ConstraintLayout 通过将高度设置为,可以匹配其他视图的高度 0dp

    我删掉了布局的其他部分,所以您必须添加它们,但下面是图像和文本的代码。 Produce this

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/cardview"
        app:cardBackgroundColor="#FF6B6B"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginRight="16dp"
        android:layout_marginLeft="16dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:cardCornerRadius="8dp">
    
        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <ImageView
                android:id="@+id/background"
                android:layout_width="0dp"
                android:layout_height="0dp"
    
                tools:src="@tools:sample/avatars"
                android:scaleType="centerCrop"
    
                app:layout_constraintStart_toStartOf="@id/text"
                app:layout_constraintEnd_toEndOf="@id/text"
                app:layout_constraintTop_toTopOf="@id/text"
                app:layout_constraintBottom_toBottomOf="@id/text" />
    
            <TextView
                android:id="@+id/text"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
    
                android:ellipsize="end"
                android:lineSpacingExtra="4dp"
                android:maxLines="4"
                android:text="This sounded nonsense to Alice, so she said nothing, but set off at her being blah blah This sounded nonsense to Alice, so she said nothing, but set off at her being blah blah"
                android:textSize="14sp"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
        </androidx.constraintlayout.widget.ConstraintLayout>
    </androidx.cardview.widget.CardView>
    
        2
  •  0
  •   NehaK    4 年前

    正确的方法是使用@Hkon-Schia代码。 但如果您想使用FrameLayout,请像这样在RelativeLayout中添加,并使用“alignBottom”约束在其中添加ImageView:

    <?xml version="1.0" encoding="utf-8"?>
    
    <androidx.cardview.widget.CardView
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/cardview"
        app:cardBackgroundColor="#FF6B6B"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginRight="16dp"
        android:layout_marginLeft="16dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:cardCornerRadius="8dp">
    
        <!--
            Code added here...
        -->
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
        <ImageView
            android:id="@+id/background"
            android:background="@drawable/universe1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/frameLayout"
            android:scaleType="centerCrop">
        </ImageView>
    
        <FrameLayout
            android:id="@+id/frameLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <!--<ImageView
                android:src="@drawable/universe1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:scaleType="centerCrop">
            </ImageView>-->
            <LinearLayout
                android:id="@+id/layout_text"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">
                <TextView
                    android:layout_marginLeft="8dp"
                    android:textColorLink="@color/white"
                    android:layout_marginBottom="8dp"
                    android:id="@+id/text"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="2dp"
                    android:ellipsize="end"
                    android:fontFamily="@font/compact_text_regular"
                    android:maxLines="4"
                    android:text="This sounded nonsense to Alice, so she said nothing, but set off at her being blah blah This sounded nonsense to Alice, so she said nothing, but set off at her being blah blah"
                    android:textColor="@color/white"
                    android:textSize="14sp"
                    android:lineSpacingExtra="4dp" />
    
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">
    
                    <LinearLayout
                        android:visibility="gone"
                        android:id="@+id/like_icon_wrap"
                        android:clickable="true"
                        android:layout_gravity="center"
                        android:layout_width="40dp"
                        android:layout_height="30dp"
                        android:orientation="horizontal">
    
                        <ImageView
                            android:layout_marginLeft="8dp"
                            android:id="@+id/like_icon"
                            android:layout_gravity="center_vertical"
                            android:layout_width="16dp"
                            android:layout_height="16dp"
                            app:srcCompat="@drawable/heart_white" />
    
                        <TextView
                            android:id="@+id/likes_count"
                            android:layout_gravity="center"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:fontFamily="@font/compact_text_regular"
                            android:textColor="@color/white"
                            android:textSize="16sp"
                            android:text="0" />
                    </LinearLayout>
    
                    <TextView
                        android:layout_marginRight="8dp"
                        android:layout_gravity="center"
                        android:id="@+id/more"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:fontFamily="sans-serif"
                        android:textColor="@color/colorPrimary"
                        android:gravity="right"
                        android:text="show more"
                        android:textSize="12sp"
                        android:visibility="invisible" />
                </LinearLayout>
            </LinearLayout>
        </FrameLayout>
        </RelativeLayout>
    </androidx.cardview.widget.CardView>