2016-08-13 34 views
2

Ich versuche, einen Untertitel in der Symbolleiste zu zeigen, aber es passiert nichts:getSupportActionBar() setSubtitle() zeigt nichts

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
setSupportActionBar(toolbar); 
setTitle("my title"); 
getSupportActionBar().setSubtitle("my subtitle"); 

Titel korrekt dargestellt, aber Untertitel nicht.

EDIT

es in einer anderen Aktivitäten arbeitet, aber ich habe eine CollapsingToolbarLayout in diesem, so denke ich, dass Ursachen zurückzuführen ist:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:app="http://schemas.android.com/apk/res-auto" 
     android:id="@+id/main_content" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:fitsSystemWindows="true"> 

     <android.support.design.widget.AppBarLayout 
      android:id="@+id/appbar" 
      android:layout_width="match_parent" 
      android:layout_height="256dp" 
      android:fitsSystemWindows="true" 

      android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> 

      <android.support.design.widget.CollapsingToolbarLayout 
       android:id="@+id/collapsing_layout" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:fitsSystemWindows="true" 
       app:contentScrim="?attr/colorPrimary" 
       app:expandedTitleMarginEnd="64dp" 
       app:expandedTitleMarginStart="48dp" 
       app:layout_scrollFlags="scroll|exitUntilCollapsed"> 

       <ImageView 
        android:id="@+id/usr_imageview" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:fitsSystemWindows="true" 
        android:scaleType="centerCrop" 
        app:layout_collapseMode="parallax" /> 

       <android.support.v7.widget.Toolbar 
        android:id="@+id/toolbar" 
        android:layout_width="match_parent" 
        android:layout_height="?attr/actionBarSize" 
        app:layout_collapseMode="pin" 
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"> 

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

      </android.support.design.widget.CollapsingToolbarLayout> 
+0

Hat Ihr user.getNick() gültigen Wert hat? – Smit

+0

@Smit Sicher, ich teste es mit einem Literal String –

+0

@vrundpurohit Nein, in einer Aktivität –

Antwort

0

Mögen diese setSubtitle(CharSequence subtitle) es hinzufügen.

 Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 

     // Title and subtitle 
     toolbar.setTitle(R.string.about_toolbar_title); 
     toolbar.setSubtitle("My Subtitle"); 

     activity.setSupportActionBar(toolbar); 

Android official docs.

+0

Danke für die Antwort, nichts passiert ... –

0

diese

Toolbar toolbar = (Toolbar) activity.findViewById(R.id.toolbar); 
activity.setSupportActionBar(toolbar); 
setTitle("my title"); 
toolbar.setSubtitle("my subtitle"); 

Sie müssen versuchen, Symbolleiste setSubtitle.

+0

Danke für die Antwort, passiert nichts ... –

0

Code unten verwenden, ohne Toolbar Verwendung mit diesem

try{ 
     getSupportActionBar().setTitle("Title"); 
     getSupportActionBar().setSubtitle("Sub Title"); 
     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
     getSupportActionBar().setHomeButtonEnabled(true); 
    } 
    catch (Exception e){ 
     e.printStackTrace(); 
    } 
0

Versuchen. Stellen Sie sicher, dass Sie eine AppCompatActivity verwenden.

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     getSupportActionBar().setTitle("Toolbar example"); 
     toolbar.setSubtitle("Here you go"); 

    }