Android - 弹出菜单不显示在菜单操作栏下方

时间:2023-01-16 14:18:53

I created a menu and popup menu like picture below (I get in internet and edit by paint), I don't know why it doesn't display normal http://i.imgur.com/jHPobCH.png. I want the interface like this http://i.imgur.com/08XomVV.png and moever, how do I change popup menu background color? I tried as below but not success. I test on Samsung Galaxy J and android 4.4.2.

我创建了一个菜单和弹出菜单,如下图所示(我上网并通过绘画编辑),我不知道为什么它不显示正常的http://i.imgur.com/jHPobCH.png。我想要这样的界面http://i.imgur.com/08XomVV.png和moever,如何更改弹出菜单背景颜色?我尝试如下,但没有成功。我测试三星Galaxy J和android 4.4.2。

MainActivity.java

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">


    <WebView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/webView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
</RelativeLayout>

menu_main.xml

<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=".MainActivity">
    <item android:id="@+id/action_filter"
        android:icon="@drawable/ic_filter_white_18dp"
        android:title="@string/action_filter"
        app:showAsAction="ifRoom" />
    <item android:id="@+id/action_label"
        android:icon="@drawable/ic_add_circle_outline_white_18dp"
        android:title="@string/action_label"
        app:showAsAction="ifRoom" />
    <item android:id="@+id/action_settings"
        android:title="@string/action_settings"
        app:showAsAction="never" />
</menu>
<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=".MainActivity">
    <item android:id="@+id/action_filter"
        android:icon="@drawable/ic_filter_white_18dp"
        android:title="@string/action_filter"
        app:showAsAction="ifRoom" />
    <item android:id="@+id/action_label"
        android:icon="@drawable/ic_add_circle_outline_white_18dp"
        android:title="@string/action_label"
        app:showAsAction="ifRoom" />
    <item android:id="@+id/action_settings"
        android:title="@string/action_settings"
        app:showAsAction="never" />
</menu>

styles.xml

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="android:background">#3079EB</item>
    <item name="android:popupMenuStyle">@style/PopupMenu</item>
</style>

<style name="PopupMenu" parent="Widget.AppCompat.Light.PopupMenu">
    <item name="android:popupBackground">@android:color/white</item>
</style>

P/S: I'm a beginner android development it

P / S:我是一个初学的android开发吧

1 个解决方案

#1


0  

Can you please post Your entire code here . From the first look what i can understand is the height of the layout where you are populating the popup menu is much high . So even after populating the four items , its having enough of empty space . Change the layout_height of the layout where you are inflating the popup to wrap-content instead of using some fixed height . I hope that will solve the issue .

你能在这里发布你的全部代码吗?从第一眼看,我可以理解的是,布局弹出菜单的布局高度非常高。所以即使在填充这四个项目之后,它也有足够的空白空间。将要弹出弹出窗口的布局的layout_height更改为wrap-content而不是使用某个固定高度。我希望能解决这个问题。

#1


0  

Can you please post Your entire code here . From the first look what i can understand is the height of the layout where you are populating the popup menu is much high . So even after populating the four items , its having enough of empty space . Change the layout_height of the layout where you are inflating the popup to wrap-content instead of using some fixed height . I hope that will solve the issue .

你能在这里发布你的全部代码吗?从第一眼看,我可以理解的是,布局弹出菜单的布局高度非常高。所以即使在填充这四个项目之后,它也有足够的空白空间。将要弹出弹出窗口的布局的layout_height更改为wrap-content而不是使用某个固定高度。我希望能解决这个问题。