[英]Action bar title is not aligning properly, android?
我在應用程序中使用工具欄
這是我的代碼
toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
我的XML
<android.support.v7.widget.Toolbar
android:id="@+id/tool_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/appColor"
android:minHeight="?attr/actionBarSize"
app:theme="@style/MyDarkToolbarStyle" />
樣式
<style name="MyDarkToolbarStyle" parent="Widget.AppCompat.Toolbar">
<item name="android:gravity">center_vertical</item>
<item name="popupTheme">@style/PopupMenuStyle</item>
</style>
<style name="PopupMenuStyle" parent="Base.ThemeOverlay.AppCompat.ActionBar">
<item name="android:textColorPrimary">@color/black</item>
<item name="android:gravity">left|center</item>
<item name="android:background">@color/white</item>
</style>
看起來就是這樣
如您所見,標題文本未與導航抽屜對齊
我也有同樣的問題,它通過使用解決
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
有關更多詳細信息,請閱讀https://code.google.com/p/android/issues/detail?id=77874上的問題。
我用這個,我放自己的TextView並設置其樣式:
<?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/tools"
android:id="@+id/materialToolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
app:shadowDrawable="@drawable/header_shadow"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
<!--app:theme declaration will make sure that your text and items are using solid colors-->
<TextView
android:id="@+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="0dp"
android:layout_marginStart="16dp"
android:layout_gravity="center"
android:text="..."
android:textColor="@color/white"
android:textSize="18sp"
android:focusable="false" />
</android.support.v7.widget.Toolbar>
然后在Activity
,我隱藏默認標題。 我也更改了字體:
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle(null);
toolbarTitle = (TextView) toolbar.findViewById(R.id.toolbar_title);
//toolbarTitle.setTypeface(((Itu) getApplication()).getToolbarTypeFace());
toolbarTitle.setText(R.string.title_activity_course_list);
我已經在工具欄小部件上具有以下內容:
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
我最終在工具欄樣式中添加了以下內容:
<item name="android:gravity">center_vertical</item>
這使我的標題正確地垂直居中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.