如何使三个点出现在Android工具栏中

时间:2016-09-17 11:14:22

标签: android android-toolbar

我是Android应用程序开发的新手,我正在尝试在UI中开发一个应用程序我添加了一个工具栏但是我不知道为什么工具栏右侧的三个点,这对我来说是必要的因为我想在那里添加一个注销按钮。

enter image description here

这是工具栏布局:

 <?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="wrap_content"
    android:background="#000">

</android.support.v7.widget.Toolbar>

这里是我在活动类中添加工具栏的地方:

 toolbar= (Toolbar) findViewById(R.id.app_bar);
    setSupportActionBar(toolbar);
    toolbar.setTitle(R.string.app_name);
    toolbar.setTitleTextColor(getResources().getColor(R.color.com_facebook_button_background_color_focused));

这是我的menu.xml:

<?xml version="1.0" encoding="utf-8"?>
<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=".HomeActivity">


    <item android:id="@+id/action_settings"

        android:title="Settings"
        android:orderInCategory="100"
        app:showAsAction="always"
        android:icon="@drawable/icon"
        />

    <item
        android:id="@+id/action_search"
        android:title="Search">
        </item>
</menu>

奇怪的是,在android studio预览中显示了三个点:

enter image description here

我做错了什么?

3 个答案:

答案 0 :(得分:1)

Android Resource Directory文件夹中创建menu类型的res,并添加名为的xml文件:

user_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/logout_menu"
        android:orderInCategory="100"
        android:title="@string/action_logout"
        app:showAsAction="never" />
</menu>

在您的活动中:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.user_menu, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.logout_menu:
            // Do whatever you want to do on logout click.
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

答案 1 :(得分:1)

这三个点是隐藏的,因为它们是黑色的,工具栏背景是黑色的,所以我不得不添加这个白色主题

  android:theme="@style/ThemeOverlay.AppCompat.Dark"

到我的工具栏布局。

答案 2 :(得分:0)

onCreateOptionmenu()中,一个文件会自动充气。删除该文件,将删除三个点。 要么 如果活动文件中未包含此功能,请删除res \ menu文件夹下的文件。

相关问题