2017-08-10 1 views
0

J'ai beaucoup de titres différents pour la barre d'outils que je passe de l'intention précédente creux d'activité:Comment changer la hauteur barre d'outils et la taille de la police programme en fonction de la longueur du texte

title = getIntent().getStringExtra("title"); 

Et je les mets à ma barre d'outils de nouvelle activité comme ceci:

getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
getSupportActionBar().setDisplayShowTitleEnabled(true); 
getSupportActionBar().setTitle(title); 

Mais la police est trop grande et ne couvre une ligne de barre d'outils: enter image description here

T son est l'un des exemples de titres: CONVERSION DE RADAR GAMME Kilometre PROPAGATION EN TEMPS DE MICROSECOND ET INVERSEMENT

Je voudrais savoir est-il un moyen de modifier dynamiquement la hauteur barre d'outils et la taille du texte pour adapter ce titre? Est-ce que cela peut être fait par programme?

Merci

Répondre

1

essayer cette création barre d'outils personnalisée comme celui-ci

<android.support.v7.widget.Toolbar 
    android:id="@+id/toolbar" 
    android:layout_width="match_parent" 
    android:layout_height="?attr/actionBarSize" 
    android:background="?attr/colorPrimary" 
    app:popupTheme="@style/AppTheme.PopupOverlay"> 

     <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:gravity="center" 
     android:orientation="vertical"> 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Did you know" 
      android:id="@+id/myTitle" 
      android:textColor="@android:color/white" /> 
    </LinearLayout> 
</android.support.v7.widget.Toolbar> 

et utiliser le thème de votre activité NoActionBar

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
TextView myTitle = (TextView) toolbar.findViewById(R.id.myTitle); 
+0

Existe-t-il un moyen de faire cela par programme? Aussi j'ai besoin d'obtenir la taille de police plus petite quand il y a un texte plus long ... –

+0

oui c'est possible –

+0

J'utiliserai votre réponse, parce que je n'ai pas réussi à le faire par programmation et j'ai adopté mon code à ceci. Aussi la police est maintenant plus petite et elle peut toujours s'adapter. –

1

Vous pouvez essayer comme ceci:

<android.support.v7.widget.Toolbar 
android:id="@+id/toolbar_top" 
android:layout_height="wrap_content" 
android:layout_width="match_parent" 
android:minHeight="?attr/actionBarSize" 
android:background="@color/action_bar_bkgnd" 
app:theme="@style/ToolBarTheme" > 


<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Toolbar Title" 
    android:layout_gravity="center" 
    android:id="@+id/toolbar_title" /> 


</android.support.v7.widget.Toolbar> 

et dans le fichier java;

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_top); 
TextView mTitle = (TextView) toolbarTop.findViewById(R.id.toolbar_title);