简体   繁体   English

使用Appcompat v7库时,ActionBar中未显示溢出图标

[英]Overflow icon not showing in ActionBar while using appcompat v7 library

ok...i have tried all the stackoverflow solutions. 好的...我已经尝试了所有的stackoverflow解决方案。 None of them seems to work. 他们似乎都不起作用。

Manifest 表现

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.firstapp"
    android:versionCode="1"
    android:versionName="1.0" >

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name="com.example.firstapp.DisplayMessageActivity"
            android:label="@string/title_activity_display_message"
            android:parentActivityName="com.example.firstapp.MainActivity"
            android:theme="@style/Theme.AppCompat.Light" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.example.firstapp.MainActivity" />
        </activity>
        <activity
            android:name="com.example.firstapp.MainActivity"
            android:label="@string/app_name"
            android:theme="@style/Theme.AppCompat.Light" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Inside /res/menu/main_activity_actions.xml /res/menu/main_activity_actions.xml内部

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:yourapp="http://schemas.android.com/apk/res-auto" >

    <item
        android:id="@+id/action_search"
        android:icon="@drawable/ic_action_search"
        android:title="@string/title_search"
        yourapp:showAsAction="ifRoom"/>
    <item
        android:id="@+id/action_share"
        android:icon="@drawable/ic_action_share"
        android:title="@string/title_share"
        yourapp:showAsAction="ifRoom"
        />
    <item
        android:id="@+id/action_settings"
        android:icon="@drawable/ic_action_settings"
        android:title="@string/title_settings"
        yourapp:showAsAction="ifRoom"/>
</menu>

Inside main activity 内部主要活动

public boolean onCreateOptionsMenu(Menu menu)
    {
        getMenuInflater().inflate(R.menu.main_activity_actions, menu);
        return super.onCreateOptionsMenu(menu);
    }

Here is the screen shot. 这是屏幕截图。

Vertical Screen 垂直屏幕

在此处输入图片说明

Horizontal Screen 横屏

在此处输入图片说明

* *

According to me the last icon in ActionBar should automatically go in overflow as stated in the google document. 据我说,ActionBar中的最后一个图标应该自动溢出,如google文档中所述。 However, its not happening. 但是,它没有发生。 Whats wrong here ? 这是怎么了

* *

Add this code in onCreate method of those activities in which you want to show icon- 将此代码添加到您要在其中显示图标的那些活动的onCreate方法中

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
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM