动作条项始终出现在溢出菜单

动作条项始终出现在溢出菜单

本文介绍了动作条项始终出现在溢出菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想加入行动吧行动项目,但项目始终被添加到溢出列表中仍存在很大空间。任何建议来解决这个??

这应该是这样的。

main.xml中

 <菜单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:程序=htt​​p://schemas.android.com/apk/res-auto>

    <项目
        机器人:ID =@ + ID / action_refresh
        机器人:图标=@可绘制/ ic_action_refresh
        机器人:showAsAction =ifRoom
        机器人:标题=@字符串/ action_refresh/>

    <项目
        机器人:ID =@ + ID / ACTION_SEARCH
        机器人:图标=@可绘制/ ic_action_search
        机器人:showAsAction =ifRoom
        机器人:标题=@字符串/ ACTION_SEARCH/>

    <项目
        机器人:ID =@ + ID / action_settings
        机器人:标题=@字符串/ action_settings
        应用程序:showAsAction =从不/>


< /菜单>
 

MainActivity.java

 公共类MainActivity扩展ActionBarActivity {

    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);
    }


    @覆盖
    公共布尔onCreateOptionsMenu(功能菜单){

        //充气菜单;这增加了项目操作栏,如果它是present。
        。getMenuInflater()膨胀(R.menu.main,菜单);
        返回true;
    }

}
 

AndroidManifest.xml中

 < XML版本=1.0编码=UTF-8&GT?;
<舱单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    包=com.example.actionbardemo.app>

    <应用
        机器人:allowBackup =真
        机器人:图标=@可绘制/ ic_launcher
        机器人:标签=@字符串/ APP_NAME
        机器人:主题=@风格/ AppTheme>
        <活动
            机器人:名称=com.example.actionbardemo.app.MainActivity
            机器人:标签=@字符串/ APP_NAME>
            <意向滤光器>
                <作用机器人:名称=android.intent.action.MAIN/>

                <类机器人:名称=android.intent.category.LAUNCHER/>
            &所述; /意图滤光器>
        < /活性GT;
    < /用途>

< /舱单>
 

build.gradle

 应用插件:'机器人'

安卓{
    compileSdkVersion 19
    buildToolsVersion19.0.3

    defaultConfig {
        的minSdkVersion 8
        targetSdkVersion 19
        版本code 1
        VERSIONNAME1.0
    }
    buildTypes {
        推出 {
            runProguard假
            proguardFiles getDefaultProguardFile('ProGuard的-android.txt'),'ProGuard的-rules.txt
        }
    }
}

依赖{
    编译com.android.support:support-v4:19.0.1
    编译com.android.support:appcompat-v7:19.0.1
    编译文件树(导演:库,包括:['的* .jar'])
}
 

解决方案

我的应用程序是使用支持库的兼容性的版本低了Android 2.2,所以我必须定义自己的XML命名空间,并使用该命名空间的属性preFIX

解决方案是这样的,我写这个安卓showAsAction =ifRoom,但我应该有应用程序:showAsAction =ifRoom

 <菜单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:程序=htt​​p://schemas.android.com/apk/res-auto>

    <项目
        机器人:ID =@ + ID / action_refresh
        机器人:图标=@可绘制/ ic_action_refresh
        应用程序:showAsAction =ifRoom
        机器人:标题=@字符串/ action_refresh/>

    <项目
        机器人:ID =@ + ID / ACTION_SEARCH
        机器人:图标=@可绘制/ ic_action_search
        应用程序:showAsAction =ifRoom
        机器人:标题=@字符串/ ACTION_SEARCH/>

    <项目
        机器人:ID =@ + ID / action_settings
        机器人:标题=@字符串/ action_settings
        应用程序:showAsAction =从不/>


< /菜单>
 

I am trying to add actions item in action bar, but items always are adding into the overflow list even there is lot of room. Any suggestion to resolve this ??

This should be like this

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'])
}
解决方案

My app is using the Support Library for compatibility on versions as low as Android 2.2 so I have to define my own XML namespace and use that namespace as the attribute prefix

Solution was this, I was writing this android:showAsAction="ifRoom" but I should have 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>

这篇关于动作条项始终出现在溢出菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 12:37