2015-07-22 2 views
0

J'ai donc réussi à obtenir une référence à un TextView qui n'est pas dans la disposition setContentView() mais je ne suis toujours pas capable de définir une police personnalisée (je ne reçois plus une exception NullPointerException) Voici le code que je:Comment faire référence à TextView pas dans la disposition setContentView()?

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); 

Voici la mise en page que je reçois avec le LayoutInflater ci-dessus:

<?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> 

Si j'ajoute Android: TEXTSTYLE = « gras » à l'TextView au point. xml ci-dessus fonctionne, mais quand j'essaie de le programmer par défaut à une police personnalisée en utilisant le code ci-dessous je n'ai rien:

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); 

Je ne reçois aucune erreur d'ailleurs.

Répondre

0

Vous créez essentiellement un nouveau TextView en le gonflant à partir de xml, puis en définissant une police personnalisée sur celui-ci. La raison pour laquelle vous ne voyez pas le changement est que la vue du texte n'apparaît pas sur votre écran. Vous devez spécifier l'emplacement où la vue texte doit être ajoutée, soit en définissant une racine dans la méthode inflate, soit en l'ajoutant par programmation à un ViewGroup dans votre mise en page de contenu.

+0

Quel serait le code pour cela? Désolé, je ne suis pas si avancé chez Android: / – Bob