将菜单项放置在操作栏的左端
Position a menu item at the left end of the actionbar
如何将菜单图标移动到栏的左端,应用程序名称的左侧,如图所示?
有没有办法只通过 xml 来做到这一点,而不添加 java 代码?
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.robyn.myapplication.MainActivity">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:background="@drawable/gradient_bg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/appbar_padding_top"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/AppTheme.PopupOverlay">
</android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TabItem
android:id="@+id/tab1"
android:icon="@drawable/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<android.support.design.widget.TabItem
android:id="@+id/tab2"
android:icon="@drawable/favo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<android.support.design.widget.TabItem
android:id="@+id/tab3"
android:icon="@drawable/hour"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</android.support.design.widget.TabLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@android:drawable/ic_dialog_email"/>
</android.support.design.widget.CoordinatorLayout>
menu_main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.robyn.myapplication.MainActivity">
<item
android:id="@+id/action_settings"
android:icon="@drawable/ic_menu_white_24dp"
android:orderInCategory="0"
android:title="@string/action_settings"
app:showAsAction="ifRoom"/>
</menu>
根据@Martin De Simone
,如果你把图片放在toolbar
标签里,会留下一个小的space,不会在最左边。如果你对此没问题,你可以像下面那样做,
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/AppTheme.PopupOverlay">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="left|start">
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="@drawable/ic_dehaze"
android:contentDescription="@string/logo"/>
</LinearLayout>
</android.support.v7.widget.Toolbar>
还有更多可能的解决方案。
- 这有点像 hack,但如果您的目标是
API
17 级及以上,您可以设置 `layoutDirection="rtl"。 您必须相应地设置您的标题方向
您必须使用 Java
代码来设置 toolbar
,向其中添加更多行。
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
if(getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
toolbar.setNavigationIcon(R.drawable.ic_dehaze); //your icon
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Do whatever you want to do here
}
});
希望对您有所帮助
`layoutDirection="rtl" 这对我有帮助
如何将菜单图标移动到栏的左端,应用程序名称的左侧,如图所示? 有没有办法只通过 xml 来做到这一点,而不添加 java 代码?
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.robyn.myapplication.MainActivity">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:background="@drawable/gradient_bg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/appbar_padding_top"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/AppTheme.PopupOverlay">
</android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TabItem
android:id="@+id/tab1"
android:icon="@drawable/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<android.support.design.widget.TabItem
android:id="@+id/tab2"
android:icon="@drawable/favo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<android.support.design.widget.TabItem
android:id="@+id/tab3"
android:icon="@drawable/hour"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</android.support.design.widget.TabLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@android:drawable/ic_dialog_email"/>
</android.support.design.widget.CoordinatorLayout>
menu_main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.robyn.myapplication.MainActivity">
<item
android:id="@+id/action_settings"
android:icon="@drawable/ic_menu_white_24dp"
android:orderInCategory="0"
android:title="@string/action_settings"
app:showAsAction="ifRoom"/>
</menu>
根据@Martin De Simone
,如果你把图片放在toolbar
标签里,会留下一个小的space,不会在最左边。如果你对此没问题,你可以像下面那样做,
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/AppTheme.PopupOverlay">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="left|start">
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="@drawable/ic_dehaze"
android:contentDescription="@string/logo"/>
</LinearLayout>
</android.support.v7.widget.Toolbar>
还有更多可能的解决方案。
- 这有点像 hack,但如果您的目标是
API
17 级及以上,您可以设置 `layoutDirection="rtl"。 您必须相应地设置您的标题方向 您必须使用
Java
代码来设置toolbar
,向其中添加更多行。Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); if(getSupportActionBar() != null) { getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setHomeButtonEnabled(true); getSupportActionBar().setDisplayShowHomeEnabled(true); } toolbar.setNavigationIcon(R.drawable.ic_dehaze); //your icon toolbar.setNavigationOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //Do whatever you want to do here } });
希望对您有所帮助
`layoutDirection="rtl" 这对我有帮助