【发布时间】:2014-03-28 21:40:57
【问题描述】:
我知道这个问题很蹩脚,但我在这里确实遇到了困难。我是 android 新手,我正在尝试按照 google 的教程创建一个操作栏:
- http://developer.android.com/design/patterns/actionbar.html
- 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