2017-07-25 46 views
0

我在使用cardview中的背景顏色(懸停效果)更改文本顏色時遇到困難。我改變了背景顏色,但沒有改變文字顏色。在cardview中更改背景和文本顏色touch32

我也試過使用選擇器,但我沒有制定出來。

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_activated="true" android:drawable="@android:color/white" /> 
    <item android:drawable="@android:color/black"/> 
</selector> 

它改變了文本的顏色和背景

enter image description here

我的佈局

enter image description here

上按我想的背景

來改變顏色的文本

enter image description here

這裏是我的LayoutCode

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:card_view="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/main" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="#f1f1f3" 
    android:orientation="vertical"> 

    <android.support.v7.widget.CardView 
     android:id="@+id/card_view" 
     android:layout_width="fill_parent" 
     android:layout_height="120dp" 
     android:layout_gravity="center" 
     android:layout_marginLeft="15dp" 
     android:layout_marginRight="15dp" 
     android:layout_marginTop="15dp" 
     card_view:cardCornerRadius="0dp" 
     card_view:contentPadding="0dp"> 

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:background="@drawable/listhover"> 

      <ImageView 
       android:id="@+id/thumbnail" 
       android:layout_width="wrap_content" 
       android:layout_height="match_parent" 
       android:layout_alignParentLeft="true" 
       android:scaleType="fitXY" 
       android:src="@drawable/cardviewimage_cio" /> 

      <TextView 
       android:id="@+id/textView" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignParentTop="true" 
       android:layout_marginLeft="12dp" 
       android:layout_marginTop="10dp" 
       android:layout_toEndOf="@+id/thumbnail" 
       android:textStyle="bold" /> 

      <TextView 
       android:id="@+id/textView2" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignStart="@+id/textView" 
       android:layout_below="@+id/textView" 
       android:layout_marginTop="5dp" 
       android:textColor="#000" /> 
     </RelativeLayout> 

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

</LinearLayout> 

回答

0

UPDATE:使用觸摸偵聽器。例如:

cardView.setOnTouchListener(new View.OnTouchListener() { 
     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      if (event.getAction() == MotionEvent.ACTION_DOWN) { 
       textView.setTextColor(Color.RED); 
       return true; 
      } else { 
       textView.setTextColor(Color.BLUE); 
      } 

      return false; 
     } 
    }); 

使用一個選擇作爲用於CardView的背景。

bg_selector.xml

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_activated="true" android:drawable="@android:color/white" /> 
    <item android:drawable="@android:color/black"/> 
</selector> 

添加這樣的:

android:background="@drawable/bg_selector" 
+0

它使文本和文本爲黑色的背景......我想只是文本來改變顏色 –

+0

我更新了我的答案。希望能幫助到你。 – sunlover3