0

如何放置两个可绘制项目,一个关联单击查看其他未单击状态!在Java代码中!因为在我的程序中,我动态创建了新视图,其中哪一个在默认状态下具有不同的颜色(当 android:state_pressed="false" 时)。

完全一样:但在java代码中:

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- PRESSED -->
<item android:state_pressed="true"> 
    <shape android:shape="rectangle">
           <stroke android:color="#286DCC" android:width="2dp"/>
           <corners android:bottomLeftRadius="5dp" android:bottomRightRadius="10dp" android:topLeftRadius="10dp" android:topRightRadius="10dp" />

                <!--  Gradient Bg for listrow -->
           <gradient  android:startColor="#f1f1f2" android:centerColor="#e7e7e8" 
                      android:endColor="#cfcfcf" android:angle="270" />
     </shape>
 </item>

        <!-- DEFAULT -->
<item >
     <shape android:shape="rectangle" >
           <corners android:bottomLeftRadius="10dp" android:bottomRightRadius="10dp" 
                   android:topLeftRadius="10dp"  android:topRightRadius="10dp" />
                <!--  Gradient Bg for listrow -->
           <gradient  **android:startColor="#F75522" android:centerColor="#F75522"**
                    **android:endColor="#F75522"** android:angle="270" />
     </shape>
</item>

</selector>

我试图这样修改它:

Drawable shape = getResources(). getDrawable(R.drawable.bg_defaults);

我不知道如何实现android:startColor, centerColor,endColor来动态改变它。

为所有人考虑。

4

1 回答 1

0

将 onClickListener 设置为按钮并在 onClick 中更改按钮的背景

public void onClick(View v) {
   if(v == ButtonName) {
     ButtonName.setImageResource(R.drawable.ImageName);
   }
}
于 2013-05-21T15:58:17.423 回答