其他分享
首页 > 其他分享> > android – ActionBar项目始终显示在溢出菜单中

android – ActionBar项目始终显示在溢出菜单中

作者:互联网

我正在尝试在操作栏中添加操作项,但项目总是添加到溢出列表中,即使有很多空间.有什么建议来解决这个问题?

enter image description here

这应该是这样的

enter image description here

main.xml中

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

    <item
        android:id="@+id/action_refresh"
        android:icon="@drawable/ic_action_refresh"
        android:showAsAction="ifRoom"
        android:title="@string/action_refresh" />

    <item
        android:id="@+id/action_search"
        android:icon="@drawable/ic_action_search"
        android:showAsAction="ifRoom"
        android:title="@string/action_search" />

    <item
        android:id="@+id/action_settings"
        android:title="@string/action_settings"
        app:showAsAction="never" />


</menu>

MainActivity.java

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

AndroidManifest.xml中

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.actionbardemo.app.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

的build.gradle

apply plugin: 'android'

android {
    compileSdkVersion 19
    buildToolsVersion "19.0.3"

    defaultConfig {
        minSdkVersion 8
        targetSdkVersion 19
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
}

dependencies {
    compile 'com.android.support:support-v4:19.0.1'
    compile 'com.android.support:appcompat-v7:19.0.1'
    compile fileTree(dir: 'libs', include: ['*.jar'])
}

解决方法:

我的应用程序使用支持库来兼容低至Android 2.2的版本,所以我必须定义自己的XML命名空间并使用该命名空间作为属性前缀

解决方案就是这样,我正在编写这个android:showAsAction =“ifRoom”但我应该有app:showAsAction =“ifRoom”

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

    <item
        android:id="@+id/action_refresh"
        android:icon="@drawable/ic_action_refresh"
        app:showAsAction="ifRoom"
        android:title="@string/action_refresh" />

    <item
        android:id="@+id/action_search"
        android:icon="@drawable/ic_action_search"
        app:showAsAction="ifRoom"
        android:title="@string/action_search" />

    <item
        android:id="@+id/action_settings"
        android:title="@string/action_settings"
        app:showAsAction="never" />


</menu>

标签:android,android-actionbar,android-appcompat
来源: https://codeday.me/bug/20191006/1861833.html