[英]Android Custom Button Font
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="alertBtn1"
android:text="Hello"
android:textSize="22sp"
Above is the code for the button in the xml, and I the custom font in a folder "fonts" in the assets folder. 上面是xml中按钮的代码,我是资产文件夹中“ fonts”文件夹中的自定义字体。 How can I change the font of the button to match the custom font I have? 如何更改按钮的字体以匹配我的自定义字体?
Here is what the button does: 该按钮的作用如下:
public void onClick(View v){}
public void alertBtn(View v){
new AlertDialog.Builder(this)
.setTitle("Button 1")
.setMessage("Hello")
.setNeutralButton("Go Back", null)
.show();
}
I tried using 我尝试使用
Button txt = (Button) findViewById(R.id.custom_font);
Typeface font = Typeface.createFromAsset(getAssets(), "custom_font.ttf");
txt.setTypeface(font);
but it said R.id.custom_font);
但它说R.id.custom_font);
cannot be resolved or is not a field, and that there is a syntax error at font in txt.setTypeface(font);
无法解析或不是字段,并且txt.setTypeface(font);
font出现语法错误txt.setTypeface(font);
Button txt = (Button) findViewById(R.id.custom_font);
Should be 应该
Button txt = (Button) findViewById(R.id.button1);
OR android:id="@+id/button1"
或android:id="@+id/button1"
should be 应该
android:id="@+id/custom_font"
That will fix your cannot be resolved error, but I don't see the syntax error in the setTypeface line. 这样可以解决您无法解决的错误,但是在setTypeface行中看不到语法错误。
Change 更改
Button txt = (Button) findViewById(R.id.custom_font);
to 至
Button txt = (Button) findViewById(R.id.button1);
and android:onClick="alertBtn1"
to android:onClick="alertBtn"
becuase you are setting alertBtn1
in Button xml and in code part you have method with alertBtn
name 和android:onClick="alertBtn1"
为android:onClick="alertBtn"
因为您要在Button xml中设置alertBtn1
,并且在代码部分中您具有带有alertBtn
名称的方法
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.