如何在工具栏设置按钮中添加项目列表?
How to add list of items in toolbar setting button?
我创建了一个包含三个东西的工具栏。
- 用户头像
- 标题
- ic_action_overflow
我想在 ic_action_overflow.When 中添加一些项目列表,用户点击此下拉列表应该会打开。
请参考activity.xml:
<?xml version="1.0" encoding="utf-8"?>
<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"
android:background="#ffffff"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.example.vimal.edkul.StudentProfile">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/layout1">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/red">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:src="@drawable/pic13" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="User Profile"
android:textSize="20sp"
android:textColor="@android:color/white" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:src="@drawable/ic_action_overflow" />
</android.support.v7.widget.Toolbar>
</LinearLayout>
</RelativeLayout>
在工具栏溢出图标上添加项目的完美教程:
您也可以尝试使用本教程:
http://www.techotopia.com/index.php/Creating_and_Managing_Overflow_Menus_on_Android
这应该有效
Toolbar toolbr = (Toolbar) findViewById(R.id.toolbar);
toolbar.inflateMenu(R.menu.some_menu);
String[] items = getResources().getStringArray(R.array.menu_items);
for (int i = 0; i < items.length; i++) {
toolbar.getMenu().add(items[i]);
}
我创建了一个包含三个东西的工具栏。
- 用户头像
- 标题
- ic_action_overflow
我想在 ic_action_overflow.When 中添加一些项目列表,用户点击此下拉列表应该会打开。
请参考activity.xml:
<?xml version="1.0" encoding="utf-8"?>
<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"
android:background="#ffffff"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.example.vimal.edkul.StudentProfile">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/layout1">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/red">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:src="@drawable/pic13" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="User Profile"
android:textSize="20sp"
android:textColor="@android:color/white" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:src="@drawable/ic_action_overflow" />
</android.support.v7.widget.Toolbar>
</LinearLayout>
</RelativeLayout>
在工具栏溢出图标上添加项目的完美教程:
您也可以尝试使用本教程:
http://www.techotopia.com/index.php/Creating_and_Managing_Overflow_Menus_on_Android
这应该有效
Toolbar toolbr = (Toolbar) findViewById(R.id.toolbar);
toolbar.inflateMenu(R.menu.some_menu);
String[] items = getResources().getStringArray(R.array.menu_items);
for (int i = 0; i < items.length; i++) {
toolbar.getMenu().add(items[i]);
}