【问题标题】:Layout definition. "You must supply a layout_width attribute."布局定义。 “您必须提供 layout_width 属性。”
【发布时间】:2011-12-06 23:55:24
【问题描述】:

我正在开发一个小 android 应用程序,但布局有问题,我一直试图在我的 xml 中找到错误,但我找不到它......

我得到的错误是“你必须提供一个 layout_width 属性”,但我做到了,但它仍然不起作用......

这是我的 XML 文件:

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

    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="100" >

    <TextView android:id="@+id/nombreEvento"
        android:layout_height="fill_parent"
        android:layout_weight="70"
    />

    <TextView android:id="@+id/moneda"
        android:layout_height="fill_parent"
        android:layout_weight="10"
    />

    <TextView android:id="@+id/totalEvento"
        android:layout_height="wrap_content"
        android:layout_weight="20"
    />
    </LinearLayout>

<TextView android:id="@+id/fecha"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
/>

</LinearLayout>

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    不是所有的 TextView 都有 layout_height 和 layout_weight 而不是 layout_height 和 layout_width(可能还有 layout_weight)。试试这个:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
    
        <LinearLayout 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:weightSum="100" >
    
        <TextView android:id="@+id/nombreEvento"
            android:layout_height="fill_parent"
            android:layout_width="0dp"
            android:layout_weight="70"
        />
    
        <TextView android:id="@+id/moneda"
            android:layout_height="fill_parent"
            android:layout_width="0dp"
            android:layout_weight="10"
        />
    
        <TextView android:id="@+id/totalEvento"
            android:layout_height="wrap_content"
            android:layout_width="0dp"
            android:layout_weight="20"
        />
        </LinearLayout>
    
    <TextView android:id="@+id/fecha"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
    />
    
    </LinearLayout>
    

    【讨论】:

    • 有什么区别?
    • @AlikElzin-kilaka 嵌套的文本视图有 layout_WEIGHT ...也应该有 layout_WIDTH。
    【解决方案2】:

    我想你没有把dp 放在宽度大小之后。为什么你的代码可能不起作用。 前android:layout_weight="20" 使用这个android:layout_weight="20dp"

    【讨论】:

      【解决方案3】:

      尝试将android:layout_width="wrap_content" 添加到没有它的 TextView 中。

      【讨论】:

      • 我以前也一直使用这个,但通常在你想要做 0dp 的几个项目上使用重量时,因为大多数时候你希望宽度是一个特定的比例而不是拥有视图以 wrap_content 为宽度,然后使用权重填充剩余空间。
      • 作为 0dip 的替代方案,我在应用权重时经常使用 fill_parent/match_parent,它似乎也是如此。但这取决于 OP 的判断。
      • 我也试过了,但我遇到了问题。它通常有效,但偶尔(但并非始终如一)会发生奇怪的事情,它会变得比您预期的要大。我遇到了将东西从屏幕上推开的问题。
      【解决方案4】:

      在我的情况下,我忘记将 layout_width 属性放入 TableLayout。

      这有点令人困惑,因为作为 T​​ableLayout 的子项的 TableRow 不需要 layout_width,所以我在这个问题中运行...

      【讨论】:

      • 我遇到了类似的问题。如果您从 XML 扩展布局,并且该 XML 的根有一个 TableRow,则必须指定 TableRow 的宽度。
      【解决方案5】:

      您需要指定视图的宽度和高度。这是必须的。对于 textview,您只需设置高度和重量。添加

      android:layout_width="0dip"
      

      【讨论】:

        【解决方案6】:

        在我的例子中,android:layout_width="@dimen/abc_action_bar_progress_bar_size" 已设置,但 @dimen/abc_action_bar_progress_bar_size“是私有的”。因此,它显然没有抱怨无法访问它,而是默默地删除了它,然后抱怨它丢失了。

        【讨论】:

          【解决方案7】:

          当您使用属性但忘记将属性映射到样式时,您也会遇到相同的错误

          【讨论】:

            【解决方案8】:

            在我的情况下,它在我重建项目和无效缓存并重新启动我的 android Studio IDE 时起作用

            【讨论】: