2015-07-08 14 views
5

Ich möchte die Symbolleiste Titel zu zentrieren, wenn das Layout erweitert wird, ist dies mein Code:Wie in einem CollapsingToolbarLayout erweitert Titel zum Zentrum

<?xml version="1.0" encoding="utf-8"?> 
<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:layout_height="256dp" 
    android:layout_width="match_parent" 
    app:contentScrim="?attr/colorPrimary" 
    android:fitsSystemWindows="true" 
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> 
    <android.support.design.widget.CollapsingToolbarLayout 
     android:id="@+id/collapsing_toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:layout_scrollFlags="scroll|exitUntilCollapsed" 
     android:fitsSystemWindows="true" 
     app:contentScrim="?attr/colorPrimary" 
     app:expandedTitleMarginStart="48dp" 
     app:expandedTitleMarginBottom="60dp"> 
     <ImageView 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:scaleType="centerCrop" 
      android:fitsSystemWindows="true" 
      app:layout_collapseMode="parallax"/> 
     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_height="?attr/actionBarSize" 
      android:layout_width="match_parent" 
      app:layout_collapseMode="pin" 
      app:popupTheme="@style/ThemeOverlay.AppCompat.Light" /> 
    </android.support.design.widget.CollapsingToolbarLayout> 
</android.support.design.widget.AppBarLayout> 
<android.support.v4.widget.NestedScrollView 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior"> 
</android.support.v4.widget.NestedScrollView> 

Antwort

1

Versuchen Sie, die expandedTitleGravity zum CollapsingToolbarLayout als App Attribut hinzufügen:

app:expandedTitleGravity="center_horizontal" 

Beispiel:

<android.support.design.widget.CollapsingToolbarLayout 
     android:id="@+id/collapsing_toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:fitsSystemWindows="true" 
     app:contentScrim="@color/colorPrimary" 
     app:expandedTitleGravity="center_horizontal"    
     app:layout_scrollFlags="scroll|exitUntilCollapsed"> 
+1

Ich habe dieses Attribut im Dokument nicht gesehen, ist es neu? – Escobar5

+0

Ich bin mir nicht sicher. Ich dachte, es war schon immer da. Aber ich könnte falsch liegen. –

+1

app: expandedTitleGravity = "center_horizonta | bottoml" wenn Sie andere Ansichten innerhalb Ihrer CTL, damit es genau richtig erscheinen – Sjd

1

Sie können nur die Werte dieser beiden Attribute ändern

app:expandedTitleMarginEnd="64dp" 
    app:expandedTitleMarginStart="48dp" 

in

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

Dieses Zentrum hat noch keinen Titel für mich. –

Verwandte Themen