在Android Studio中更改字体样式的不同方法有哪些

时间:2017-06-24 05:39:27

标签: android android-layout

我是Android应用开发的新手。我想更改我的应用程序的字体。 我想知道通过XML和Java更改字体的不同方法。  想要改变的字体: 1.TextView 2.RadioButton 3.EditText 4.CheckBox等。

5 个答案:

答案 0 :(得分:3)

  1. 在Android Studio中右键单击 app &创建文件夹资产
  2. 右键点击资源并创建一个文件夹字体

  3. 下载.ttf文件,即fontName.ttf并粘贴到fonts文件夹中。

  4. 现在你必须做主要的事情。在你的包中创建一个类。

    此课程适用于TextView

    public class Railway_Regular extends TextView {
    public Railway_Regular(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.setTypeface(Typeface.createFromAsset(context.getAssets(),"fonts/Raleway-Regular.ttf"));
    }}
    

    此课程适用于按钮

    public class Railway_Regular_Btn extends Button {
    public Railway_Regular_Btn(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.setTypeface(Typeface.createFromAsset(context.getAssets(),"fonts/Raleway-Regular.ttf"));
    }}
    

    此课程适用于EditText

    public class Railway_Regular_EdTx extends EditText {
    public Railway_Regular_EdTx(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.setTypeface(Typeface.createFromAsset(context.getAssets(),"fonts/Raleway-Regular.ttf"));
    }}
    

    就像这些一样,你可以为你所有的小部件创建类。将fontname.ttf引用到您的类中。

    现在,设置textview作为你的字体。

    for TextView

    <package_name.fonts.Railway_Regular
        android:padding="5sp"
        android:id="@+id/test_nameDR"
        android:layout_marginTop="4dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Details Text"
        android:textColor="@color/black"
        android:textSize="14sp"/>
    
    按钮的

     <package_name.fonts.Railway_Regular_Btn
        android:id="@+id/revSubmit"
        android:background="@color/greenDeep"
        android:layout_marginTop="-54dp"
        android:layout_width="match_parent"
        android:layout_height="54dp"
        android:textColor="@color/white"
        android:textSize="16dp"
        android:text="SUBMIT REVIEW"
        />
    

    适用于EditText

       <package_name.fonts.Railway_Regular_EdTx
                    android:layout_width="match_parent"
                    android:layout_height="40dp"
                    android:layout_marginLeft="30dp"
                    android:layout_marginRight="30dp"
                    android:layout_marginTop="15dp"
                    android:textColorHint="@color/lightgray"
                    android:textSize="14dp"
                    android:background="@drawable/edit_text"
                    android:id="@+id/email_id"
                    android:hint="Email Id"
                    android:inputType="textEmailAddress" />
    

    更改字体的第二种方法:

      Typeface typeface,typeface2;
      typeface = Typeface.createFromAsset(this.getAssets(),"fonts/Raleway-Regular.ttf");
      typeface2 = Typeface.createFromAsset(this.getAssets(),"fonts/Raleway-SemiBold.ttf");
    
     button1.setTypeface(typeface);
     edit_text1.setTypeface(typeface2);
    

答案 1 :(得分:0)

您可以使用TTF格式更改字体

 tv1=(TextView)findViewById(R.id.textView3);
      tv2=(TextView)findViewById(R.id.textView4);

      Typeface face= Typeface.createFromAsset(getAssets(), "font/font.ttf");
      tv1.setTypeface(face);

      Typeface face1= Typeface.createFromAsset(getAssets(), "font/font1.ttf");
      tv2.setTypeface(face1);

https://www.tutorialspoint.com/android/android_custom_fonts.htm

http://tekeye.uk/android/examples/android-textview-edittext-font

答案 2 :(得分:0)

您可以使用以下代码进行编程:RadioButton / TextView / Checkbox -

还要确保在Assets文件夹中有相应的.ttf文件。

RadioButton rb  = (RadioButton) findViewById(R.id.radiobutton);
Typeface font = Typeface.createFromAsset(getAssets(), "SF_Cartoonist_Hand_Bold.ttf");
rb.setTypeface(font);

或者您可以在TextView中的xml文件中输入 -

从android 4.1 / 4.2 / 5.0开始,可以使用以下Roboto字体系列:

android:fontFamily="sans-serif"           // roboto regular
android:fontFamily="sans-serif-light"     // roboto light
android:fontFamily="sans-serif-condensed" // roboto condensed
android:fontFamily="sans-serif-black"     // roboto black
android:fontFamily="sans-serif-thin"      // roboto thin (android 4.2)
android:fontFamily="sans-serif-medium"    // roboto medium (android 5.0)

答案 3 :(得分:0)

您可以更改应用程序的字体,以便在项目的assets文件夹下创建fonts文件夹并将TTF放入其中。可能会帮助您How to add external fonts to android application

答案 4 :(得分:0)

您可以使用各种方法更改字体。其中之一是:

  Typeface typeface,typeface2;
  typeface = Typeface.createFromAsset(this.getAssets(),"fonts/Raleway-
  Regular.ttf");
  typeface2 = Typeface.createFromAsset(this.getAssets(),"fonts/Raleway-
  SemiBold.ttf");

  button1.setTypeface(typeface);
  edit_text1.setTypeface(typeface2);
相关问题