How to make the three dots appear in the Android toolbar

本小妞迷上赌 提交于 2019-12-12 22:20:04

问题


I am new in Android applications development, and I am trying to develop an application in UI I added a toolbar but In don't know why the three dots in the right side of the toolbar, it is necessary for me because I want to add a logout button there.

here is the toolbar layout:

 <?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>

And here is where i add the toolbar inside the activity class:

 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));

and this is my 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>

And what is weird is that in the android studio preview the three dots are shown:

what I am doing wrong ?


回答1:


Create Android Resource Directory of type menu in res folder and add xml file named:

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>

In your Activity:

@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);
    }
}



回答2:


The three dots were hidden because they were black, and the toolbar background is black, so I had to add this white theme

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

to my toolbar layout.




回答3:


In onCreateOptionmenu() one file will be inflating automatically. Remove that file and three dots will be removed. Or If this function is not included in your activity file then remove the file under res\menu folder.



来源:https://stackoverflow.com/questions/39546152/how-to-make-the-three-dots-appear-in-the-android-toolbar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!