【问题标题】:The required layout_width and layout_height attributes are missing缺少必需的 layout_width 和 layout_height 属性
【发布时间】:2015-03-29 08:07:33
【问题描述】:

我有以下布局 xml:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<item android:id="@+id/descriptions_menu"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:icon="@drawable/ic_menu_info_details"
      android:title="Toggle Descriptions On/ Off" />

<item android:id="@+id/settings_menu"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:icon="@drawable/ic_menu_preferences"
      android:title="Settings" />

但我不断收到此错误: 缺少必需的 layout_width 和 layout_height 属性

这是在第 2、9、10 行报告的。这看起来很奇怪。这是我试图复活的一个旧项目(> 5 年)。

【问题讨论】:

  • 这是你的布局文件还是菜单文件??
  • 恐怕我不明白你的问题。它在 res/layout/mainmenu.xml 中,它定义了以前的菜单(通过按下手机上的菜单按钮)
  • 你能告诉我们你的菜单是在哪里添加的吗?

标签: android android-layout


【解决方案1】:

我认为menu 布局在res/layout 中无效,您需要将其放置在res/menu 中,如documentation 中所述

要定义菜单,请在项目的 res/menu/ 目录中创建一个 XML 文件,并使用以下元素构建菜单

你可以使用MenuInflater.inflate()给它充气

【讨论】:

    【解决方案2】:

    如果这是菜单文件,那么您必须在文件末尾关闭菜单标签, 确保此文件位于 res > menu 文件夹 > menu 文件中。

    <?xml version="1.0" encoding="utf-8"?>
        <menu xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    
        <item android:id="@+id/descriptions_menu"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:icon="@drawable/ic_menu_info_details"
              android:title="Toggle Descriptions On/ Off" />
    
        <item android:id="@+id/settings_menu"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:icon="@drawable/ic_menu_preferences"
              android:title="Settings" />
        </menu>
    

    【讨论】:

    • 很好看,我实际上正在这样做,但我在复制/粘贴时错过了它
    猜你喜欢
    • 1970-01-01
    • 2015-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多