简体   繁体   English

Android自定义字体Lollipop

[英]Android custom fonts Lollipop

I have two TextView which I want to put a custom font on. 我有两个要放置自定义字体的TextView。

In the OnCreate of my MainActivity, after the setContentView call, I wrote the following code: 在setContentView调用之后,在MainActivity的OnCreate中,我编写了以下代码:

Typeface myTypeface = Typeface.createFromAsset(getAssets(), "fonts/BernerBasisschrift1.ttf");
TextView welcomeTextView = (TextView)findViewById(R.id.welcome_message);
TextView introTextView = (TextView)findViewById(R.id.introduction_message);

welcomeTextView.setTypeface(myTypeface);        
introTextView.setTypeface(myTypeface);

But for some reason, the font stays default. 但是由于某种原因,字体保持默认状态。

The app runs on Galaxy S6 Edge device (Lollipop 5.0.2). 该应用程序可在Galaxy S6 Edge设备(Lollipop 5.0.2)上运行。

I read here that there's a problem with custom fonts on Lollipop, and in order to fix it I converted my font with the tool provided in the thread to ttx and vice versa, but even this didn't help. 在这里读到Lollipop上的自定义字体有问题,为了解决该问题,我使用线程中提供的工具将字体转换为ttx,反之亦然,但这还是无济于事。

Any ideas what can I do? 有什么想法我该怎么办?

Edit: Tested on Jellybean (4.3), not working either. 编辑:在豆形软糖(4.3)上测试,也不起作用。

Another edit: It works on a clean new project! 另一个编辑:它适用于一个干净的新项目! Cant figure out the difference. 无法找出差异。 Putting the MainActivity and its' XML for help: 将MainActivity及其XML寻求帮助:

public class MainActivity extends FragmentActivity {

private LoginFragment loginFragment;
private TextView welcomeTextView;
private TextView introTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    hideActionBar();
    setContentView(R.layout.activity_main);

    welcomeTextView = (TextView)findViewById(R.id.welcome_message);
    introTextView = (TextView)findViewById(R.id.introduction_message);

    Typeface myTypeface = Typeface.createFromAsset(getAssets(), "fonts/BernerBasisschrift1.ttf");
    welcomeTextView.setTypeface(myTypeface);
    introTextView.setTypeface(myTypeface);

    if (savedInstanceState == null) {
        // Add the fragment on initial activity setup
        loginFragment = new LoginFragment();
        getSupportFragmentManager().beginTransaction()
                .add(android.R.id.content, loginFragment, "facebookFragment").commit();

    }

}


@Override
public void onWindowFocusChanged(boolean hasFocus) {

    if(hasFocus){
        // get all views on screen
        ArrayList<View> views = new ArrayList<View>()
        {{
            add(findViewById(R.id.welcome_message));
            add(findViewById(R.id.introduction_message));
            add(findViewById(R.id.below_login));
            add(findViewById(R.id.above_login));
            add(findViewById(R.id.authButton));
            add(findViewById(R.id.logo));
        }};

        // set animations
        for(View view : views)
            YoYo.with(Techniques.Tada).duration(700).playOn(view);

    }
}

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public void hideActionBar(){
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
        getActionBar().hide();
}

And the activity's XML: 活动的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"
android:background="@drawable/main_background">

<ImageView
    android:id="@+id/logo"
    android:layout_width="256dp"
    android:layout_height="101dp"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="40dp"
    android:background="@drawable/logo" />

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:layout_marginStart="20dp"
    android:layout_marginLeft="20dp"
    android:layout_marginEnd="20dp"
    android:layout_marginRight="20dp">

    <TextView
        android:id="@+id/welcome_message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="@string/welcome_message"
        android:textColor="@color/White"
        android:textSize="25sp"
        style="@style/ShadowStyleText"
        android:gravity="center" />


    <TextView
        android:id="@+id/introduction_message"
        android:layout_below="@+id/welcome_message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="@string/introduction_message"
        android:textColor="@color/White"
        android:textSize="18sp"
        android:gravity="center"
        style="@style/ShadowStyleText"
        android:layout_marginTop="5dp" />

    </RelativeLayout>

<RelativeLayout
    android:id="@+id/bottom_login_chunk"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="50dp" 
    android:layout_marginStart="50dp"
    android:layout_marginLeft="50dp"
    android:layout_marginEnd="50dp"
    android:layout_marginRight="50dp">

    <TextView
        android:id="@+id/above_login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="@string/above_login_button"
        android:textColor="@color/White"
        android:textSize="18sp"
        style="@style/ShadowStyleText"
        android:gravity="center" />

    <com.facebook.widget.LoginButton
        android:id="@+id/authButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/above_login"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="5dp"
        android:layout_marginTop="5dp" />

    <TextView
        android:id="@+id/below_login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/authButton"
        android:layout_centerHorizontal="true"
        android:text="@string/below_login_button"
        android:textColor="@color/White"
        style="@style/ShadowStyleText"
        android:textSize="14sp" />

</RelativeLayout>

</RelativeLayout>

Ok so I found out recently that the facebook login button caused the problem, since everything worked when it was commented out. 好的,所以我最近发现Facebook登录按钮引起了该问题,因为注释掉后一切正常。

So basically to solve this problem use another method to show the button instead using support fragment manager; 因此,基本上要解决此问题,请使用另一种方法来显示按钮,而不要使用支持片段管理器。 sdk docs has lots of examples. sdk docs有很多示例。

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

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