问题描述
这是 Google 地图工具栏的屏幕截图.
Here is a screenshot of Google Maps Toolbar.
如您所见,图标是左对齐而不是右对齐(默认行为).我已经尝试将 android:layout_gravity="left" 和 android:gravity="left" 添加到工具栏,但没有用.我还尝试向工具栏添加内部 LinearLayout(具有相同的重力值),但这也不起作用.有任何想法吗?
As you can see icons are aligned to the left instead of right (default behavior). I've tried adding android:layout_gravity="left" and android:gravity="left" to the toolbar but it didn't work. I also tried adding an internal LinearLayout (with same gravity values) to the Toolbar but that didn't work either. Any ideas?
我希望能够使用带有工具栏小部件的常规 Android 菜单,而不是从头开始重新创建所有内容.
I want to be able to use a regular Android menu with the Toolbar widget instead of recreating everything from scratch.
推荐答案
在对 Android 工具栏代码进行了一些努力和挖掘之后,我设法让它工作了.基本上,这个想法是添加一个新的 android.support.v7.widget.ActionMenuView 作为 Toolbar 的子项,将其重力设置为 top|start,然后将菜单添加到您的 Activity 中的该操作菜单视图.代码如下:
After some struggling and digging in Android Toolbar code I managed to make it work. Basically, the idea is to add a new android.support.v7.widget.ActionMenuView as child of the Toolbar, set its gravity to top|start, and then add the menu to that action menu view in your Activity. Here is the code:
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/tToolbar"
android:layout_height="?attr/actionBarSize"
android:layout_width="match_parent"
android:background="?attr/colorPrimary"
android:gravity="center_vertical|start"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
<android.support.v7.widget.ActionMenuView
android:id="@+id/amvMenu"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"/>
</android.support.v7.widget.Toolbar>
my_activity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--Toolbar-->
<include
android:id="@+id/tToolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
layout="@layout/my_toolbar" />
</RelativeLayout>
MyActivity.java
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.ActionMenuView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
public final class MyActivity extends ActionBarActivity {
private ActionMenuView amvMenu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// this layout includes the custom toolbar my_toolbar.xml
setContentView(R.layout.my_activity);
Toolbar t = (Toolbar) findViewById(R.id.tToolbar);
amvMenu = (ActionMenuView) t.findViewById(R.id.amvMenu);
amvMenu.setOnMenuItemClickListener(new ActionMenuView.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem menuItem) {
return onOptionsItemSelected(menuItem);
}
});
setSupportActionBar(t);
getSupportActionBar().setTitle(null);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
// use amvMenu here
inflater.inflate(R.menu.my_activity_menu, amvMenu.getMenu());
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Do your actions here
return true;
}
}
这篇关于如何像在 Google 地图应用中一样将 Android 工具栏菜单/图标向左对齐?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!