【问题标题】:Setting Background Drawable with states to android buttons将带有状态的背景可绘制对象设置为 android 按钮
【发布时间】:2013-03-08 05:04:24
【问题描述】:

我正在构建一个多项选择问卷安卓程序。 我的 onCreate() 方法中有以下内容

btnArray = new Button[5];
btnArray[0] = (Button) findViewById(R.id.bOp1);
btnArray[1] = (Button) findViewById(R.id.bOp2);
btnArray[2] = (Button) findViewById(R.id.bOp3);
btnArray[3] = (Button) findViewById(R.id.bOp4);
btnArray[4] = (Button) findViewById(R.id.bOp5);

Typeface othmanyFont = Typeface.createFromAsset(getAssets(),
        "fonts/amiri.ttf");
Drawable shape = getResources().getDrawable(R.drawable.optionbutton);

for(int i=0;i<5;i++){
    btnArray[i].setTypeface(myFont);
    ((Button)btnArray[i]).setBackgroundDrawable(shape); //Button-4 only turns red
    btnArray[i].setOnClickListener(this);
}  

可绘制资源“optionbutton.xml”为“按下状态”定义红色渐变,为其他状态定义灰色渐变。如下所示

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_pressed="true" >
    <shape>
    <gradient
      android:startColor="#bf1d00"
      android:endColor="#801300"
      android:angle="270" />
        <corners android:radius="10dp" />
    <stroke
      android:width="1dp"
      android:color="#71c2eb" />
    </shape>
  </item>
  <item>
        <shape xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="rectangle">
            <gradient android:startColor="#FFFFFF" 
                android:endColor="#999"
                android:angle="270" />
            <corners android:radius="10dp" />
            <stroke android:width="1px" android:color="#0070b7" />
        </shape>
  </item>
</selector>

选择器确实有效,但仅在最后应用的按钮上。无论按下 5 中的哪个按钮,最后一个(不是按下的那个)按钮只会将其背景变为红色。

作为调试步骤:我尝试展开 for 循环并更改应用背景的顺序,最后应用的按钮只有红色背景:

((Button)btnArray[0]).setBackgroundDrawable(shape);
((Button)btnArray[1]).setBackgroundDrawable(shape);
((Button)btnArray[2]).setBackgroundDrawable(shape);
((Button)btnArray[4]).setBackgroundDrawable(shape);
((Button)btnArray[3]).setBackgroundDrawable(shape); //Button-3 only turns red

这令人困惑! 我的实现有什么问题?

【问题讨论】:

  • 你试过这个((Button)btnArray[0]).setBackgroundDrawable(shape);
  • 是的,我试过了,同样的问题。我将编辑问题以消除混淆。
  • 用XML设置背景怎么样?
  • 你也有两次xmlns:android="http://schemas.android.com/apk/res/android"。不知道这是否是一个问题。
  • 我有几个 XML,在运行时我为错误答案选择红色背景,为正确答案选择绿色背景。

标签: android textview xml-drawable


【解决方案1】:

您可以在一个 xml 中设置形状,在另一个中设置按钮状态,然后尝试这里是 xml 按钮以获得不同的状态

 <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android"> 

    <item android:drawable="@drawable/save_button_active" android:state_focused="true" android:state_pressed="true"/>
    <item android:drawable="@drawable/save_button_active" android:state_focused="false" android:state_pressed="true"/>
    <item android:drawable="@drawable/save_button_active" android:state_focused="true"/>
    <item android:drawable="@drawable/save_button_inactive" android:state_focused="false" android:state_pressed="false"/>
    </selector>

【讨论】:

    【解决方案2】:

    似乎 setBackgroundDrawable() 已被弃用。您可以使用其他替代方法,例如 setBackgroundResource()。

    【讨论】:

      【解决方案3】:

      由于不推荐使用 setBackgroundDrawable(shape) 方法,我尝试了另一种实际上完美运行并解决了问题的方法:

      ((Button)btnArray[i]).setBackgroundResource(R.drawable.optionbutton);
      

      【讨论】:

      • 感谢 vmironov、DjHacktorReborn 和 paul 的帮助
      猜你喜欢
      • 1970-01-01
      • 2013-03-25
      • 2017-07-05
      • 2016-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-04
      • 2011-12-27
      相关资源
      最近更新 更多