简体   繁体   中英

How to get reference to TextView not in setContentView() layout?

So I managed to get a reference to a TextView that is not in the setContentView() layout but I'm still not able to set a custom font (I'm not getting a NullPointerException anymore, so at least that's good)> This is the code that I used:

LayoutInflater mInflater = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        View textview= (View) mInflater.inflate(R.layout.item, null);

        TextView myTextView = (TextView) textview.findViewById(R.id.helloText);
        Typeface tf = Typeface.createFromAsset(getAssets(),"fonts/custom_font.ttf");
        myTextView.setTypeface(tf);

Here's the layout I'm getting with the LayoutInflater above:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_gravity="center"
    android:layout_width="300dp"
    android:layout_height="230dp">

    <TextView
        android:id="@+id/helloText"
        android:textSize="20sp"
        android:textColor="#567dc0"
        android:background="#fbcb43"
        android:gravity="center"
        tools:text="@string/hello_world"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <View
        android:id="@+id/item_swipe_left_indicator"
        android:alpha="0"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_margin="10dp"
        android:background="#A5F" />

    <View
        android:id="@+id/item_swipe_right_indicator"
        android:alpha="0"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_margin="10dp"
        android:layout_gravity="right"
        android:background="#5AF" />

</FrameLayout>

If I add android:textStyle="bold" to the TextView in item.xml above it works, but when I try to programatically set it to a custom font using the code below I get nothing:

LayoutInflater mInflater = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            View textview= (View) mInflater.inflate(R.layout.item, null);

            TextView myTextView = (TextView) textview.findViewById(R.id.helloText);
            Typeface tf = Typeface.createFromAsset(getAssets(),"fonts/custom_font.ttf");
            myTextView.setTypeface(tf);

I am not getting any errors by the way.

You are essentially programmatically creating a new TextView by inflating it from xml, and then setting a custom font on it. The reason you're not seeing the change is that the text view is not on your screen. You need to specify where the textview is to be added, either by setting a root in the inflate method or programatically adding it to a ViewGroup in your content layout.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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