【问题标题】:Change color or custom button更改颜色或自定义按钮
【发布时间】:2017-06-15 17:35:21
【问题描述】:

您好,我制作了一个自定义圆角按钮 ( gps_button ),它可以工作,但我想根据按钮的状态更改按钮的颜色,但是颜色没有改变。颜色变量存储在 Colors.xml 中

我的主要问题是如何将 Color.xml 中的颜色设置为我的自定义按钮?

这是我的代码主要活动:

public void start_gps (View view){

gps_button = (Button) (findViewById(R.id.start_gps_button));

if ((log_state == true) && (start_gps_button_state == false)){

            start_gps_button_state = true;
            gps_button.setBackgroundColor(R.color.startGpsColor);
            gps_button.setText("STOP");
            voyagePoints.setText("0");

            Toast.makeText(this, "GPS Started", Toast.LENGTH_SHORT).show();

        }else if ((log_state == true) && start_gps_button_state == true){

            start_gps_button_state = false;
            gps_button.setBackgroundColor(R.color.stopGpsColor);
            gps_button.setText("START");

            Toast.makeText(this, "GPS Stopped", Toast.LENGTH_SHORT).show();
        }
    }

这是自定义按钮:

    <?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_pressed="true" >
        <shape android:shape="rectangle"  >
            <corners android:radius="10dip" />
            <stroke android:width="1dip" android:color="#1EB7F1" />
            <gradient android:angle="-90" android:startColor="#1EB7F1" android:endColor="#1EB7F1"  />
        </shape>
    </item>
    <item android:state_focused="true">
        <shape android:shape="rectangle"  >
            <corners android:radius="10dip" />
            <stroke android:width="1dip" android:color="#1EB7F1" />
            <solid android:color="#1EB7F1"/>
        </shape>
    </item>
    <item >
        <shape android:shape="rectangle"  >
            <corners android:radius="10dip" />
            <stroke android:width="1dip" android:color="#1EB7F1" />
            <gradient android:angle="-90" android:startColor="#1EB7F1" android:endColor="#1EB7F1" />
        </shape>
    </item>
</selector>

这是 Color.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#A7E483</color>
    <color name="colorPrimaryDark">#A7E483</color>
    <color name="colorAccent">#FF4081</color>
    <color name="stopGpsColor">#F9108E</color>
    <color name="startGpsColor">#1EB7F1</color>
</resources>

【问题讨论】:

    标签: android xml


    【解决方案1】:

    this question。你必须使用button.setBackgroundColor(getResources().getColor(R.color.colorPrimary));

    【讨论】:

    • 谢谢,但这看起来适用于“工厂”按钮,它适用于我的客户按钮 >?
    • 是的,它会的。只需使用 gps_button.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
    • @Cardona 所有按钮,包括您的自定义按钮,都可以访问相同的 setBackgroundColor() 方法
    【解决方案2】:

    我让它工作,也从这里找到它https://stackoverflow.com/a/34057311/1515105

    gps_button.setBackgroundTintList(ColorStateList.valueOf(ContextCompat.getColor(this, R.color.startGpsColor)));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多