从字面上解释就是,你必须提供layout_width属性。 layout_width的属性很重要,好多控件和布局都必须写layout_width属性,否则就会报这个错误,所以我们再写布局文件的时候要特别注意这个属性。
今天我写了一个组合控件,如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_setting"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="60dip"
android:background="#C000BB00"
android:gravity="center"
android:text="@string/option9"
android:textColor="#FFFFFF"
android:textSize="30sp" />
<com.example.view.CheckedItemBox
android:id="@+id/checked_relative"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</com.example.view.CheckedItemBox>
</LinearLayout>
com.example.view.CheckedItemBox的文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
>
<TextView
android:id="@+id/tv_setautoupdate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="2dip"
android:gravity="left"
android:text="@string/setautoupdate"
android:textColor="#000000"
android:textSize="20sp" />
<TextView
android:id="@+id/tv_setautoupdateword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/tv_setautoupdate"
android:layout_marginLeft="2dip"
android:gravity="left"
android:text="@string/setautoupdateword"
android:textColor="#88000000"
android:textSize="18sp" />
<CheckBox
android:id="@+id/cb_setautoupdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dip"
android:gravity="center_vertical" />
<View
android:layout_width="fill_parent"
android:layout_height="1px"
android:layout_below="@id/tv_setautoupdateword"
android:layout_marginLeft="2dip"
android:layout_marginRight="5dip"
android:layout_marginTop="5dip"
android:background="#000000" />
</RelativeLayout>
我在com.example.view.CheckedItemBox类型中加了layout_width的属性,以为和下面的RelativeLayout是一个属性,所以在RelativeLayout没有加,其实CheckedItemBox只是RelativeLayout的父窗体,所以在RelativeLayout也一样得加。