Android 操作栏中的“选项”菜单显示选项过高
我想要在点击垂直三点后弹出的菜单,以显示从操作栏下方开始的菜单,而不是如图所示与之对齐。我该怎么做 默认选项菜单: 选项菜单的预期位置: 以下是我在活动中的代码:Android 操作栏中的“选项”菜单显示选项过高,android,android-studio,android-layout,kotlin,Android,Android Studio,Android Layout,Kotlin,我想要在点击垂直三点后弹出的菜单,以显示从操作栏下方开始的菜单,而不是如图所示与之对齐。我该怎么做 默认选项菜单: 选项菜单的预期位置: 以下是我在活动中的代码: override fun onCreateOptionsMenu(menu: Menu): Boolean { // Inflate the menu; this adds items to the action bar if it is present. menuInflater.infl
override fun onCreateOptionsMenu(menu: Menu): Boolean {
// Inflate the menu; this adds items to the action bar if it is present.
menuInflater.inflate(R.menu.channelmenu, menu)
return super.onCreateOptionsMenu(menu)
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
// Handle presses on the action bar menu items
when (item.itemId) {
R.id.logoutButton -> {
logoutClick()
return true
}
else -> {
return super.onOptionsItemSelected(item)
}
}
}
布局中的代码:
<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="com.example.treechat.ChannelListActivity">
<item
android:id="@+id/logoutButton"
android:title="Logout"
app:showAsAction="collapseActionView"/>
<item
android:id="@+id/item2"
android:title="Item 2"
app:showAsAction="collapseActionView"/>
</menu>
使用
actionOverflowMenuStyle
并在styles.xml中的AppTheme
中放置一个自定义样式,这样您就可以将overlapAnchor
设置为false
并为其设置偏移量:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="actionOverflowMenuStyle">@style/OptionsMenuCustomStyle</item>
</style>
<style name="OptionsMenuCustomStyle" parent="Widget.AppCompat.PopupMenu.Overflow">
<item name="overlapAnchor">false</item>
<item name="android:overlapAnchor" tools:ignore="NewApi">false</item>
<item name="android:dropDownVerticalOffset">4.0dip</item>
</style>
@样式/选项菜单自定义样式
假的
假的
4.0dip
输出与您的要求完全相同:
使用actionOverflowMenuStyle
并在styles.xml中的AppTheme
中放置一个自定义样式,这样您可以将overlapAnchor
设置为false
,并为其设置偏移量:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="actionOverflowMenuStyle">@style/OptionsMenuCustomStyle</item>
</style>
<style name="OptionsMenuCustomStyle" parent="Widget.AppCompat.PopupMenu.Overflow">
<item name="overlapAnchor">false</item>
<item name="android:overlapAnchor" tools:ignore="NewApi">false</item>
<item name="android:dropDownVerticalOffset">4.0dip</item>
</style>
@样式/选项菜单自定义样式
假的
假的
4.0dip
输出与您的要求完全相同: