繁体   English   中英

Android自定义按钮字体

[英]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"

上面是xml中按钮的代码,我是资产文件夹中“ fonts”文件夹中的自定义字体。 如何更改按钮的字体以匹配我的自定义字体?

该按钮的作用如下:

public void onClick(View v){}

public void alertBtn(View v){

    new AlertDialog.Builder(this)
    .setTitle("Button 1")
    .setMessage("Hello")
    .setNeutralButton("Go Back", null)
    .show();
}

我尝试使用

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

但它说R.id.custom_font); 无法解析或不是字段,并且txt.setTypeface(font); font出现语法错误txt.setTypeface(font);

Button txt = (Button) findViewById(R.id.custom_font);

应该

Button txt = (Button) findViewById(R.id.button1);

android:id="@+id/button1"

应该

android:id="@+id/custom_font"

这样可以解决您无法解决的错误,但是在setTypeface行中看不到语法错误。

更改

Button txt = (Button) findViewById(R.id.custom_font);

Button txt = (Button) findViewById(R.id.button1);

android:onClick="alertBtn1"android:onClick="alertBtn"因为您要在Button xml中设置alertBtn1 ,并且在代码部分中您具有带有alertBtn名称的方法

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM