简体   繁体   English

自定义字体仅应用于第一个textview和按钮

[英]Custom font is only applied on first textview and button

Custom font is only applied on the first textView and Button, not on the rest.I had used findViewByid to get the Views. 自定义字体仅应用于第一个textView和Button,而不应用于其余字体。我使用findViewByid获取视图。 I had used fontFam id for textview and btFontFam for button 我已经将fontFam id用于textview和btFontFam用于按钮

xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginTop="100dp" >

 <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:textSize="24sp"
    android:text="@string/L1" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="@string/LBtn"
    android:textSize="24sp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="@string/L2"
    android:textSize="24sp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="@string/L3"
    android:textSize="24sp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="@string/L4"
    android:textSize="24sp" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="@string/SBtn"
    android:textSize="24sp" />

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Typeface tf = Typeface.createFromAsset(getAssets(),
            "Fonts/Roboto-Medium.ttf");

    TextView tv = (TextView) findViewById(R.id.fontFam);
    tv.setTypeface(tf);

    Button bt = (Button) findViewById(R.id.btfontFam);
    bt.setTypeface(tf);

}
}

You should extends the TextView, Button,... class to using custom fonts. 您应该将TextView,Button,...类扩展为使用自定义字体。

First, add new property to "values/attrs.xml" file: 首先,将新属性添加到“ values / attrs.xml”文件中:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="TextViewPlus">
        <attr name="customFont" format="string"/>
    </declare-styleable>
</resources>

Second, rewrite the TextView class: 其次,重写TextView类:

package com.ex.ui;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;

public class TextViewPlus extends TextView {
    public TextViewPlus(Context context) {
        super(context);
    }

    public TextViewPlus(Context context, AttributeSet attrs) {
        super(context, attrs);
        setCustomFont(context, attrs);
    }

    public TextViewPlus(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setCustomFont(context, attrs);
    }

    private void setCustomFont(Context ctx, AttributeSet attrs) {
        TypedArray a = ctx.obtainStyledAttributes(attrs, R.styleable.TextViewPlus);
        String customFont = a.getString(R.styleable.TextViewPlus_customFont);
        setCustomFont(ctx, customFont);
        a.recycle();
    }

    public boolean setCustomFont(Context ctx, String asset) {
        Typeface tf = null;
        try {
        tf = Typeface.createFromAsset(ctx.getAssets(), asset);  
        } catch (Exception e) {
            return false;
        }
        setTypeface(tf);  
        return true;
    }
}

Then, using with custom font: 然后,使用自定义字体:

<com.ex.ui.TextViewPlus
xmlns:android="http://schemas.android.com/apk/res/android"    
xmlns:app="http://schemas.android.com/apk/res/com.ex"  
android:id="@android:id/text1"    
android:layout_width="match_parent"
android:layout_height="match_parent"
app:customFont="Roboto-Light.ttf"/>

Note: 注意:

"xmlns:app="http://schemas.android.com/apk/res/com.ex" => "com.ex" is the main package, that mean you can access the resource via com.ex.R “ xmlns:app =” http://schemas.android.com/apk/res/com.ex“ =>” com.ex“是主要软件包,这意味着您可以通过com.ex.R访问资源

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

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