2017-02-18 2 views
0

J'ai une barre d'outils dans mon fichier xml, aucune vue enfant n'y est définie. Je veux ajouter un TextView sur l'emplacement exact du titre par défaut dans la barre d'outils lorsque nous appelons setSupportActionBar(toolbar). Sur des événements particuliers, j'aime fondre ou disparaître ces deux points de vue en cas d'événements particuliers.Comment ajouter TextView au même endroit du titre dans la barre d'outils?

J'ai essayé de faire ce qui suit dans onCreate() de l'activité, mais rien n'est visible sauf le titre par défaut dans la barre d'outils.

TextView tv = new TextView(this); 
    tv.setLayoutParams(toolbarTitle.getLayoutParams()); 
    tv.setHeight(toolbarTitle.getHeight()); 
    tv.setWidth(toolbarTitle.getWidth()); 
    tv.setText("Other Title"); 
    tv.setLeft(toolbarTitle.getLeft()); 
    toolbar.addView(tv); 

J'ai essayé aussi d'ajouter un RelativeLayout dans la barre d'outils avec la largeur et la hauteur réglé pour correspondre parent, mais cette fois, le titre par défaut de TextView disparaît la barre d'outils.

Comment ajouter un autre TextView sur l'emplacement exact du titre par défaut TextView dans la barre d'outils?

Répondre

0

désactiver le titre dans la barre d'outils en utilisant getSupportActionBar().setDisplayShowTitleEnabled(false) dans la méthode onCreate() de mon activité et mettre deux textViews se chevauchent encapsulant eux avec relativeLayout à l'intérieur de la barre d'outils dans la mise en page. Ensuite, je les anime comme je veux. En changeant de texte, j'ai utilisé titleTextView.setText() plutôt que la méthode toolbar.setTitle().

1

Vous pouvez utiliser AppBarLayout comme ci-dessous

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.AppBarLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:id="@+id/toolbar_layout" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
> 

<android.support.v7.widget.Toolbar 
    android:id="@+id/toolbar" 
    android:layout_width="match_parent" 
    android:layout_height="?attr/actionBarSize" 
    android:background="?attr/colorPrimary" 
    > 
<TextView 
    android:id="@+id/toolbar_title" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/aadhaar_sign_up" 
    android:textColor="@color/white" 
    android:textSize="18.0sp" 
    app:fontName="Font-Medium.ttf" 
    /> 
</android.support.v7.widget.Toolbar> 

</android.support.design.widget.AppBarLayout> 

Ensuite, vous utilisez le textview comme ci-dessous

Toolbar toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar); 
((TextView) toolbar.findViewById(R.id.toolbar_title)).setText(title); 
+0

cela donnera un textview, non? Je veux que l'original textview reste là où il est plus pour en ajouter un – Mehmed

+0

Vous pouvez ajouter autant de textiews que vous voulez dans 'Toolbar' – arjun

+0

Je veux que le titre original TextView reste mais quand j'ajoute un autre TextView il disparaît. Ma question est comment puis-je conserver le TextView original et mettre un autre TextView dessus: / – Mehmed