2012-03-28 38 views
3

我正在使用以下代碼。在相對佈局上更改touchevent的背景顏色

<RelativeLayout 
      android:layout_width="310dp" 
      android:layout_height="70dp" 
      android:layout_below="@+id/UIReportView" 
      android:layout_marginLeft="4dp" 
      android:layout_marginTop="2dp" 
      android:background="@drawable/small_corners" > 

small_corners

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/UINoCorners" 
    android:shape="rectangle"> 
    <corners android:radius="10dp"/> 
    <padding android:left="10dp" 
      android:right="10dp" 
      android:top="5dp" 
      android:bottom="5dp"/> 
    <solid android:color="#FFFFFF"/> 

    </shape> 

現在,它顯示了一個白條。我想,只要我點擊這個相對佈局,它的顏色應該改變,由我設置。

report = (RelativeLayout) findViewById(R.id.UIMainReportViewTimely); 


     report .setOnClickListener(new View.OnClickListener() 
     { 
     @Override 
     public void onClick(View v) 
     { 
// My Code 
     } 
     }); 

我應該在這裏做什麼? report.onTouchEvent?

而我該如何改變背景的顏色。

問候

回答

3

您可以使用此: -

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_pressed="true"> 
     <shape> 
      <stroke android:width="1dp" android:color="#999999" /> 
      <padding android:left="10dp" android:top="3dp" android:right="10dp" 
       android:bottom="3dp" /> 
      <corners android:radius="7dp" android:bottomRightRadius="7dp" 
       android:bottomLeftRadius="7dp" android:topLeftRadius="0dp" 
       android:topRightRadius="0dp" /> 

      <gradient android:startColor="#449def" android:endColor="#2f6699" 
       android:angle="270" /> 


     </shape> 
    </item> 
<item> 
<shape> 
    <corners android:radius="10dp"/> 
    <padding android:left="10dp" 
      android:right="10dp" 
      android:top="5dp" 
      android:bottom="5dp"/> 
    <solid android:color="#FFFFFF"/> 
    </shape> 
    </item> 
</selector> 
</shape> 
4

創建新的背景另一繪製,並在setOnclickListener把

report.setBackgroundDrawable(R.drawable.newbackground_small_corners);

report.setBackgroundColor(Color.parseColor("#00000000")); 
+0

試過,我用不同的顏色,但它只是使它走到黑,不是我的願望 – 2012-03-28 10:05:42

0

我寧願建議使用onTouch()方法並檢查觸摸的座標是否在相對佈局的範圍內。如果是,那麼只需使用setBackgroundColor()設置背景。

+0

fullReport.setOnTouchListener(新View.OnTouchListener(){顏色 \t \t \t \t @覆蓋 \t \t公共布爾onTouch(視圖v,MotionEvent事件) \t \t { \t \t \t report.setBackgroundColor(R.drawable.touchbackground_corners); \t \t \t return false; \t \t \t} });這樣做,當我點擊它,它只是變黑,並調用onclickListener這也是定義。 – 2012-03-28 10:00:39