我知道這個問題很蹩腳,但我真的在這裏遇到困難。我是新來的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文件夾;這樣我就不會出現語法錯誤,但當主屏幕意圖拋出應用程序崩潰時,我無法再使用圖形佈局和其他方式。
所以請,我在這裏錯過了什麼?