【问题标题】:extending cardview class show white rectangle around the card扩展 cardview 类在卡片周围显示白色矩形
【发布时间】:2015-07-05 20:48:17
【问题描述】:

我正在扩展 CardView 类,因为我想为列表视图的每一行创建自定义视图。 这是我的 xml 布局

<com.mojiapps.myquran.items.TranslationDownloadItemView  xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:foreground="?selectableItemBackground"
android:gravity="right"
card_view:cardCornerRadius="3dp"
card_view:cardElevation="3dp"
card_view:cardPreventCornerOverlap="false">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
...

</com.mojiapps.myquran.items.TranslationDownloadItemView>

这是我的java类

public class TranslationDownloadItemView extends CardView {


public TranslationDownloadItemView(Context context) {
    super(context);
    init();
}

public TranslationDownloadItemView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

private void init() {
    LayoutInflater inflater = LayoutInflater.from(getContext());

    inflater.inflate(R.layout.translation_type_row, this);
...
}
...
}

这就是结果

谁能帮帮我?

【问题讨论】:

  • 如果您有自定义视图,为什么还要使用 CardView?只需将布局传递给 RecyclerView Adapter onCreateViewHolder
  • 我不使用 RecyclerView,如果我不使用 cardview,我应该创建一个可绘制的选择器以具有 cardview 样式

标签: java android android-custom-view android-cardview


【解决方案1】:

我在将 CardView 背景设置为透明时遇到了这个问题。我解决了它设置非 alpha 颜色。

尝试将您的android:foreground 设置为没有字母的东西。

【讨论】:

    【解决方案2】:

    在我的情况下,我通过将白色背景设置为 CardView 来解决它

    <androidx.cardview.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:cardBackgroundColor="@color/white">
    

    或者您可以将 cardElevation 设置为 0dp,但这会禁用阴影

    <androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:cardBackgroundColor="@color/trans_black_darker"
        app:cardCornerRadius="10dp"
        app:cardPreventCornerOverlap="false"
        app:cardUseCompatPadding="true"
        app:cardElevation="0dp">
    

    【讨论】:

      猜你喜欢
      • 2015-06-06
      • 2017-12-11
      • 2018-01-11
      • 1970-01-01
      • 2013-09-24
      • 2013-10-13
      • 1970-01-01
      • 2012-05-08
      相关资源
      最近更新 更多