【问题标题】:Android actions not showing up in actionbarAndroid 操作未显示在操作栏中
【发布时间】:2015-05-09 05:23:53
【问题描述】:

我一直在尝试编写一个简单的应用程序来在 Android 上做笔记。 我想在主要活动的操作栏中显示一个“添加”按钮。但是,它仅显示为溢出。 我试过了

<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/action_add"
    android:icon="@drawable/ic_add"
    android:title="@string/add"
    android:showAsAction="always"></item>
</menu>

但我无法运行该应用程序,因为 android studio 告诉我

应将 app:showAsAction 与带有 xmlns:app="http://schemas.android.com/apk/res-auto" 的 appcompat 库一起使用

有什么想法吗?据我所知,我没有使用 appcompat

编辑: java类:

public class NoteChoice extends Activity {
@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.notechooser);
    SQLiteDatabase db = openOrCreateDatabase("Notes", MODE_PRIVATE, null);
    db.execSQL("CREATE TABLE IF NOT EXISTS Notes ( id INTEGER PRIMARY KEY AUTOINCREMENT, val VARCHAR )");


}
@Override
public boolean onCreateOptionsMenu(Menu menu){
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.notechooser_actions, menu);
    return super.onCreateOptionsMenu(menu);
}

public boolean onOptionsItemSelected(MenuItem item){
    switch(item.getItemId()){
        case R.id.action_add:
            onPause();
            startActivity(new Intent("com.example.theorangeutan.notes.Create"));
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

}

styles.xml 文件:

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="@android:style/Theme.Holo.Light">
    <!-- Customize your theme here. -->
</style>
<style name="splash" parent="@android:style/Theme.Holo.Light.NoActionBar">
    <!-- Customize your theme here. -->
</style>

【问题讨论】:

  • 发布您的样式文件。
  • 添加了它。 Apptheme 是所有活动中使用的,splash 用于启动画面。

标签: java android xml android-studio android-appcompat


【解决方案1】:

support-v7 被认为是第 3 方库,因此通过 support-v7 引入的 XML 属性必须使用 app 命名空间而不是 android 命名空间,以保证向后兼容 v7。例如,android:showAsAction 仅可用 >= SDK 11,而 app:showAsAction 可用 >= SDK 7。

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/action_add"
    android:icon="@drawable/ic_add"
    android:title="@string/add"
    app:showAsAction="always"></item>
</menu>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-28
    • 2012-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多