2012-03-28 93 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