【问题标题】:CardView size not dynamic when set to wrap_content设置为 wrap_content 时,CardView 大小不是动态的
【发布时间】:2020-08-31 16:31:36
【问题描述】:

我有一个用于 RecyclerView 项目的 CardView,其中包含一个 ImageView 和一个 LinearLayout (actionBar)

<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/communityImageCv"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:layout_marginStart="16dp"
    android:layout_marginEnd="16dp"
    android:layout_marginTop="15dp"
    android:layout_marginBottom="15dp"
    app:cardCornerRadius="30dp"
    android:elevation="16dp">


    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/communityImageCl"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <androidx.appcompat.widget.AppCompatImageView
            android:id="@+id/communityImageIv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:background="@color/transparent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintBottom_toTopOf="@+id/actionBar"
            tools:src="@drawable/person_splash_2" />


        <LinearLayout
            android:id="@+id/actionBar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:gravity="center"
            android:background="@color/white"
            android:orientation="horizontal"
            android:paddingTop="20dp"
            android:paddingBottom="20dp"
            android:visibility="gone"
            android:weightSum="3"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            tools:visibility="visible">


            <LinearLayout
                android:id="@+id/loveButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center"
                android:orientation="vertical"

                >

                <ImageView
                    android:id="@+id/lovedIcon"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="3dp"
                    android:src="@drawable/loved_heart_empty">

                </ImageView>

                <TextView
                    android:id="@+id/lovedText"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:fontFamily="@font/radikal"
                    android:textColor="@color/black"
                    android:textSize="10sp"
                    tools:text="@string/love_this">

                </TextView>

            </LinearLayout>

            <LinearLayout
                android:id="@+id/shareButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center"
                android:orientation="vertical"
                android:visibility="gone"
                tools:visibility="visible"

                >

                <ImageView
                    android:id="@+id/shareIcon"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="3dp"
                    android:src="@drawable/share_arrow_empty">

                </ImageView>

                <TextView
                    android:id="@+id/sharedText"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:fontFamily="@font/radikal"
                    android:textColor="@color/black"
                    android:textSize="10sp"
                    tools:text="@string/shared_this">

                </TextView>

            </LinearLayout>

        </LinearLayout>
        

    </androidx.constraintlayout.widget.ConstraintLayout>


</androidx.cardview.widget.CardView>

LinearLayout 被编程为根据模型的属性出现和消失。之后,ImageView 的高度会根据另一个属性以编程方式更改(我只包括这部分以防万一):

   fun bind(model: CommunityOffer) {

 if (model.isLikeable || model.isShareable) {
            actionBar.visibility = View.VISIBLE

        } else{
            actionBar.visibility = GONE

        }


        if (!model.mediaAspectRatio.isNullOrEmpty()) {

            val aspectRatioSplit = model.mediaAspectRatio.split(":")
            widthRatio = Integer.parseInt(aspectRatioSplit[0])
            heightRatio = Integer.parseInt(aspectRatioSplit[1])

            communityImageIv.scaleType = ImageView.ScaleType.FIT_XY

            height = ((width.toFloat() / widthRatio.toFloat()) * heightRatio).toInt()

            communityImageIv.layoutParams.height = height
            communityImageCl.layoutParams.height = height

            Glide.with(itemView.context).load(model.coverUrl).apply(RequestOptions().override(width, height).skipMemoryCache(true)).listener(requestListener).into(communityImageIv)

        } else {

            communityImageIv.scaleType = ImageView.ScaleType.CENTER_CROP
            widthRatio = 25
            heightRatio = 21

            height = ((width.toFloat() / widthRatio.toFloat()) * heightRatio).toInt()

            communityImageIv.layoutParams.height = height
            communityImageCl.layoutParams.height = height

            Interactors.glide.loadImage(model.coverUrl, communityImageIv, requestListener)
        }

         

CardView 并没有增加大小来容纳它的两个子视图,而是按照 actionBar 的大小按比例向上移动 ImageView,切断 ImageView 的顶部:

没有actionBar:

带有动作栏:

这看起来应该在 .xml 文件的显示面板中,但不是在运行时。如何使 CardView 在运行时适当地包装其子项?

【问题讨论】:

    标签: android xml android-cardview


    【解决方案1】:

    我认为您应该尝试将约束布局的线性布局设置为 cardView 的子布局,而不是将所有子小部件设置为 linearLayout 我希望它可以工作。

    喜欢这个

    <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/communityImageCv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="16dp"
        android:layout_marginTop="15dp"
        android:layout_marginBottom="15dp"
        app:cardCornerRadius="30dp"
        android:elevation="16dp">
    
    
     <LinearLayout
            android:id="@+id/communityImageCl"
            android:layout_width="match_parent"
            android:orientation="vertical"
            android:layout_height="wrap_content">
    
               //set here your all child widgets 
    
    </LinearLayout>
    
    </androidx.cardview.widget.CardView>
    

    【讨论】:

    • 我已经尝试过了,这实际上使得 actionBar 根本不会出现,即使它应该出现。据推测,它在底部被切断了。问题似乎在于卡片视图僵化。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-02
    • 1970-01-01
    相关资源
    最近更新 更多