无法为Recycler View项添加阴影和边框

4
我已经创建了一个“回收视图”,并添加了一个“偏移项装饰器”,并将海拔属性添加到列表项中,但它不起作用。请帮忙!! 这就是我想要的

enter image description here

我有什么

enter image description here

我的偏移项装饰器代码

public class ItemOffsetDecoration extends RecyclerView.ItemDecoration {

private int mItemOffset;

private ItemOffsetDecoration(int itemOffset) {

    mItemOffset = itemOffset;

}

public ItemOffsetDecoration(@NonNull Context context, @DimenRes int itemOffsetId) {

    this(context.getResources().getDimensionPixelSize(itemOffsetId));

}

@Override

public void getItemOffsets(Rect outRect, View view, RecyclerView parent,

                           RecyclerView.State state) {

    super.getItemOffsets(outRect, view, parent, state);

    outRect.set(mItemOffset, mItemOffset, mItemOffset, mItemOffset);

  }

}

这是我添加了“卡片视图”并使用“elevation”属性后的列表项。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:longClickable="true"
android:orientation="vertical"
android:paddingBottom="10dp"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/row_padding_vertical">

<android.support.v7.widget.CardView
    android:id="@+id/transaction_cardView"
    android:layout_width="match_parent"
    android:layout_height="80dp"
    android:elevation="20dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:layout_marginBottom="0dp">

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/txtHour"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_marginEnd="8dp"
            android:layout_marginTop="2dp"
            android:text="5:10"
            android:textAlignment="viewEnd"
            android:textColor="@color/transaction_text"
            android:textSize="16sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/txtDate" />

        <TextView
            android:id="@+id/txtDescription"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignEnd="@+id/txtAmount"
            android:layout_alignParentTop="true"
            android:ellipsize="end"
            android:lines="1"
            android:text="TextView"
            android:textColor="@color/transaction_text"
            android:textSize="25sp"
            app:layout_constraintTop_toBottomOf="@+id/txtAmount" />

        <TextView
            android:id="@+id/txtAmount"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/txtDescription"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_marginTop="8dp"
            android:lines="1"
            android:text="TextView"
            android:textColor="@color/transaction_text"
            android:textSize="26sp"
            android:textStyle="bold"
            android:typeface="sans"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/txtDate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_marginEnd="8dp"
            android:layout_marginTop="8dp"
            android:text="07/01/2"
            android:textAlignment="textEnd"
            android:textColor="@color/transaction_text"
            android:textSize="16sp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </android.support.constraint.ConstraintLayout>

</android.support.v7.widget.CardView>


</RelativeLayout>

我认为这段代码无法正常工作是因为没有设置边界。任何帮助都将不胜感激!

使用CardView可以实现这种效果,因为CardView还提供了高程和所需的表面区分。 - Awais
你可以分享一下布局文件吗?你需要一些特定的配置和CardView才能实现效果,如果你发布它,我可以帮助你。 - Jhon Fredy Trujillo Ortega
在xml中设置CardView_cardElevation以获取更多的CardView属性,请参考cardview_doc - Uttam Panchasara
4个回答

9

在recyclerView的item_file中尝试将cardView添加为您的主布局,您可以设置elevation属性。

以下是相关代码:

<android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:cardBackgroundColor="@color/white"
        app:cardCornerRadius="4dp"
        app:cardElevation="5dp"
        app:cardUseCompatPadding="true">

<YOUR LAYOUT>
</android.support.v7.widget.CardView>

4
请将以下行添加到父RelativeLayout中。
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:clipToPadding="false"
android:clipChildren="false"

在 CardView 中加入以下代码行。
card_view:cardElevation="10dp"

2

更新你的 Cardview 布局,来自:

<android.support.v7.widget.CardView
    android:id="@+id/transaction_cardView"
    android:layout_width="match_parent"
    android:layout_height="80dp"
    android:elevation="20dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:layout_marginBottom="0dp">

为了

<android.support.v7.widget.CardView
    android:id="@+id/transaction_cardView"
    android:layout_width="match_parent"
    android:layout_height="80dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:layout_marginBottom="0dp"
    app:cardElevation="10dp"
    app:contentPadding="2dp"
    app:cardUseCompatPadding="true"
    >

如果在您的清单中有以下行,则阴影将不会显示。

<application
     android:hardwareAccelerated="false"

因此,请确保它设置为true,或者不存在


0

将父级布局标签更改为

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

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接