扩展cardview类显示卡周围的白色矩形

时间:2015-07-05 20:48:17

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

我正在扩展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);
...
}
...
}

这是结果

enter image description here

任何人都可以帮助我吗?

2 个答案:

答案 0 :(得分:0)

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

尝试将android:foreground设置为不带alpha的内容。

答案 1 :(得分:0)

对于我来说,我将白色背景设置为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">