我正在尝试制作一个包含两个项目的菜单,“退出”和“设置”,其中“退出”项目位于操作栏上,而“设置”项目位于溢出菜单中。
然而,当我点击菜单图标时,溢出菜单与“退出”项重叠。无论如何我可以阻止这种行为吗?
This is what's currently happening
This is what I'm trying to achive
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
}
答案 0 :(得分: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>