2016-12-15 67 views
0

我需要在用戶單擊我的框架時添加視覺反饋。我遵循類似問題的一些建議,但我無法得到它的工作,我不知道爲什麼它不工作。這裏是我迄今所做的:單擊RelativeLayout時的視覺反饋

rectangle_visual_feedback.xml:

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

rectangle_visual_feedback_selector.xml:

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> 
<solid android:color="#40808080"/> 
<padding android:bottom="10dp" android:right="10dp" android:top="10dp" android:left="10dp"></padding> 
</shape> 

fragment.xml之:

<LinearLayout 
     android:id="@+id/linearlayout_holder" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:background="?android:attr/selectableItemBackground" 
     android:clickable="true" 
     android:paddingBottom="3dp" 
     android:paddingLeft="10dp" 
     android:paddingRight="3dp" 
     android:paddingTop="10dp"> 

     <RelativeLayout 
      android:id="@+id/relativelayout_mainwrap" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:padding="8dp" 
      android:background="@drawable/rectangle_visual_feedback_selector" 
      > 

      <LinearLayout 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignParentLeft="true" 
       android:layout_centerVertical="true" 
       android:gravity="center" 
       android:orientation="horizontal"> 

       <ImageView 
        android:id="@+id/imageview_image" 
        android:layout_width="22dp" 
        android:layout_height="22dp" 
        android:src="@drawable/thehotel_b" /> 

       <com.hotelstayapp.android.custom.CustomTextView 
        android:id="@+id/customtextview_text" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_marginLeft="5dp" 
        android:gravity="center_vertical" 
        android:textAllCaps="true" 
        android:textSize="16sp" /> 
      </LinearLayout> 

     </RelativeLayout> 
    </LinearLayout> 

這是裏面的代碼我用來調用RelativeLayout的片段:

RelativeLayout relativelayout_mainwrap = (RelativeLayout) view.findViewById(R.id.relativelayout_mainwrap); 
RelativeLayout relativelayout_mainwrap.setBackgroundColor(Color.parseColor("#" + Theme.BackColorOpacity + Theme.BackColor)); 

任何想法爲什麼這不起作用?

+0

你已經使用'android:background'在XML中給它一個背景。爲什麼你在java中設置另一個背景顏色? – Karakuri

+0

嘗試從父級佈局中移除android:clickable =「true」 – student

回答

0

視覺反饋,您可以創建以下繪製,

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:color="@color/colorAccent" android:state_pressed="true" /> 
    <item android:color="@color/navigation_icon_default" /> 
</selector> 

並將此繪製你的相對佈局的背景。