CardView: How do I add a gradient background while maintaining the radius

感情迁移 提交于 2020-05-13 04:21:26

问题


I want to re-create the image below with a CardView. To achieve this, I created a gradient file (btn_gradient.xml) and then proceeded to create the CardView.

CardView implementation:

<android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="44dp"
        android:layout_margin="25dp"
        app:cardElevation="0dp"
        app:cardCornerRadius="4dp"
        app:cardPreventCornerOverlap="false">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:background="@drawable/btn_gradient"
            android:text="Create Account"
            android:textColor="#000000"
            android:textStyle="bold"
            android:textAllCaps="false"/>


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

Everything works fine this way except that the radius disappears and this is not what I want. Is there a way I can set the gradient directly on the CardView? The cardBackgroundColor attribute accepts only colors, not drawables.

Any help would be appreciated.



Addendum:
As requested, this is my btn_gradient.xml file:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
    android:type="linear"
    android:angle="0"
    android:startColor="#ffc200"
    android:endColor="#fca10b" />
</shape>

回答1:


If I may ask, are you per-chance testing/running on a pre-lollipop Android device? Your code seems to work as you desire (curved corners showing with the gradient) except on Android 4.

To achieve the desired result on pre-lollipop devices, you can add <corners android:radius="4dp" /> to your @drawable/btn_gradient file, (you would have to set the corner radius to match the CardView's cardCornerRadius.




回答2:


  <androidx.cardview.widget.CardView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_margin="@dimen/_10sdp"
                    app:cardCornerRadius="@dimen/_10sdp"
                    app:cardElevation="@dimen/_1sdp">


                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="vertical"
                        android:background="@drawable/gradient_16"
                        android:padding="@dimen/_6sdp">



                    </LinearLayout>


                </androidx.cardview.widget.CardView>

and gradient be like

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
    android:startColor="#5B86E5"
    android:endColor="#36D1DC"
    android:angle="180" />

<corners
    android:radius="10dp">
</corners>

CardView card_radius and gradient radius should be same dimentions




回答3:


Move this line

android:background="@drawable/btn_gradient"

To the CardView object

UPDATE

My bad:

Inside the place a layout to wrap the content of the .

In this case, I'd go with a <FrameLayout> like this:

<androidx.cardview.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <FrameLyout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/btn_gradient">



回答4:


Do not use android:background attribute in XML file. use app:cardBackground instead.

To wrap it up, first create a gradient background XML file in drawables. then assign it to the app:cardBackgroundColor like this:

app:cardBackgroundColor="@drawable/gradient_background"

if you don't know how to create the gradient_background.xml, write click on drawables directory, create new xml file and paste the code below.

<?xml version="1.0" encoding="utf-8"?>
<shape android:shape="rectangle"  
xmlns:android="http://schemas.android.com/apk/res/android">
<gradient android:startColor="@color/secondaryColor"
    android:endColor="@color/primaryColor"
    android:angle="90"/>
</shape>


来源:https://stackoverflow.com/questions/55714392/cardview-how-do-i-add-a-gradient-background-while-maintaining-the-radius

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!