布局定义。"您必须提供一个layout_width属性。"

28

我正在开发一个小型的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>
8个回答

20

不是所有的TextView都有layout_heightlayout_weight属性,而是可能有layout_heightlayout_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>

2
有什么区别? - AlikElzin-kilaka
1
@AlikElzin-kilaka 嵌套的文本视图具有layout_WEIGHT...也应该有layout_WIDTH。 - ComeIn

11

我认为您没有在宽度大小后面添加 dp。这是为什么您的代码可能无法正常工作的原因。 例如:android:layout_weight="20" 请使用 android:layout_weight="20dp"


3

在我的情况下,我忘记给TableLayout添加layout_width属性。

这有点令人困惑,因为作为TableLayout的子元素的TableRow并不需要layout_width属性,所以我遇到了这个问题...


我曾经遇到过类似的问题。如果你从XML文件中填充布局,并且该XML文件的根元素是TableRow,则必须指定TableRow的宽度。 - Bitcoin Cash - ADA enthusiast

3
尝试将android:layout_width="wrap_content"添加到没有该属性的TextView中。

我以前也经常使用这个,但通常当在多个项上使用权重时,您希望使用0dp,因为大多数情况下,您希望宽度保持一定的比例,而不是将视图的宽度设置为wrap_content,然后使用权重填充剩余空间。 - kabuko
作为0dip的替代方案,我通常在应用权重时使用fill_parent/match_parent,它似乎可以达到相同的效果。但这取决于OP自己的判断。 - MrZander
我也尝试过那个方法,但是遇到了问题。通常它可以正常工作,但偶尔(不是每次)会出现奇怪的情况,使得它变得比预期更大。我遇到过屏幕外的元素被推出的问题。 - kabuko

2
您需要指定视图的宽度和高度。这是必须的。对于TextView,您只设置了高度和重量。请添加宽度属性。
android:layout_width="0dip"

1
在我的情况下,设置了android:layout_width="@dimen/abc_action_bar_progress_bar_size",但@dimen/abc_action_bar_progress_bar_size是“私有的”。因此,它似乎默默地将其删除,而不是抱怨无法访问它,以后再抱怨它丢失了。

0

如果您使用属性但忘记将其映射到样式,则也会出现相同的错误。


0
在我的情况下,当我重建项目并使缓存无效并重新启动我的Android Studio IDE时,它起作用了。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接