如何禁用阴影周围的卡片视图在机器人

你好,我正在工作的演示应用程序,其中我使用的卡片视图的支持库。默认情况下,它是在它周围添加阴影。我想删除这个阴影 & 应该看起来很简单。

我试过了,但对我不起作用。

CardView cardView = (CardView) v.findViewById(R.id.cardView);
cardView.setElevation(0);

做完这些,我要崩溃了

11-06 15:12:17.018: E/AndroidRuntime(24315): FATAL EXCEPTION: main
11-06 15:12:17.018: E/AndroidRuntime(24315): Process: com.xyz, PID: 24315
11-06 15:12:17.018: E/AndroidRuntime(24315): java.lang.NoSuchMethodError: android.support.v7.widget.CardView.setElevation
11-06 15:12:17.018: E/AndroidRuntime(24315):    at com.xyz.adapters.RecycleViewAdapter.onCreateViewHolder(RecycleViewAdapter.java:85)
11-06 15:12:17.018: E/AndroidRuntime(24315):    at com.xyz.adapters.RecycleViewAdapter.onCreateViewHolder(RecycleViewAdapter.java:1)
11-06 15:12:17.018: E/AndroidRuntime(24315):    at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:2915)
11-06 15:12:17.018: E/AndroidRuntime(24315):    at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:2511)
11-06 15:12:17.018: E/AndroidRuntime(24315):    at android.support.v7.widget.LinearLayoutManager$RenderState.next(LinearLayoutManager.java:1425)
11-06 15:12:17.018: E/AndroidRuntime(24315):    at android.support.v7.widget.LinearLayoutManager$RenderState.next(LinearLayoutManager.java:1425)

Xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/res/com.xyz"
android:id="@+id/cardView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:orientation="horizontal" >

先谢谢你。

91407 次浏览

try like this may help you,

CardView cardView = (CardView) v.findViewById(R.id.cardView);
cardView.setCardElevation(0);

Try to put the elevation in Xml.

app:cardElevation="0dp"

OR

cardView.setCardElevation(0);

And check you are using the latest CardView library.

CardView sets its own elevation during initialization, which will override whatever you set from XML. You should file this as a bug at chek this

@Override
public void initialize(CardViewDelegate cardView, Context context, int backgroundColor,
float radius) {
cardView.setBackgroundDrawable(new RoundRectDrawable(backgroundColor, radius));
View view = (View) cardView;
view.setClipToOutline(true);
view.setElevation(context.getResources().getDimension(R.dimen.cardview_elevation));
}

use this attribute in XML

card_view:cardElevation="0dp"

and remember add xmlns:card_view="http://schemas.android.com/tools" in your root layout.

OR you can call cardView.setCardElevation(0) to disable shadow programmatically.

cardView.setElevation() method and CardView attribute android:elevation will throw java.lang.NoSuchMethodError in platform before Android 5.0

You should first add this to the your parent layout

xmlns:card_view="http://schemas.android.com/tools"

then set the elevation like this

card_view:cardElevation="0dp"

 <android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:cardElevation="0dp"
app:cardCornerRadius="2dp">
....`
</android.support.v7.widget.CardView>

You can have it in XML like:

<android.support.v7.widget.CardView
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
app:cardElevation="0dp"
app:cardCornerRadius="0.5dp"
app:cardPreventCornerOverlap="false"
app:cardUseCompatPadding="true"
>
</android.support.v7.widget.CardView>

hope it helps you !!!

You have to use the following attributes

<android.support.v7.widget.CardView
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardElevation="0dp"
app:cardCornerRadius="0.5dp"
app:cardPreventCornerOverlap="false"
>
</android.support.v7.widget.CardView>

Just put this line inside your CardView:

app:cardElevation="0dp"

Hope it will help you.

In my case, only setting of background alpha with suggested elevation and backgroundColor hide shadow border:

 this.setCardElevation(0);
this.setCardBackgroundColor(getContext().getResources().getColor(android.R.color.transparent));
this.getBackground().setAlpha(0);

use app:cardElevation="0dp", don't use app:elevation="0dp"

If anyone is looking for kotlin answer then this worked for me

card_layout.cardElevation = 0F

Attenion: Attribute outlineSpotShadowColor is only used in API level 28 and higher

app:cardElevation="0dp"
android:outlineSpotShadowColor="@android:color/transparent"