我正在尝试在操作栏中添加操作项,但是即使有很多空间,项也总是添加到溢出列表中。有什么建议可以解决这个问题吗?
这应该是这样的
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'])
}
答案 0 :(得分:8)
我的应用程序正在使用支持库来兼容低至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>
答案 1 :(得分:5)
你需要添加yourapp:showAsAction =“always”,如下所示。
<item
android:id="@+id/Option"
android:actionProviderClass="android.support.v7.widget.ShareActionProvider"
android:icon="@drawable/ic_action_settings"
android:title="Option"
yourapp:showAsAction="always"/>
<item
android:id="@+id/Save"
android:actionProviderClass="android.support.v7.widget.ShareActionProvider"
android:icon="@drawable/ic_action_save"
android:title="Save"
yourapp:showAsAction="always"/>
答案 2 :(得分:4)
根据document
如果您的应用使用支持库来兼容版本低至 Android 2.1,showAsAction属性不能从android:namespace获得。相反,此属性由支持库提供,您必须定义自己的XML命名空间并将该命名空间用作属性前缀。 (自定义XML命名空间应该基于您的应用程序名称,但它可以是您想要的任何名称,并且只能在您声明它的文件范围内访问。)例如:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
<!-- Search, should appear as action button -->
<item android:id="@+id/action_search"
android:icon="@drawable/ic_action_search"
android:title="@string/action_search"
yourapp:showAsAction="ifRoom" />
...
</menu>
答案 3 :(得分:2)
试试这个:
<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" />
答案 4 :(得分:1)
如果您想要始终在操作栏中显示所有项目,请制作如下所示的菜单xml文件:
<item
android:id="@+id/action_refresh"
android:icon="@drawable/ic_action_refresh"
app:showAsAction="always"
android:title="@string/action_refresh" />
<item
android:id="@+id/action_search"
android:icon="@drawable/ic_action_search"
app:showAsAction="always"
android:title="@string/action_search" />
<item
android:id="@+id/action_settings"
android:title="@string/action_settings"
app:showAsAction="always" />
答案 5 :(得分:0)
在您尝试执行此操作之前,我遇到了类似的问题
<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"
app:showAsAction="never"
android:title="@string/action_refresh" />
<item
android:id="@+id/action_search"
app:showAsAction="never"
android:title="@string/action_search" />
<item
android:id="@+id/action_settings"
android:title="@string/action_settings"
app:showAsAction="never" />
答案 6 :(得分:-3)
尝试使用android:showAsAction="always"
获取菜单中的项目。