Android Material 设计工具栏自定义操作菜单未正确对齐
Android Material design toolbar custom action menu is not aligned correctly
我刚刚在我的应用程序中采用了 Material 设计工具栏。我已按照 Chris Banes blog 的指导在 pre-Lollipop 设备上运行
但是,我似乎无法让我的操作菜单项垂直居中。
这是它在 Kitkat 设备上的截图。
如您所见,标题和菜单图标未对齐。标题以某种方式正确对齐,但操作菜单或导航图标均未正确对齐。它们与底部对齐。
这是我的工具栏布局
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:background="?attr/colorPrimaryDark"/>
我尝试将 android:gravity 放在布局上,但它没有任何作用。
这是我的摘要 activity 代码,将工具栏指定为操作栏
public abstract class BaseMaterialActivity extends ActionBarActivity {
private Toolbar toolbar;
public abstract int getLayoutResources();
public abstract boolean isShowActionBar();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayoutResources());
if (isShowActionBar()) {
toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar != null) {
setSupportActionBar(toolbar);
toolbar.setNavigationIcon(getResources().getDrawable(R.drawable.ic_drawer));
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_nav_back);
}
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
}
}
public Toolbar getToolbar() {
return toolbar;
}
}
这是我的menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_filter"
android:icon="@drawable/ic_filter"
android:orderInCategory="1"
app:showAsAction="always|withText"
android:title="@string/filters" />
<item
android:id="@+id/action_cart"
app:actionLayout="@layout/action_cart_icon_layout"
android:orderInCategory="2"
app:showAsAction="always"
android:title="@string/cart" />
</menu>
任何关于如何使操作菜单垂直居中的帮助或指示将不胜感激。谢谢!
而不是
android:layout_height="?android:attr/actionBarSize"
写
android:layout_height="?actionBarSize"
第二个由具有正确高度的 AppCompat 主题定义(默认 56dp,正常横向 48dp,平板电脑 64dp)。
注意:您编写的内容只能在 Android 5.0.
上正常运行
我刚刚在我的应用程序中采用了 Material 设计工具栏。我已按照 Chris Banes blog 的指导在 pre-Lollipop 设备上运行 但是,我似乎无法让我的操作菜单项垂直居中。
这是它在 Kitkat 设备上的截图。
如您所见,标题和菜单图标未对齐。标题以某种方式正确对齐,但操作菜单或导航图标均未正确对齐。它们与底部对齐。
这是我的工具栏布局
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:background="?attr/colorPrimaryDark"/>
我尝试将 android:gravity 放在布局上,但它没有任何作用。
这是我的摘要 activity 代码,将工具栏指定为操作栏
public abstract class BaseMaterialActivity extends ActionBarActivity {
private Toolbar toolbar;
public abstract int getLayoutResources();
public abstract boolean isShowActionBar();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayoutResources());
if (isShowActionBar()) {
toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar != null) {
setSupportActionBar(toolbar);
toolbar.setNavigationIcon(getResources().getDrawable(R.drawable.ic_drawer));
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_nav_back);
}
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
}
}
public Toolbar getToolbar() {
return toolbar;
}
}
这是我的menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_filter"
android:icon="@drawable/ic_filter"
android:orderInCategory="1"
app:showAsAction="always|withText"
android:title="@string/filters" />
<item
android:id="@+id/action_cart"
app:actionLayout="@layout/action_cart_icon_layout"
android:orderInCategory="2"
app:showAsAction="always"
android:title="@string/cart" />
</menu>
任何关于如何使操作菜单垂直居中的帮助或指示将不胜感激。谢谢!
而不是
android:layout_height="?android:attr/actionBarSize"
写
android:layout_height="?actionBarSize"
第二个由具有正确高度的 AppCompat 主题定义(默认 56dp,正常横向 48dp,平板电脑 64dp)。
注意:您编写的内容只能在 Android 5.0.
上正常运行