在Android中的自定义按钮上设置字体

在Android中的自定义按钮上设置字体,android,fonts,android-button,Android,Fonts,Android Button,我正在使用自定义背景的按钮 首先,我将按钮图像添加到drawable文件夹中。 然后,我在drawable中为按钮创建了一个名为large_button.xml的布局: <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/lar

我正在使用自定义背景的按钮

首先,我将按钮图像添加到drawable文件夹中。 然后,我在drawable中为按钮创建了一个名为large_button.xml的布局:

<?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>

然后我使用活动布局中的按钮:

<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、monospace。

从xml更改字体的类型和样式

<Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textStyle="bold"
        android:textAllCaps="false"
        android:typeface="monospace"/>
如果要更改活动中的字体

Button btn = (TextView) findViewById(R.id.btn);
Typeface font = Typeface.createFromAsset(getAssets(), "Helv Neue 67 Med Cond.ttf");
btn.setTypeface(font);
从您的活动中,您可以使用此

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) {
  }
 }
private void覆盖(最终上下文,最终视图v){
试一试{
if(视图组的v实例){
视图组vg=(视图组)v;
对于(int i=0;i
也许你应该看看这些字体,它们要么是全局字体,要么是谷歌默认提供的字体,链接如下:谢谢你,是的,
android:textAllCaps=“false”
在我将其添加到布局文件中的按钮时确实有效。有没有办法从可绘制的xml文件中执行此操作以影响应用程序中的所有按钮?谢谢,是的,
android:textAllCaps=“false”
在我将其添加到布局文件中的按钮时起作用。是否有一种方法可以从可绘制的xml文件中执行此操作以影响应用程序中的所有按钮?同样,字体也是如此:如果我在活动中执行此操作,我必须为应用程序中每个活动的每个按钮编写此操作。理想情况下,我只需要写一次。
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) {
  }
 }