嘗試設置中間的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>
然而,這並沒有產生預期的結果(即這使得文字仍然左對齊),這種方法有什麼問題,以及如何解決這個問題?
你嘗試添加getSupportActionBar()setDisplayShowCustomEnabled()。之前getSupportActionBar()。setCustomView(R.layout.actionbar_layout); ? –