简体   繁体   English

CardView 设计在布局数据内移动时发生变化

[英]CardView design changes when moved inside layout data

I used data layout for my CardView, it was looking good until moved inside data layout for data binding.我为我的 CardView 使用了数据布局,它看起来不错,直到移动到数据布局中进行数据绑定。 Here is how it looked before layout data and data binding:这是布局数据和数据绑定之前的样子:

在此处输入图像描述

here is how it looked after data layout这是数据布局后的样子

在此处输入图像描述

here is the xml:这是 xml:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    >

    <data>
        <variable
            name="user"
            type="User" />
    </data>

    <androidx.cardview.widget.CardView
        android:usernameOnClick="@{user.username}"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        app:cardElevation="6dp"
        >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:padding="5dp">

          --- Some content for avatar view and text ---


        </LinearLayout>

    </androidx.cardview.widget.CardView>

</layout>

here is the code to use data binding这是使用数据绑定的代码

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): UserViewHolder {
        val binding = ItemUserBinding.inflate(LayoutInflater.from(parent.context))
        return UserViewHolder(binding)
    }

 class UserViewHolder(var binding: ItemUserBinding) : RecyclerView.ViewHolder(binding.root) {
        fun bind(user:User){
            binding.user=user
            }
        }

Also note that I tried to put CardView inside ConstraintLayout, it fixed the width but not the elevation另请注意,我尝试将 CardView 放在 ConstraintLayout 中,它固定了宽度但没有固定高度

I got the answer thanks to Mike M. comment.感谢Mike M.的评论,我得到了答案。 The problem is fixed by changing the inflate function call to the following通过将 inflate function 调用更改为以下内容来解决问题

val binding = ItemUserBinding.inflate(LayoutInflater.from(parent.context), parent, false)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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