android.support.v7.cardview:cardBackgroundColor 不起作用
android.support.v7.cardview:cardBackgroundColor doesn't work
我在资源文件中有以下 XML 代码,它表示 RecyclerView 中的一个项目:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:android.support.v7.cardview="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android.support.v7.cardview:cardBackgroundColor="#9e9e9e"
android.support.v7.cardview:cardElevation="7dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent"
android:orientation="vertical">
<ImageView
android:id="@+id/type"
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_gravity="end" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/field_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="2dp"
android:textColor="#000000"
android:textSize="20sp"
android:typeface="monospace" />
<LinearLayout
android:layout_width="15dp"
android:layout_height="match_parent" />
</LinearLayout>
<LinearLayout
android:id="@+id/main_recyclerview_item_actions"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="end"
android:visibility="gone">
<Button
android:id="@+id/delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/delete"
android:textColor="@color/colorAccent"
style="@style/Widget.AppCompat.Button.Borderless"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
问题是 CardView 永远无法获得颜色:9e9e9e,如下图所示:
颜色是白色不是9e9e9e
代码哪里出了问题?我该如何解决这个问题?
您可以像这样修改此代码:
android.support.v7.cardview:cardBackgroundColor="@color/myColor"
然后转到 Strings.xml
文件夹并添加此行:
<color name="myColor">#e9e9e9</color>
尝试移除
android.support.v7
并仅设置
card_view:cardBackgroundColor="#9e9e9e"
希望能帮到你!
只需删除 cardBackgroundColor 属性并将 android:background="#9e9e9e"
设置为布局中的第一个 LinearLayout
。我的意思是:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:android.support.v7.cardview="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android.support.v7.cardview:cardElevation="7dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#9e9e9e"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent"
android:orientation="vertical">
<ImageView
android:id="@+id/type"
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_gravity="end" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/field_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="2dp"
android:textColor="#000000"
android:textSize="20sp"
android:typeface="monospace" />
<LinearLayout
android:layout_width="15dp"
android:layout_height="match_parent" />
</LinearLayout>
<LinearLayout
android:id="@+id/main_recyclerview_item_actions"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="end"
android:visibility="gone">
<Button
android:id="@+id/delete"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DELETE"
android:textColor="@color/colorAccent" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
app:cardBackgroundColor="@color/cv_bgcolor"
对我有用。
我在资源文件中有以下 XML 代码,它表示 RecyclerView 中的一个项目:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:android.support.v7.cardview="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android.support.v7.cardview:cardBackgroundColor="#9e9e9e"
android.support.v7.cardview:cardElevation="7dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent"
android:orientation="vertical">
<ImageView
android:id="@+id/type"
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_gravity="end" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/field_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="2dp"
android:textColor="#000000"
android:textSize="20sp"
android:typeface="monospace" />
<LinearLayout
android:layout_width="15dp"
android:layout_height="match_parent" />
</LinearLayout>
<LinearLayout
android:id="@+id/main_recyclerview_item_actions"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="end"
android:visibility="gone">
<Button
android:id="@+id/delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/delete"
android:textColor="@color/colorAccent"
style="@style/Widget.AppCompat.Button.Borderless"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
问题是 CardView 永远无法获得颜色:9e9e9e,如下图所示: 颜色是白色不是9e9e9e
代码哪里出了问题?我该如何解决这个问题?
您可以像这样修改此代码:
android.support.v7.cardview:cardBackgroundColor="@color/myColor"
然后转到 Strings.xml
文件夹并添加此行:
<color name="myColor">#e9e9e9</color>
尝试移除
android.support.v7
并仅设置
card_view:cardBackgroundColor="#9e9e9e"
希望能帮到你!
只需删除 cardBackgroundColor 属性并将 android:background="#9e9e9e"
设置为布局中的第一个 LinearLayout
。我的意思是:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:android.support.v7.cardview="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android.support.v7.cardview:cardElevation="7dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#9e9e9e"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent"
android:orientation="vertical">
<ImageView
android:id="@+id/type"
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_gravity="end" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/field_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="2dp"
android:textColor="#000000"
android:textSize="20sp"
android:typeface="monospace" />
<LinearLayout
android:layout_width="15dp"
android:layout_height="match_parent" />
</LinearLayout>
<LinearLayout
android:id="@+id/main_recyclerview_item_actions"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="end"
android:visibility="gone">
<Button
android:id="@+id/delete"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DELETE"
android:textColor="@color/colorAccent" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
app:cardBackgroundColor="@color/cv_bgcolor"
对我有用。