Android、setText和setTypeface don´;行不通

Android、setText和setTypeface don´;行不通,android,android-layout,android-studio,Android,Android Layout,Android Studio,我对某件事有一个小问题,我已经说过很多次了,而且都是正确的。我想分别使用自己的字体。外部google Roboto字体用于我在MainActivity中的按钮文本,因此我使用了一个简单的代码,在应用程序/活动启动时,onCreate()方法中使用了该代码。然后我试着只改变文本,但它也不起作用。应用程序启动后,它将保持为指定的活动_main.xml,任何更改。当我使用另一个按钮的功能,它的工作。问题在哪里?因为我想在活动开始后立即设置它 MainActivity.java public class

我对某件事有一个小问题,我已经说过很多次了,而且都是正确的。我想分别使用自己的字体。外部google Roboto字体用于我在MainActivity中的按钮文本,因此我使用了一个简单的代码,在应用程序/活动启动时,onCreate()方法中使用了该代码。然后我试着只改变文本,但它也不起作用。应用程序启动后,它将保持为指定的活动_main.xml,任何更改。当我使用另一个按钮的功能,它的工作。问题在哪里?因为我想在活动开始后立即设置它

MainActivity.java

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    DbHelper cryfi_db = new DbHelper(this);
    DbHelper_user user_db = new DbHelper_user(this);

    Button btn_ratings = (Button) findViewById(R.id.btn_ratings);
    Typeface roboto_light = Typeface.createFromAsset(getAssets(), "fonts/Roboto-Light.ttf");
    btn_ratings.setTypeface(roboto_light);

    //here are some others working functions for sqlite dbs
}

public void onButtonClick(View v){
    Button btn_ratings = (Button) findViewById(R.id.btn_ratings);
    Typeface roboto_light = Typeface.createFromAsset(getAssets(), "fonts/Roboto-Light.ttf");
    btn_ratings.setTypeface(roboto_light);
}
该问题是否与数据库功能或扩展AppCompatActivity相关

活动\u main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<ImageView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/iv_intro"
    android:src="@drawable/intro_1"
    android:scaleType="fitXY" />

<Button
    android:id="@+id/btn_ratings"
    android:text="@string/ratings"
    android:textColor="#FFFFFF"
    android:textSize="20sp"
    android:layout_width="200dp"
    android:layout_height="40dp"
    android:background="@drawable/intro_btn"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="20dp" />

<Button
    android:id="@+id/btn_generate"
    android:text="@string/generate"
    android:textColor="#FFFFFF"
    android:textSize="20sp"
    android:layout_width="200dp"
    android:layout_height="40dp"
    android:background="@drawable/intro_btn"
    android:layout_marginBottom="20dp"
    android:layout_above="@+id/btn_ratings"
    android:layout_alignLeft="@+id/btn_ratings"
    android:layout_alignStart="@+id/btn_ratings" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="OtherButton"
    android:id="@+id/button2"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="47dp"
    android:layout_below="@+id/ib_settings"
    android:onClick="onButtonClick" />


我还有其他的ImageButton,但它们与此无关。

使用TypeFaceHelper类缓存字体并使用它们

public class Typefaces{

    private static final Hashtable<String, Typeface> cache = new Hashtable<String, Typeface>();

    public static Typeface get(Context c, String name){
        synchronized(cache){
            if(!cache.containsKey(name)){
                Typeface t = Typeface.createFromAsset(c.getAssets(), String.format("fonts/%s.ttf", name));
                cache.put(name, t);
            }
            return cache.get(name);
        }
    }    
}

创建可解决问题的自定义按钮:

public final class RobotoButton extends Button {
    private static Typeface mTypeface;
     public RobotoButton(final Context context, final AttributeSet attrs) {
         super(context, attrs);
         setTypeface(Typeface.createFromAsset(getAssets(), "fonts/Roboto-Light.ttf"));
     }
}
在xml中:

<com.x.RobotoButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="OtherButton"......./>

只需确保您的ttf文件位于主/assets/fonts文件夹中即可。 并在中尝试相同的代码

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if(hasFocus){
             Button btn_ratings = (Button) findViewById(R.id.btn_ratings);
             Typeface roboto_light = Typeface.createFromAsset(getAssets(), "fonts/Roboto-Light.ttf");
            btn_ratings.setTypeface(roboto_light);
        }
    }

有趣的解决方案,但不起作用。我调用onCreate()-btn_ratings.setTypeface(Typefaces.get(这是“Roboto Light”)。。。但它仍然保持原样,字体也没有改变。我确信,它通过点击按钮改变了字体,但在onCreate()中没有。谢谢,它工作正常。你能让我和其他人快速解释一下为什么会这样吗?为什么这个简单的代码在onCreate()中不起作用?
    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if(hasFocus){
             Button btn_ratings = (Button) findViewById(R.id.btn_ratings);
             Typeface roboto_light = Typeface.createFromAsset(getAssets(), "fonts/Roboto-Light.ttf");
            btn_ratings.setTypeface(roboto_light);
        }
    }