【发布时间】:2018-03-15 10:37:18
【问题描述】:
尝试将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) 之前; ?
标签: java android xml android-actionbar