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

7

我知道这个问题已经被问了很多次,我也尝试了很多解决方案,但是对我来说都不起作用。我想使用 Popup 来将我的 Menu 的方向改为 rtl,但是它不起作用。

我的 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" />

我的 弹出窗口

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"

你能具体说明一下你尝试过哪些方法,这样我们就不会推荐那些已经对你无效的东西了吗? - pushasha
@pushasha,请问您所说的“approach”的意思是什么? - Ahmed
你在问题中提到“我尝试了很多解决方案,但都不起作用”。我想问一下你指的“解决方案”是什么。此外,你能否展示一下你认为菜单不是从右到左排列的证据?是文本没有对齐吗? - pushasha
我使用了我在问题中提到的解决方案。然后我尝试像这样使用Gravity.RIGHTPopupMenu popup = new PopupMenu(MainActivity.this, imageButton , Gravity.RIGHT); 但是在这一行中,我必须将最小SDK设置为19,而我不想这样做。并且也不起作用。 - Ahmed
“Menu” xml怎么样?我可以在自定义布局中使用它吗? - Ahmed
显示剩余5条评论
1个回答

9
我找到了一个解决方案。 像这样制作自己的样式。
<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);

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