【问题标题】:Create android action bar - How to?创建 android 操作栏 - 如何?
【发布时间】:2014-03-28 21:40:57
【问题描述】:

我知道这个问题很蹩脚,但我在这里确实遇到了困难。我是 android 新手,我正在尝试按照 google 的教程创建一个操作栏:

  1. http://developer.android.com/design/patterns/actionbar.html
  2. http://developer.android.com/training/basics/actionbar/adding-buttons.html

但我已经被困在这里好几个小时了。

我的应用程序有一个登录界面,它将触发主屏幕,在这个主屏幕中我将有我的操作栏。我尝试了很多组合来使此操作栏出现,但没有任何用处。

我尝试过的事情之一,简单地添加这个 xml 源来查看我的布局:

    <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>

当我这样做并来到图形布局进行检查时,它给了我这样的信息:

    "<menu>" does not set the required layout_width attribute:
     (1) Set to "wrap_content"
     (2) Set to "match_parent"

        "<menu>" does not set the required layout_height attribute:
         (1) Set to "wrap_content"
         (2) Set to "match_parent"
        "action_search" does not set the required layout_width attribute:
         (1) Set to "wrap_content"
         (2) Set to "match_parent"
        "action_search" does not set the required layout_height attribute:
         (1) Set to "wrap_content"
         (2) Set to "match_parent"
        "action_settings" does not set the required layout_width attribute:
         (1) Set to "wrap_content"
         (2) Set to "match_parent"
        "action_settings" does not set the required layout_height attribute:
         (1) Set to "wrap_content"
         (2) Set to "match_parent"
        Exception raised during rendering: com.android.layoutlib.bridge.MockView cannot be cast to android.view.ViewGroup
        You must supply a layout_width attribute.
        You must supply a layout_height attribute.

如果我设置布局内容,下一个错误信息是:

Exception raised during rendering: com.android.layoutlib.bridge.MockView cannot be cast to android.view.ViewGroup

它无法以某种方式呈现布局...我也尝试在菜单外设置一个相对或线性布局,如下所示:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background" >

    <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>

</RelativeLayout>

但是这样我收到了:

Unexpected namespace prefix “xmlns” found for tag Menu

还有;我试图从 res/layout 文件夹中删除我的 .xml 并将他单独留在另一个 res/menu 文件夹中……仅使用 google 提供的 .xml;这样我就不会出现语法错误,但是当主屏幕意图引发应用程序崩溃时,我无法再使用图形布局和一种或另一种方式。

请问,我到底错过了什么?

【问题讨论】:

    标签: android xml android-layout android-actionbar


    【解决方案1】:

    菜单 xml 不是布局 xml 的一部分。它是菜单文件夹中的一个单独的 xml 文件,并在运行时使用以下代码添加:

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
      // Inflate the menu items for use in the action bar
      MenuInflater inflater = getMenuInflater();
      inflater.inflate(R.menu.main_activity_actions, menu);
      return super.onCreateOptionsMenu(menu);
    }
    

    然后使用以下代码响应菜单 xml 中的各个项目:

    @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);
      }
    }
    

    如果您想支持 Android 2.1 及更高版本,请使用此建议 - http://developer.android.com/training/basics/actionbar/setting-up.html#ApiLevel7

    【讨论】:

      【解决方案2】:

      这个答案基于 samleighton87 的答案,我建议他编辑他的帖子,但他在 stackoverflow 上不太活跃;因此,根据他的回答,我给出了另一个答案,并进行了一些更正,以澄清我遇到问题时的观点:

      查看本教程:https://www.youtube.com/watch?v=iA2Efmo2PCA

      一开始你就会看到需要做什么以及缺少什么。

      您需要使用 google 在 main_activity_actions.xml 中提供的代码在 .xml 文件之后创建 res/menu 文件夹;在此之后,请确保在您的活动 .java 文件中包含此方法:

      @Override
      public boolean onCreateOptionsMenu(Menu menu)
      {
        // Inflate the menu items for use in the action bar
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main_activity_actions, menu);
        return super.onCreateOptionsMenu(menu);
      }
      

      这个 .java 文件中的主类将扩展“Activity”。现在您将意识到自动生成的是 R.java 中的引用,他 (R.java) 将能够找到您的 res/menu 文件夹,允许您使用 inflater.inflate(R.menu.main_activity_actions, menu); 指向它。请记住:此 .xml 文件不是您的布局 xml 文件的一部分。它是一个 菜单文件夹中的分隔 .xml 文件,您也将无法使用图形 布局来处理它。

      然后使用以下代码响应菜单 xml 中的各个项目:

      @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);
        }
      }
      

      如果您想支持 Android 2.1 及更高版本,请使用此建议检查 - http://developer.android.com/training/basics/actionbar/setting-up.html#ApiLevel7

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-11-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-05-08
        相关资源
        最近更新 更多