我正在尝试添加actionbar演示。我为此使用了紧凑的v7应用程序,我需要在7到18个api级别上对此进行支持。我尝试了很多解决方案,但我无法获得适合我演示的合适解决方案。
问题是溢出菜单在Api级别的底部显示在 13。我需要在Top vesrion中显示所有内容。

此图像是API级别2.3。这是我所需要的完美。但是当我尝试在api级别

此图像是API级别4.0



Java代码

    @SuppressLint("NewApi")
    public class ActionBarMain extends ActionBarActivity implements TabListener {
        RelativeLayout rl;
        ActionBarDrawerToggle mDrawerToggle;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            supportRequestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
            setContentView(R.layout.activity_action_bar_main);

            try {
                rl = (RelativeLayout) findViewById(R.id.mainLayout);

                fragMentTra = this.getSupportFragmentManager().beginTransaction();

                ActionBar bar = getSupportActionBar();
                bar.setIcon(R.drawable.ic_action_previous_item);
                // getSupportActionBar().setTitle(Html.fromHtml("<font color='#ffffff'>test1</font>"));

                Drawable d = getResources().getDrawable(R.drawable.top_bar);
                getSupportActionBar().setBackgroundDrawable(d);
                bar.addTab(bar.newTab().setText("testTab1").setTabListener(this));
                bar.addTab(bar.newTab().setText("testTab2").setTabListener(this));
                bar.addTab(bar.newTab().setText("testTab3").setTabListener(this));
                bar.addTab(bar.newTab().setText("testTab4").setTabListener(this));

                bar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM
                        | ActionBar.DISPLAY_USE_LOGO);
                bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
                bar.setDisplayShowHomeEnabled(true);
                bar.setDisplayShowTitleEnabled(true);
                bar.show();

            } catch (Exception e) {
                e.getMessage();
            }

        }

        FragMent1 fram1;
        FragmentTransaction fragMentTra = null;
        FragMent2 fram2;
        FragMent3 fram3;
        FragMent4 fram4;

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // getMenuInflater().inflate(R.menu.activity_action_bar_main, menu);
            MenuInflater inflater = getMenuInflater();
            inflater.inflate(R.menu.activity_action_bar_main, menu);

            return true;
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // TODO Auto-generated method stub

            View menuItemView = findViewById(R.id.item_menu_overflow);

            PopupMenu popupMenu = new PopupMenu(this, menuItemView);
            popupMenu.inflate(R.menu.popup);

            popupMenu.setOnMenuItemClickListener(new OnMenuItemClickListener() {

                @Override
                public boolean onMenuItemClick(MenuItem arg0) {
                    // TODO Auto-generated method stub

                    return false;
                }

            });

            popupMenu.show();

            return true;

        }

        @Override
        public void onTabReselected(Tab tab,
                android.support.v4.app.FragmentTransaction ft) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onTabSelected(Tab tab,
                android.support.v4.app.FragmentTransaction ft) {
            // TODO Auto-generated method stub
            if (tab.getText().equals("Home")) {

                try {
                    rl.removeAllViews();
                } catch (Exception e) {
                }
                fram1 = new FragMent1();
                fragMentTra.addToBackStack(null);
                fragMentTra = this.getSupportFragmentManager().beginTransaction();
                // fragMentTra.add(rl.getId(), fram1);
                fragMentTra.commit();
            } else if (tab.getText().equals("Post")) {
                try {
                    rl.removeAllViews();
                } catch (Exception e) {
                }
                fram2 = new FragMent2();
                fragMentTra.addToBackStack(null);
                fragMentTra = this.getSupportFragmentManager().beginTransaction();
                fragMentTra.add(rl.getId(), fram2);
                fragMentTra.commit();
            } else if (tab.getText().equals("Filter")) {
                try {
                    rl.removeAllViews();
                } catch (Exception e) {
                }
                fram3 = new FragMent3();
                fragMentTra.addToBackStack(null);
                fragMentTra = this.getSupportFragmentManager().beginTransaction();
                // fragMentTra.add(rl.getId(), fram3);
                fragMentTra.commit();
            } else if (tab.getText().equals("GPS")) {
                try {
                    rl.removeAllViews();
                } catch (Exception e) {
                }
                fram4 = new FragMent4();
                fragMentTra.addToBackStack(null);
                fragMentTra = this.getSupportFragmentManager().beginTransaction();

                // fragMentTra.add();
                fragMentTra.commit();
            }
        }

        @Override
        public void onTabUnselected(Tab tab,
                android.support.v4.app.FragmentTransaction ft) {
            // TODO Auto-generated method stub

        }

    }


Menu.xml

    <?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:yourapp="http://schemas.android.com/apk/res-auto" >

        <item
            android:id="@+id/item_menu_overflow"
            android:icon="@drawable/ic_action_overflow"

               android:orderInCategory="100"

             yourapp:showAsAction="ifRoom"
             android:showAsAction="ifRoom"
           >
        </item>

    </menu>


AndroidMainFrest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.ahmad.actionBar"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

    <meta-data
        android:name="android.support.UI_OPTIONS"
        android:value="splitActionBarWhenNarrow" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/Theme.AppCompat.Light"
        android:uiOptions="splitActionBarWhenNarrow" >
        <activity
            android:name=".ActionBarMain"
            android:label="@string/title_activity_action_bar_main"
            android:screenOrientation="portrait"
            android:uiOptions="splitActionBarWhenNarrow" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

最佳答案

这将迫使溢出按钮在所有设备上显示,主要问题是,如果有可用的硬件按钮,则不会显示溢出按钮。

    try {
        ViewConfiguration config = ViewConfiguration.get(this);
        Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
        if(menuKeyField != null) {
            menuKeyField.setAccessible(true);
            menuKeyField.setBoolean(config, false);
        }
    } catch (Exception ex) {
        // Ignore
    }


但是使用此代码无论如何都应显示。

关于java - 使用V7 App Compat,Actionbar Overflow菜单没有显示在<13 API级别的顶部和orid中吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20212054/

10-10 05:13