操作栏标题未正确对齐,android?
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>
这是它的样子
如您所见,标题文本未与导航抽屉对齐
我用这个,我把我自己的 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"
有关更多详细信息,请阅读问题
https://code.google.com/p/android/issues/detail?id=77874
我的工具栏小部件上已经有以下内容:
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
我最终将以下内容添加到工具栏样式中:
<item name="android:gravity">center_vertical</item>
这使我的标题能够正确地垂直居中。
我在我的应用程序中使用工具栏
这是我的代码
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>
这是它的样子
如您所见,标题文本未与导航抽屉对齐
我用这个,我把我自己的 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"
有关更多详细信息,请阅读问题 https://code.google.com/p/android/issues/detail?id=77874
我的工具栏小部件上已经有以下内容:
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
我最终将以下内容添加到工具栏样式中:
<item name="android:gravity">center_vertical</item>
这使我的标题能够正确地垂直居中。