Android溢出菜单覆盖在操作栏上

3
我正在尝试制作一个菜单,其中包括“退出”和“设置”两个选项。其中,“退出”项位于操作栏上,而“设置”项位于溢出菜单中。
然而,当我点击菜单图标时,溢出菜单会覆盖“退出”项。有没有办法可以防止这种行为发生?
此处是目前的情况:链接 此处是我想要实现的结果:链接 代码如下:Menue.XML
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <group android:checkableBehavior="single">
        <item android:title="@string/setting"
            android:id="@+id/setting"
            android:orderInCategory="2"
            app:showAsAction="never"
        />

        <item android:title="@string/exit"
            android:id="@+id/exit"
            android:orderInCategory="1"
            app:showAsAction="withText|ifRoom"

            android:icon="@drawable/exit"
        />
    </group>
</menu>

Activity_main.XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.meunueexample.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</RelativeLayout>

Main_Activity.Java

package com.example.meunueexample;

import android.app.*;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) { // it wil inflate the menue in the action bar
        getMenuInflater().inflate(R.menu.main, menu);

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) { // here we will get the selected item form group option
        int id = item.getItemId(); // will get id of selected item these IDs are not user defined
        if (id == R.id.setting) {

            android.app.DialogFragment myfragment= new DialogFragment(); // made object of dialog fragment
            myfragment.show(getFragmentManager() , "thedialog"); // thedialog is object created in DialogFragment class

            return true;
        } else if (id == R.id.exit) {
            finish();

            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

Dialog_Fragment.Java

package com.example.meunueexample;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.Toast;

/**
 * Created by AQ on 9/10/2017.
 */
public class DialogFragment extends android.app.DialogFragment {
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) { // override this method to ake dialog box
        AlertDialog.Builder thedialog = new AlertDialog.Builder(getActivity()); // jiss activity ka oper is na show hona hy
        thedialog.setTitle("Sample Dialog");
        thedialog.setMessage("Hlw AQ");

        thedialog.setPositiveButton("OK", new DialogInterface.OnClickListener() { // on click listener on button clicked
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(getActivity(), "Clicked OK", Toast.LENGTH_SHORT).show();
            }
        }); // place semicolon there
        thedialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { // on click listener on button clicked
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(getActivity(), "Clicked Cancel", Toast.LENGTH_SHORT).show();
            }
        }); // place semicolon there

        return thedialog.create();
    } // may change abndroid version from android manifest and from build.gradle
}

这不是Android Studio的问题,所以请不要使用Android Studio标签。 - Zoe stands with Ukraine
提醒一下,"menu" 拼写时末尾不需要加上 "e"。 - Pac0
请在第一条语句/段落中添加一个简单而清晰的总结,说明您想要实现或需要帮助的内容。 - Rafael Gorski
1个回答

0

您可以通过创建一个具有垂直偏移量的样式来更改这种行为,如下所示:

<style name="OverflowMenu" parent="Widget.AppCompat.PopupMenu.Overflow">
    <!-- Required for pre-Lollipop. -->
    <item name="overlapAnchor">false</item>
    <item name="android:dropDownVerticalOffset">-4.0dip</item>
    <!-- Required for Lollipop. -->
    <item name="android:overlapAnchor">false</item>
    <item name="android:dropDownVerticalOffset">4.0dip</item>
</style>

然后您可以在主题中应用此样式:

<item name="actionOverflowMenuStyle">@style/OverflowMenu</item>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接