【发布时间】:2018-05-03 07:06:25
【问题描述】:
colors.xml 中有五种不同的颜色。
<color name="colorSU">#4130f0</color>
<color name="colorBM">#ff752d</color>
<color name="colorTM">#2a8cbd</color>
<color name="colorBE">#7400d5</color>
<color name="colorSE">#22B573</color>
现在我需要在打开“按钮”时应用它。 (圆形切换)
toggleButton = (Switch)view.findViewById(R.id.switch_filter);
toggleBtnUserRoleDrawable();
但下面的实现并没有改变圆形开关按钮的颜色。它始终采用默认主题颜色。
private void toggleBtnUserRoleDrawable() {
String userRole = AppUtils.getUserRole(mSharedPreferences);
switch (userRole) {
case USER_TYPE_SE:
// toggleButton.setBackground(getResources().getDrawable(R.drawable.toggle_button_se));
//toggleButton.setTrackDrawable(getResources().getDrawable(R.color.colorSE));
break;
case USER_TYPE_TM:
toggleButton.setBackground(getResources().getDrawable(R.drawable.colorTM));
toggleButton.setTrackDrawable(getResources().getDrawable(R.color.colorTM));
break;
case USER_TYPE_BM:
toggleButton.setBackground(getResources().getDrawable(R.drawable.colorBM));
toggleButton.setTrackDrawable(getResources().getDrawable(R.color.colorBM));
break;
case USER_TYPE_BE:
toggleButton.setBackground(getResources().getDrawable(R.drawable.colorBM));
toggleButton.setTrackDrawable(getResources().getDrawable(R.color.colorBE));
break;
case USER_TYPE_SU:
toggleButton.setBackground(getResources().getDrawable(R.drawable.colorSU));
toggleButton.setTrackDrawable(getResources().getDrawable(R.color.colorSU));
break;
}
}
【问题讨论】:
-
不是重复的。实际上我是在以编程方式而不是在 xml 中询问。
标签: android android-layout android-togglebutton