2017-10-04 51 views
1

嘗試設置中間的ActionBar標題,而不是默認的左對齊位置。要做到這一點的基礎上,其他的答案,這是我做了什麼:如何設置中間的ActionBar標題而不是默認的左對齊位置?

Toolbar myToolbar = (Toolbar) findViewById(R.id.toolbarDownloads); 
    setSupportActionBar(myToolbar); 
    if(getSupportActionBar()!=null) 
    { 
     getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); 
     getSupportActionBar().setCustomView(R.layout.actionbar_layout); 
    } 

actionbar_layout.xml

<?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" > 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Downloads" 
     android:layout_centerInParent="true" 
     android:textColor="#000000" 
     android:id="@+id/mytext" 
     android:textSize="18sp" /> 

</RelativeLayout> 

然而,這並沒有產生預期的結果(即這使得文字仍然左對齊),這種方法有什麼問題,以及如何解決這個問題?

+0

你嘗試添加getSupportActionBar()setDisplayShowCustomEnabled()。之前getSupportActionBar()。setCustomView(R.layout.actionbar_layout); ? –

回答

5

嘗試這種使用習慣Toolbar

<android.support.v7.widget.Toolbar 
    android:id="@+id/ar_toolbar" 
    android:layout_width="match_parent" 
    android:layout_height="?attr/actionBarSize" 
    android:background="?attr/colorPrimary" 
    app:layout_scrollFlags="exitUntilCollapsed" 
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"> 


    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:text="Center" 
     android:orientation="vertical"> 

     <TextView 
      android:id="@+id/toolbar_title" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:gravity="center" /> 
    </LinearLayout> 

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

現在的java文件

private Toolbar toolbar; 
toolbar = (Toolbar) findViewById(R.id.ar_toolbar); 
TextView mTitle = (TextView) toolbar.findViewById(R.id.toolbar_title); 
mTitle.setText("Nilesh Rathod"); 
setSupportActionBar(toolbar); 
+0

@Nilesh,感謝您的快速方法,但我不能使用自定義佈局文件中的工具欄,還有其他元素依賴於Activity中的工具欄,即我有一個自定義的SmartTabLayout(https:// github.com/ogaclejapan/SmartTabLayout)與工具欄一起工作,因此無法使用工具欄進行操作欄的自定義佈局文件:/ – OBX

+1

好吧,我明白了! ,讓我試試:) – OBX

+0

@OBX讓我知道在任何查詢的情況下 –

0

你需要在你xml文件中使用Toolbar,你可以使用TextViewToolbar。您可以將自定義視圖添加到Toolbar。一旦你完成了xml文件,那麼你可以將Toolbar定義爲Activity類。

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

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="58dp" 
     android:background="?attr/colorPrimary" 
     android:minHeight="?attr/actionBarSize" 
     android:titleTextColor="#ffffff"> 

     <TextView 
      android:id="@+id/mytext" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:gravity="center" 
      android:text="Downloads" 
      android:textColor="#000000" 
      android:textSize="18sp" /> 

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

    //Rest of your code 

</LinearLayout> 

在活動類,你可以定義爲使用下面的代碼後,你的TextView和隱藏默認的標題

Toolbar toolbar; 
toolbar = (Toolbar) findViewById(R.id.ar_toolbar); 
toolbar.setTitle(""); 
1

簡單的方法使用android:layout_gravity="center"初始化TextView和設定值

裏面你活動:

setSupportActionBar(toolbar); 
getSupportActionBar().setDisplayShowTitleEnabled(false); 
toolbar_title = (TextView) findViewById(R.id.toolbar_title); 
toolbar_title.setText("My Custom center title"); 

自定義工具欄XML代碼:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/toolbar" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    app:contentInsetEnd="0dp" 
    app:contentInsetLeft="0dp" 
    app:contentInsetRight="0dp" 
    app:contentInsetStart="0dp" 
    app:contentInsetStartWithNavigation="0dp"> 

    <TextView 
     android:id="@+id/toolbar_title" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:text="Toolbar Title" /> 

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

你不需要爲這個Toolbar。就在你的活動使用像Theme.AppCompat.Light.DarkActionBar主題與ActionBar和使用下面的代碼:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    if (getSupportActionBar() != null) { 
     getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); 
     getSupportActionBar().setCustomView(R.layout.actionbar_layout); 
    } 
    //your code 
} 
0
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:orientation="vertical"> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@android:color/transparent" 
     android:minHeight="?attr/actionBarSize" 
     app:contentInsetStart="0dp"> 

     <TextView 
      android:id="@+id/back_txt" 
      style="@android:style/TextAppearance.Medium" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_margin="10dp" 
      android:gravity="center" 
      android:text="Back" /> 


     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Downloads" 
      android:layout_gravity="center|center_horizontal|center_vertical" 
      android:layout_marginTop="20dp" 
      android:layout_centerInParent="true" 
      android:textColor="#000000" 
      android:id="@+id/mytext" 
      android:textSize="18sp" /> 

     <TextView 
      android:id="@+id/filter_btn" 
      style="@style/Base.TextAppearance.AppCompat.Medium" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="right|center_vertical|center_horizontal" 
      android:layout_margin="10dp" 
      android:drawablePadding="5dp" 

      android:gravity="center" 
      android:text="Back" 
      android:textColor="@android:color/black" /> 
    </android.support.v7.widget.Toolbar> 


</LinearLayout> 
相關問題