Android 如何在工具栏设置按钮中添加项目列表?
我已经创建了一个工具栏,其中包含三个东西Android 如何在工具栏设置按钮中添加项目列表?,android,android-layout,toolbar,android-toolbar,Android,Android Layout,Toolbar,Android Toolbar,我已经创建了一个工具栏,其中包含三个东西 用户化身 头衔 ic_动作_溢出 我想在ic_action_overflow中添加一些项目列表。当用户单击此列表时,下拉列表应打开 请查找activity.xml以供参考: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools=
- 用户化身
- 头衔
- ic_动作_溢出
<?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>
在工具栏溢出图标上添加项目的完美教程: 您也可以尝试使用本教程: 这应该行得通
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]);
}
Toolbar toolbr=(Toolbar)findviewbyd(R.id.Toolbar);
工具栏.充气菜单(右菜单.部分菜单);
String[]items=getResources().getStringArray(R.array.menu\u items);
对于(int i=0;i
您可以添加微调器而不是图像视图(即ic\u action\u overflow
)@Kathi谢谢。这是个好主意。