2016-03-25 2 views
3

У меня есть следующий код XML в файле ресурсов, который представляет собой элемент в RecyclerView:android.support.v7.cardview: cardBackgroundColor не работает

<?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

enter image description here

Где проблема в коде? И как я могу решить эту проблему?

+0

Вы пытались использовать card_view: cardBackgroundColor = "# 9e9e9e" без android.support.v7 –

+0

Очень хорошо. Я изменил его, и у карты появился цвет. Спасибо. –

+0

Я положил ответ, вы можете принять его, если им поможет, если вы хотите, конечно :) –

ответ

1

Попробуйте удалить

android.support.v7

и установить только

card_view:cardBackgroundColor="#9e9e9e"

Надеюсь помочь!

0

Вы можете изменить этот код, как:

android.support.v7.cardview:cardBackgroundColor="@color/myColor" 

, а затем перейти к Strings.xml папку и добавьте следующую строку:

<color name="myColor">#e9e9e9</color> 
+0

да он может это изменить, но как ваш ответ разрешит его проблему ??? –

+0

Имел подобную проблему, и когда я добавил код цвета непосредственно, он не работал, использовал этот подход, и он решил мою проблему. – hTony

+0

Вы не имели в виду 'string.xml', но стили? – Vucko

3

Просто удалите атрибут 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> 

0
app:cardBackgroundColor="@color/cv_bgcolor" 

работал для меня.

Смежные вопросы