【问题标题】:Button change font family in android studioandroid studio中的按钮更改字体系列
【发布时间】:2015-05-05 13:15:18
【问题描述】:

我想更改按钮的字体系列。我试图改变它尝试这段代码:

Button txt = (Button)findViewById(R.id.button1);
Typeface font = Typeface.createFromAsset(getAssets(), "sfont.ttf");
txt.setTypeface(font);

但我收到一个错误,如果我忽略它,应用程序每次都会崩溃。

这是布局 XML 代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:orientation="vertical"
>

<TextView

    android:text="@string/enter"
    android:textColor="#000"
    android:textSize="16dp"
    android:textAlignment="center"
    android:id="@+id/**button1**"text1
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:padding="20dp" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/**text1**" button1
    android:padding="30dp"
    android:layout_gravity="center"
    android:text="@string/entenbut"
    />

问题已解决 XML 代码中存在问题 谢谢大家:)

【问题讨论】:

  • 您介意与我们分享异常的堆栈跟踪吗?
  • 对不起,我怎样才能得到异常的堆栈跟踪?这是我的第一个应用程序:)
  • 查看logcat的内容
  • 这就是你可以在预览中看到的:app.box.com/s/zst4c76cwzwiteqq5tdtvcvy7sgleeyc
  • 为什么要将AppCompatTextView 转换为Button

标签: java android android-studio


【解决方案1】:

在您的文件夹“src”中创建一个名为“Assets”的文件夹(如果您使用 Android Studio)并使用:

Typeface tfFutura = Typeface.createFromAsset(getAssets(), "sfont.ttf");

观察:看看你的文件是“TTF”还是“ttf”。我遇到了问题。

【讨论】:

【解决方案2】:

在android studio中没有目录“src”但有“res”。 要创建资产目录,首先右键单击“应用程序”,然后: 新建 -> 文件夹 -> 资产文件夹。在此之后,将字体复制到此目录。 click here for more info

【讨论】:

  • 谢谢!这篇文章正是我想要的。
猜你喜欢
  • 2015-12-25
  • 2015-07-06
  • 2021-05-13
  • 2020-05-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-12
  • 1970-01-01
相关资源
最近更新 更多