2016-03-03 76 views
0

我想在工具栏中使用toolbar_title布局中心,但它看起来像左边有一些边距,如果我移除了CollapsingToolbarLayout就没问题。 中心toolbar_title文本我设置toolbar_title的边缘右边的代码,但只有片段恢复后,我toolbar_title.getX()可以得到正确的值,所以我postDelayed 50毫秒做到这一点。CollapsingToolbarLayout中心工具栏中的TextView

现在是问题: 如何更好地将CoordinatorLayout中的toolbar_title居中?

<android.support.design.widget.CoordinatorLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
xmlns:tools="http://schemas.android.com/tools"> 

<android.support.design.widget.AppBarLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <android.support.design.widget.CollapsingToolbarLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:expandedTitleMarginStart="?attr/actionBarSize" 
     app:layout_scrollFlags="scroll|exitUntilCollapsed"> 

     <LinearLayout 
      android:id="@+id/layout_header" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="#f5f5f5" 
      android:orientation="vertical" 
      app:layout_collapseMode="parallax"> 
     </LinearLayout> 

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

      <TextView 
       android:id="@+id/toolbar_title" 
       style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:gravity="center" 
       android:textColor="@android:color/white" 
       android:textSize="18sp" 
       tools:text="title"/> 

     </android.support.v7.widget.Toolbar> 
    </android.support.design.widget.CollapsingToolbarLayout> 
</android.support.design.widget.AppBarLayout> 

<android.support.v7.widget.RecyclerView 
    android:id="@+id/swipe_container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior"/> 

回答

0

你只需要添加以下属性在您的工具栏。

app:contentInsetLeft="0dp" 
app:contentInsetStart="0dp" 

查看计算器类似的线程:Android API 21 Toolbar Padding

前看到和属性工具栏中添加标题后TextView的位置。

添加属性之前:

Before adding attributes

添加属性后:

enter image description here

+0

它的工作原理,如果没有homeAsUpIndicator,当显示homeAsUpIndicator标题还是右边 – qinqie

相关问题