如何从右到左改变弹出菜单项的方向?

时间:2018-04-18 13:56:16

标签: android popupmenu

我知道这个问题被多次询问并且我使用了很多解决方案,但它对我不起作用。我想使用Menurtl的方向更改为Popup,但它不起作用。

我的menu

<menu xmlns:android="http://schemas.android.com/apk/res/android"
android:layoutDirection="rtl">
<item
    android:id="@+id/more"
    android:title="@string/menue" />
<item
    android:id="@+id/rate"
    android:title="@string/rate"/>
<item
    android:id="@+id/share"
    android:title="@string/share" />

我的popup

imageButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            PopupMenu popup = new PopupMenu(MainActivity.this, imageButton );
            popup.getMenuInflater().inflate(R.menu.app_menue, popup.getMenu());
            popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem menuItem) {
                    switch (menuItem.getItemId()) {
                        case R.id.more:
                            Intent intent = new Intent(Intent.ACTION_VIEW);
                            intent.setData(Uri.parse(res.getString(R.string.moreApps)));
                            startActivity(intent);
                            break;
                        case R.id.rate:
                            Intent intent1 = new Intent(Intent.ACTION_VIEW);
                            intent1.setData(Uri.parse(res.getString(R.string.rateApp)));
                            startActivity(intent1);
                            break;
                        case R.id.share:
                            Intent myIntent = new Intent(Intent.ACTION_SEND);
                            myIntent.setType("text/plain");
                            String shareBody = "Share our app";
                            myIntent.putExtra(Intent.EXTRA_SUBJECT, shareBody);
                            myIntent.putExtra(Intent.EXTRA_TEXT, res.getString(R.string.shareApp));
                            startActivity(Intent.createChooser(myIntent, "Share using"));
                            break;
                    }
                    return true;
                }
            });
            popup.show();
        }
    });

在我的Manifest

    android:supportsRtl="true"

1 个答案:

答案 0 :(得分:4)

我找到了解决方案。

制作我自己的风格

<style name="menuStyle">
<item name="android:layoutDirection">rtl</item>
</style>

然后添加此代码

Context myContext = new ContextThemeWrapper(MainActivity.this,R.style.menuStyle); 
PopupMenu popupMenu = new PopupMenu(myContext , myView);