在 Android 中为自定义按钮设置字体
Set a font on customised buttons in Android
我正在使用具有自定义背景的按钮。
首先,我将按钮图像添加到可绘制文件夹中。
然后我在名为 large_button.xml 的 drawable 中为按钮创建了一个布局:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/large_buttom_pressed"
android:state_pressed="true" />
<item android:drawable="@drawable/large_buttom_pressed"
android:state_focused="true" />
<item android:drawable="@drawable/large_buttom_default" />
</selector>
然后我使用 activity 布局中的按钮:
<Button
android:id="@+id/button_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="test button Text"
android:onClick="sendMessage"
android:background="@drawable/large_button" />
问题是(除了按钮比我想要的高),按钮中的文本全部大写:
我还想知道如何告诉它使用另一种字体(例如默认字体,或已经可用的字体,不一定是我必须在 ttf 中导入的字体)
我会在另一个问题中解决按钮大小问题,这样搜索其中一个问题的人就不必同时阅读两个问题而可能会感到困惑。
因此要修复大写使用:
android:textAllCaps="false"
要更改您可以使用的字体:
android:typeface="serif"
包含的默认字体有:普通字体、衬线字体、sans 字体和等宽字体。
从 xml
更改字体类型和样式
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textAllCaps="false"
android:typeface="monospace"/>
这样文字就不会全是大写了
android:textAllCaps="false"
如果您想更改 activity
中的字体
Button btn = (TextView) findViewById(R.id.btn);
Typeface font = Typeface.createFromAsset(getAssets(), "Helv Neue 67 Med Cond.ttf");
btn.setTypeface(font);
从你的activity你可以使用这个
private void overrideFonts(final Context context, final View v) {
try {
if (v instanceof ViewGroup) {
ViewGroup vg = (ViewGroup) v;
for (int i = 0; i < vg.getChildCount(); i++) {
View child = vg.getChildAt(i);
overrideFonts(context, child);
}
} else if (v instanceof Button ) {
((Button) v).setTypeface(Typeface.createFromAsset(context.getAssets(), "font.ttf"));
}
} catch (Exception e) {
}
}
我正在使用具有自定义背景的按钮。
首先,我将按钮图像添加到可绘制文件夹中。 然后我在名为 large_button.xml 的 drawable 中为按钮创建了一个布局:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/large_buttom_pressed"
android:state_pressed="true" />
<item android:drawable="@drawable/large_buttom_pressed"
android:state_focused="true" />
<item android:drawable="@drawable/large_buttom_default" />
</selector>
然后我使用 activity 布局中的按钮:
<Button
android:id="@+id/button_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="test button Text"
android:onClick="sendMessage"
android:background="@drawable/large_button" />
问题是(除了按钮比我想要的高),按钮中的文本全部大写:
我还想知道如何告诉它使用另一种字体(例如默认字体,或已经可用的字体,不一定是我必须在 ttf 中导入的字体)
我会在另一个问题中解决按钮大小问题,这样搜索其中一个问题的人就不必同时阅读两个问题而可能会感到困惑。
因此要修复大写使用:
android:textAllCaps="false"
要更改您可以使用的字体:
android:typeface="serif"
包含的默认字体有:普通字体、衬线字体、sans 字体和等宽字体。
从 xml
更改字体类型和样式<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textAllCaps="false"
android:typeface="monospace"/>
这样文字就不会全是大写了
android:textAllCaps="false"
如果您想更改 activity
中的字体Button btn = (TextView) findViewById(R.id.btn);
Typeface font = Typeface.createFromAsset(getAssets(), "Helv Neue 67 Med Cond.ttf");
btn.setTypeface(font);
从你的activity你可以使用这个
private void overrideFonts(final Context context, final View v) {
try {
if (v instanceof ViewGroup) {
ViewGroup vg = (ViewGroup) v;
for (int i = 0; i < vg.getChildCount(); i++) {
View child = vg.getChildAt(i);
overrideFonts(context, child);
}
} else if (v instanceof Button ) {
((Button) v).setTypeface(Typeface.createFromAsset(context.getAssets(), "font.ttf"));
}
} catch (Exception e) {
}
}