Android,更改应用程序:以编程方式更改SwitchCompat的主题值
我想将app:theme属性的SwitchCompat值更改为其他值(@style/switchColorStyleBlue)。如何以编程方式实现这一点?(应用:主题基本上改变了切换的颜色)Android,更改应用程序:以编程方式更改SwitchCompat的主题值,android,user-interface,switchcompat,Android,User Interface,Switchcompat,我想将app:theme属性的SwitchCompat值更改为其他值(@style/switchColorStyleBlue)。如何以编程方式实现这一点?(应用:主题基本上改变了切换的颜色) 我想要的是将主题(开关的颜色)从粉红色更改为蓝色或其他颜色。您可以尝试switchCompat.setSwitchTypeface(typeface,R.style.switchcolorstypeblue) 参考:-您可以尝试switchCompat.setSwitchTypeface(字体,R.sty
我想要的是将主题(开关的颜色)从粉红色更改为蓝色或其他颜色。您可以尝试
switchCompat.setSwitchTypeface(typeface,R.style.switchcolorstypeblue)代码>
参考:-您可以尝试switchCompat.setSwitchTypeface(字体,R.style.switchColorStyleBlue)代码>
参考:-试试这个。。。我已经测试过了,它工作得非常好
public class MainActivity extends AppCompatActivity {
int[][] states = new int[][] {
new int[] {-android.R.attr.state_checked},
new int[] {android.R.attr.state_checked},
};
int[] thumbColors = new int[] {
Color.BLACK,
Color.RED,
};
int[] trackColors = new int[] {
Color.GREEN,
Color.BLUE,
};
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SwitchCompat switchCompat = (SwitchCompat) findViewById(R.id.switch);
DrawableCompat.setTintList(DrawableCompat.wrap(switchCompat.getThumbDrawable()), new ColorStateList(states, thumbColors));
DrawableCompat.setTintList(DrawableCompat.wrap(switchCompat.getTrackDrawable()), new ColorStateList(states, trackColors));
}
}
您只需根据您的要求/需要更新“TrackColor”和“thumbColor” 试试这个。。。我已经测试过了,它工作得非常好
public class MainActivity extends AppCompatActivity {
int[][] states = new int[][] {
new int[] {-android.R.attr.state_checked},
new int[] {android.R.attr.state_checked},
};
int[] thumbColors = new int[] {
Color.BLACK,
Color.RED,
};
int[] trackColors = new int[] {
Color.GREEN,
Color.BLUE,
};
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SwitchCompat switchCompat = (SwitchCompat) findViewById(R.id.switch);
DrawableCompat.setTintList(DrawableCompat.wrap(switchCompat.getThumbDrawable()), new ColorStateList(states, thumbColors));
DrawableCompat.setTintList(DrawableCompat.wrap(switchCompat.getTrackDrawable()), new ColorStateList(states, trackColors));
}
}
您只需根据您的要求/需要更新“TrackColor”和“thumbColor” 它只设置在开关上显示“文本”的字体和样式。它只设置在开关上显示“文本”的字体和样式。您运行过上面的代码吗?因为从开发者网站上的文档来看,我很怀疑你也可以设定上面的主题。请参阅摘要参考下面的XML属性部分:另外,请告诉我您希望在本次转换中实现什么。是的,我已经在我的问题中尝试了上述代码。为了更清楚,我更新了这个问题。同时看看文件,给我点时间。我正在寻找一个解决方案,并会回来给你…你有没有试过上面的代码运行它?因为从开发者网站上的文档来看,我很怀疑你也可以设定上面的主题。请参阅摘要参考下面的XML属性部分:另外,请告诉我您希望在本次转换中实现什么。是的,我已经在我的问题中尝试了上述代码。为了更清楚,我更新了这个问题。同时看看文件,给我点时间。我正在寻找解决方案,会给你回复的……是的,绝对是。谢谢你们!是的,绝对是。谢谢你们!
public class MainActivity extends AppCompatActivity {
int[][] states = new int[][] {
new int[] {-android.R.attr.state_checked},
new int[] {android.R.attr.state_checked},
};
int[] thumbColors = new int[] {
Color.BLACK,
Color.RED,
};
int[] trackColors = new int[] {
Color.GREEN,
Color.BLUE,
};
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SwitchCompat switchCompat = (SwitchCompat) findViewById(R.id.switch);
DrawableCompat.setTintList(DrawableCompat.wrap(switchCompat.getThumbDrawable()), new ColorStateList(states, thumbColors));
DrawableCompat.setTintList(DrawableCompat.wrap(switchCompat.getTrackDrawable()), new ColorStateList(states, trackColors));
}
}