放在哪里的自定义按钮字体此行code的? [英] Where to put this line of code for custom button font?

查看:90
本文介绍了放在哪里的自定义按钮字体此行code的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我应该在哪里把这个code

 按钮的txt =(按钮)findViewById(R.id.button1);
    字体的字体= Typeface.createFromAsset(getAssets(),customfont.ttf);
    txt.setTypeface(字体);

这样我就可以更改显示在我的按钮上的文字的字体?
我只是想在按钮上的字体变化。我不是想改变在警告对话框中的字体。我只是不知道放在哪里code键更改按钮字体。我已经有放置在一个字体文件夹位于资产自定义字体。

下面是按钮的XML部分:

 <按钮
    机器人:ID =@ + ID /按钮1
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    安卓的onClick =alertBtn
    机器人:文字=点击信息
    机器人:TEXTSIZE =22sp
    />

下面是我的项目的code:

 包com.test.hellothere;进口android.app.Activity;
进口android.app.AlertDialog;
进口android.graphics.Typeface;
进口android.os.Bundle;
进口android.view.View;
进口android.widget.Button;公共类HelloThereActivity扩展活动实现View.OnClickListener {
    / **当第一次创建活动调用。 * /
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);
    }    公共无效的onClick(视图v){}    公共无效alertBtn(视图v){        新AlertDialog.Builder(本)
        .setTitle(你好)
        .setMessage(你好!)
        .setNeutralButton(返回,NULL)
        。显示();
    }    }


解决方案

把你的code里面的的onCreate 活动之后,的setContentView 为:

 公共类HelloThereActivity扩展活动实现View.OnClickListener {
    / **当第一次创建活动调用。 * /
 按钮TXT;
 字体字库;
  @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);
       TXT =(按钮)findViewById(R.id.button1);       //调用这里设置字体
       setfonttoView(TXT);       //.....same其他按钮
    }
  公共无效setfonttoView(Button按钮){
       字体= Typeface.createFromAsset(getAssets(),customfont.ttf);
       button.setTypeface(字体);
    }  //你的code这里...

Where should I put this code

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

so I can change the font of the text that is displayed on my button? I just want the font on the button changed. I am not trying to change the font in the alert dialog box. I just don't know where to put the code to change the button font. I already have the custom font placed inside a "fonts" folder located in assets.

Here is the xml part of the button:

<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"
    />

Here is the code of my project:

package com.test.hellothere;

import android.app.Activity;
import android.app.AlertDialog;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

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


    }

    public void onClick(View v){}

    public void alertBtn(View v){

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

    }

解决方案

put your code inside onCreate of Activity after setContentView as:

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

这篇关于放在哪里的自定义按钮字体此行code的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆