Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/189.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 自定义按钮字体的这行代码放在哪里?_Java_Android_Xml_Button_Fonts - Fatal编程技术网

Java 自定义按钮字体的这行代码放在哪里?

Java 自定义按钮字体的这行代码放在哪里?,java,android,xml,button,fonts,Java,Android,Xml,Button,Fonts,我应该把这个代码放在哪里 Button txt = (Button) findViewById(R.id.button1); Typeface font = Typeface.createFromAsset(getAssets(), "customfont.ttf"); txt.setTypeface(font); 所以我可以更改按钮上显示的文本的字体? 我只想更改按钮上的字体。我没有试图更改“警报”对话框中的字体。我只是不知道把更改按钮字体的代码放在哪里。我已经将自

我应该把这个代码放在哪里

 Button txt = (Button) findViewById(R.id.button1);  
    Typeface font = Typeface.createFromAsset(getAssets(), "customfont.ttf");  
    txt.setTypeface(font);
所以我可以更改按钮上显示的文本的字体? 我只想更改按钮上的字体。我没有试图更改“警报”对话框中的字体。我只是不知道把更改按钮字体的代码放在哪里。我已经将自定义字体放在资产中的“字体”文件夹中

以下是按钮的xml部分:

<Button
    android:id="@+id/button1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:onClick="alertBtn"
    android:text="Click for message" 
    android:textSize="22sp"
    />

将代码放入
setContentView
之后的活动的
onCreate
中,如下所示:

public class HelloThereActivity extends Activity implements View.OnClickListener{
    /** Called when the activity is first created. */
 Button txt;
 Typeface font;
  @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       txt = (Button) findViewById(R.id.button1);  

       // call here for setting font 
       setfonttoView(txt);

       //.....same for other buttons
    }
  public void setfonttoView(Button button){
       font = Typeface.createFromAsset(getAssets(), "customfont.ttf");  
       button.setTypeface(font);
    }

  //your code here...

setContentView()之后的
onCreate()

如果字体位于子文件夹下,则应在路径中引用它,即
Typeface.createFromAsset(getAssets(),“fonts/customfont.ttf”)
谢谢,还有一种方法可以一次为多个按钮设置字体,这样我就不必反复键入代码,为了整洁起见?@CaptnBuzz:只需在同一个类中创建一个方法,并将button实例作为参数传递给它,以便将字体设置为多个按钮,可以给我一个示例吗?对所有的错误感到抱歉questions@CaptnBuzz当前位置查看我的编辑答案甜蜜的谢谢你刚刚教了我很多哈哈:)
public class HelloThereActivity extends Activity implements View.OnClickListener{
    /** Called when the activity is first created. */
 Button txt;
 Typeface font;
  @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       txt = (Button) findViewById(R.id.button1);  

       // call here for setting font 
       setfonttoView(txt);

       //.....same for other buttons
    }
  public void setfonttoView(Button button){
       font = Typeface.createFromAsset(getAssets(), "customfont.ttf");  
       button.setTypeface(font);
    }

  //your code here...
Button txt = (Button) findViewById(R.id.button1);  
Typeface font = Typeface.createFromAsset(getAssets(), "customfont.ttf");  
txt.setTypeface(font);