如何更改Android应用程序中自定义按钮的字体颜色?
我使用自定义按钮类为我的应用程序中的所有按钮使用自定义字体。我也想换颜色,但我不知道怎么换。这可能很容易,但我想不出来 以下是我使用的代码:如何更改Android应用程序中自定义按钮的字体颜色?,android,button,android-button,android-fonts,Android,Button,Android Button,Android Fonts,我使用自定义按钮类为我的应用程序中的所有按钮使用自定义字体。我也想换颜色,但我不知道怎么换。这可能很容易,但我想不出来 以下是我使用的代码: public class CustomButton extends Button { public CustomButton(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(
public class CustomButton extends Button {
public CustomButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public CustomButton(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public CustomButton(Context context) {
super(context);
init();
}
private void init() {
if (!isInEditMode()) {
Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "MyCustomFont.otf");
setTypeface(tf);
}
}
}
明显地
<application android:theme="@style/ApplicationStyle" android:icon="@drawable/icon" android:label="@string/app_name">
在Mystyle.xml中
<style name="ApplicationStyle" parent="android:Theme">
<item name="android:buttonStyle">@style/CKButton</item>
</style>
<style name="CKButton" parent="android:style/Widget.Button">
<item name="android:textSize">19sp</item>
<item name="android:layout_margin">0dip</item>
<item name="android:background">#ff0000</item>
</style>
@样式/按钮
19便士
0度
#ff0000
在清单中
<application android:theme="@style/ApplicationStyle" android:icon="@drawable/icon" android:label="@string/app_name">
在Mystyle.xml中
<style name="ApplicationStyle" parent="android:Theme">
<item name="android:buttonStyle">@style/CKButton</item>
</style>
<style name="CKButton" parent="android:style/Widget.Button">
<item name="android:textSize">19sp</item>
<item name="android:layout_margin">0dip</item>
<item name="android:background">#ff0000</item>
</style>
@样式/按钮
19便士
0度
#ff0000
尝试使用setTextColor(Color.BLUE)代码>
尝试使用setTextColor(Color.BLUE)代码>
这也是一个可靠的答案,但我正在寻找一个Java代码。无论如何,谢谢你。我对你的答案投了赞成票。这也是一个可靠的答案,但我正在寻找一个Java代码。无论如何,谢谢你。我对你的答案投了赞成票。