【问题标题】:Why my Cardview doesn't appear like in layout when i run it为什么我的 Cardview 在我运行时没有出现在布局中
【发布时间】:2020-11-07 17:22:46
【问题描述】:

您好,我在我的应用程序中使用 Cardview 来表示项目,在代码布局下方,我在回收站视图中使用它,但它与布局不同。卡片视图完全是白色的,没有圆角半径,有人知道如何帮助我吗?

当我转到设计选项卡时它看起来很好,但是当我在设备上运行时它看起来不同。它没有圆角半径或颜色只有边距我该如何解决这个问题?

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:layout_constraintCircleRadius="1dp"
android:id="@+id/cardview"
android:padding="16dp"
app:cardCornerRadius="32dp"
app:cardBackgroundColor="@color/pink"
app:cardElevation="5dp"
android:layout_margin="16dp">
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">


    <de.hdodenhof.circleimageview.CircleImageView
        android:id="@+id/cardImage"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:padding="8dp"
        android:layout_centerHorizontal="true"
        app:civ_border_color="#FF000000"
        app:civ_border_width="2dp"/>

    <TextView
        android:id="@+id/cardName"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/cardImage"
        android:layout_centerHorizontal="true"
        android:textAlignment="center"
        android:textColor="#673AB7"
        android:textSize="17sp" />

    <TextView
        android:id="@+id/cardBodySize"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/cardName"
        android:layout_centerHorizontal="true"
        android:paddingBottom="8dp"
        android:textAlignment="center"
        android:textColor="#2C2C2C"
        android:textSize="15sp" />
</RelativeLayout>


</androidx.cardview.widget.CardView>

RecyclerView

public class ClothesRecycler extends RecyclerView.Adapter<ClothesViewHolder> implements Filterable {
List<Clothes> data;
List<Clothes> filteredData;
private OnItemClickListener mListener;
private OnItemLongClickListener lListener;

public void setOnItemClickListener(OnItemClickListener listener){
    mListener=listener;
}
public void setOnItemLongClickListener(OnItemLongClickListener listener){
    lListener =listener;
}


public interface OnItemClickListener{
    void onItemClick(int position);
}
public interface OnItemLongClickListener{
    void onItemLongClick(int position);
}


public ClothesRecycler(List<Clothes> clothes){
    data = clothes;
    filteredData = clothes;
}
@NonNull
@Override
public ClothesViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.clothes_item_layout,parent,false);

    return new ClothesViewHolder(view,mListener,lListener);
}

@Override
public void onBindViewHolder(@NonNull final ClothesViewHolder holder, int position) {
        final Clothes clothes= data.get(position);
        holder.imageView.setImageBitmap(DataConverter.convertByteArray2Image(clothes.getImage()));
        holder.name.setText(clothes.getName());
        holder.bodySize.setText(clothes.getBodySize());
        holder.itemView.setBackgroundColor(clothes.isSelected() ? Color.LTGRAY : Color.WHITE);

}

@Override
public int getItemCount() {
    return data.size();
}

【问题讨论】:

  • 贴出RecyclerView.Adapter的代码
  • @GabrieleMariotti 我编辑帖子
  • 适配器的代码不是RecyclerView的布局
  • @GabrieleMariotti 我已添加到代码中
  • 图片是否覆盖了整个卡片视图?

标签: android xml android-layout android-recyclerview android-cardview


【解决方案1】:
  • 检查这个 xml 文件
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    app:layout_constraintCircleRadius="1dp"
    android:id="@+id/cardview"
    android:padding="16dp"
    app:cardCornerRadius="32dp"
    app:cardBackgroundColor="@android:color/holo_purple"
    app:cardElevation="5dp"
    android:layout_margin="16dp">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">


        <ImageView
            android:id="@+id/cardImage"
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:padding="8dp"
            android:background="@color/colorPrimaryDark"
            android:layout_centerHorizontal="true"
            />

        <TextView
            android:id="@+id/cardName"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:layout_below="@+id/cardImage"
            android:layout_centerHorizontal="true"
            android:textAlignment="center"
            android:textColor="#673AB7"
            android:textSize="17sp" />

        <TextView
            android:id="@+id/cardBodySize"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:layout_below="@+id/cardName"
            android:layout_centerHorizontal="true"
            android:paddingBottom="8dp"
            android:textAlignment="center"
            android:textColor="#2C2C2C"
            android:textSize="15sp" />
    </RelativeLayout>


</androidx.cardview.widget.CardView>

【讨论】:

  • 我实际上在我的项目中尝试了完全相同的代码并且它显示正确,您是否在其他一些屏幕尺寸不同的设备上尝试过?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多