操作栏未在4.2.2中显示

时间:2013-08-23 16:11:27

标签: android android-actionbar android-4.2-jelly-bean

  

解决方案:图像搜索图标出现问题。 Android没有找到搜索操作的图像,我还没有添加它们   res-folders,之后开始显示...

我正在尝试将操作栏添加到应用中,我正在关注Google Developer Website

中显示的基本应用教程

我写了以下代码。

  

MainActivity

public class MainActivity extends ActionBarActivity {

    public void openSearch(){

        System.out.println("TEST SEARCH");

    }
    public void openSettings(){

        System.out.println("TEST SETTINGS");

    }

    @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.
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main_activity_actions, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle presses on the action bar items
        switch (item.getItemId()) {
            case R.id.action_search:
                openSearch();
                return true;
            case R.id.action_settings:
                openSettings();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

}
  

RES /菜单/ main_activity_actions.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <!-- Search, should appear as action button -->
    <item android:id="@+id/action_search"
          android:icon="@drawable/ic_action_search"
          android:title="@string/action_search"
          android:showAsAction="ifRoom" />
    <!-- Settings, should always be in the overflow -->
    <item android:id="@+id/action_settings"
          android:title="@string/action_settings"
          android:showAsAction="never" />
</menu>
  

的AndroidManifest.xml

<uses-sdk android:minSdkVersion="7"  android:targetSdkVersion="18" />

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

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

但不是像这样的动作栏

enter image description here

我得到一个选项菜单。当我在运行应用程序时按下菜单按钮时显示。

Screen Shot on Emulator running 4.2.2

我在教程中遗漏了什么,或者代码中有什么问题?

  

修改

做了什么 beworker 问我得到了这个结果,但仍然没有找到搜索框。

enter image description here

2 个答案:

答案 0 :(得分:8)

您的代码或教程都没有错。这完全与您的模拟器配置有关。您需要配置模拟器,它具有软件按钮。切换到&#34;设备定义&#34;选项卡和编辑设备使用&#34;软件&#34;按钮如下图所示。

enter image description here

或者,您只需取消选中&#34;键盘/硬件键盘存在&#34; ADV配置中的属性(见下图)并重新启动模拟器。

enter image description here

如果存在硬件菜单按钮,Android 4.0会模拟旧式菜单。这就是为什么所有菜单操作都会进入那里的原因。如果在模拟器中禁用硬件菜单,则操作将按预期进入操作栏。

请注意,在具有三星G2,G3等硬件菜单按钮的手机上仍会出现菜单弹出窗口,您无法在代码中更改此设置。 Android将决定在每台设备上更好的用户体验。

<强>更新

另一个说明。如果您使用兼容性库,请确保使用xmlns:yourapp="http://schemas.android.com/apk/res-auto"yourapp:showAsAction="always",如下所述。

  

但是,如果您的应用使用支持库来实现兼容性   版本低至Android 2.1,showAsAction属性不是   可从android:名称空间获得。相反,这个属性是   由支持库提供,您必须定义自己的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="always"  />
    ...
</menu>

答案 1 :(得分:1)

  

我在教程中遗漏了什么,或者代码中有什么问题?

是的,有区别。 尝试从项目中删除示例字词。

实际上您的项目名称占据了所有空间,并且在搜索菜单项中,如果有任何可用空间/空间而不是由于长项目名称,您已要求系统提供空间(可能是活动或其他)在你的情况下。

因此,要么将项目名称更改为小型项目,要么像这样使用“始终”:

  <item android:id="@+id/action_search"
      android:icon="@drawable/ic_action_search"
      android:title="@string/action_search"
      android:showAsAction="always" />
相关问题