android 扩展cardview类在卡片周围显示白色矩形

rjzwgtxy  于 2023-05-27  发布在  Android
关注(0)|答案(3)|浏览(193)

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

这就是结果

有人能帮我吗?

nhaq1z21

nhaq1z211#

我有这个问题设置CardView背景透明的东西。我解决了它设置一个非alpha颜色。
尝试将android:foreground设置为不带alpha的值。

r7knjye2

r7knjye22#

在我的例子中,我通过将白色背景设置为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">
icomxhvb

icomxhvb3#

我知道这是一个老问题,但我想指出一个更简单的方法来膨胀一个孩子的观点(所以经常被忽视)。
而不是:

LayoutInflater inflater = LayoutInflater.from(getContext());
inflater.inflate(R.layout.translation_type_row, this);

试试这个:

inflate(getContext(), R.layout.translation_type_row, this);

使用静态View**inflate()**是最干净、最有效的方法;你不需要LayoutInflater。

相关问题