How to change the direction of a popup menu Item from right to left?

杀马特。学长 韩版系。学妹 提交于 2020-07-08 20:48:09

问题


I know this question was asked many times and I used many solutions but it doesn't work for me . I want to change the direction of my Menu to rtl using a Popup but It doesn't work .

my 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" />

my 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();
        }
    });

And in my Manifest

    android:supportsRtl="true"

回答1:


I found a solution .

Making my own style like this

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

Then add this code

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


来源:https://stackoverflow.com/questions/49901594/how-to-change-the-direction-of-a-popup-menu-item-from-right-to-left

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!