2017-04-19 61 views
0

如何在中心位置實現標題欄?我希望它正好在我繪製在下面的圖片上的藍線上。在此先感謝標題欄位於中心

這裏是我的代碼:

在觸摸:

 getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); 
     getSupportActionBar().setCustomView(R.layout.abs_layout); 

XML:

 <LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <TextView 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:layout_gravity="center" 
    android:text="Home" 
    android:textColor="#ffffff" 
    android:id="@+id/mytext" 
    android:textSize="18sp" /> 

    </LinearLayout> 

我的輸出: 我想它正好在藍線是我畫在下面的圖片上。

I want its exactly on the blue line that I drew on the picture below.

+0

[Android的工具欄標題中心和自定義字體(http://stackoverflow.com/a/26548766/7871886) –

+0

還是同樣的事情: )仍然不完全在中心 – CS9423

+0

我認爲你的解決方案已經[這裏](http://stackoverflow.com/questions/432037/how-do-i-center-text-horizo​​ntally-and-vertically-in-a-textview -on-android?page = 1&tab = votes#tab-top) –

回答

0

使用RelativeLayout,而不是LinearLayoutandroid:layout_gravity不適用於LinearLayout

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center_horizontal"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_gravity="center" 
     android:text="Home" 
     android:textColor="#ffffff" 
     android:id="@+id/mytext" 
     android:textSize="18sp" /> 

</RelativeLayout> 
+0

已經嘗試過。仍然不在中心 – CS9423

0

只需創建自定義工具欄。加上重力TextView的

<android.support.design.widget.AppBarLayout 
     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="@color/mypay_purple"> 

      <TextView 
       android:id="@+id/title" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center" 
       style="@style/FontNormal" 
       android:textColor="@color/mypay_white" 
       android:text="Title" 
       /> 

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

在活動

setSupportActionBar(toolbar); 
if (getSupportActionBar() != null) { 
    getSupportActionBar().setDisplayShowTitleEnabled(false); 
    title.setText("Payment"); 
}