我知道这个问题很蹩脚,但我真的在这里遇到困难。我是新来的Android和我试图效仿谷歌的教程创建一个操作栏:创建android动作栏 - 如何?
但我一直困在这里小时。
我的应用程序有一个登录界面,这将触发主屏幕,在这个主屏幕上我会有我的操作栏。我尝试了很多组合来使这个动作栏出现,但没有用处。
一个我试过的事情,简单的添加这个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
它不能仁德R上的布局莫名其妙。我已经试过还设置一个相对或线性布局外菜单,就像这样:
<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文件夹;这样我就不会出现语法错误,但当主屏幕意图抛出应用程序崩溃时,我无法再使用图形布局和其他方式。
所以请,我在这里错过了什么?