我试图在RelativeLayout中使用ListView,但是当我运行我的应用程序时,我收到一个运行时异常,并显示以下消息:
二进制 XML 文件第 2 行:您必须提供layout_width 属性。
我尝试将layout_width属性放在xml资源文件中每个可以想到的位置,但到目前为止还没有成功。
我尝试用这行代码填充 ListView :
.setListAdapter(new ArrayAdapter(this,
R.layout.tablerow3, R.id.label,
items));
这是 tablerow3.xml 内容:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android">
android:layout_width="20dp"
android:layout_height="5dp"
android:id="@+id/tablerow01">
<Label android:id="@+id/label01"
android:layout_width="5dp"
android:layout_height="5dp"
android:textColor="@color/solid_white"
android:singleLine="true"/>
<Label android:id="@+id/label02"
android:layout_width="5dp"
android:layout_height="5dp"
android:textColor="@color/solid_white"
android:singleLine="true"/>
</LinearLayout>
这是包含RelativeLayout(forex2.xml)的xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button android:text="Static Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:id="@+id/button_id">
</Button>
<Spinner android:id="@+id/spinner1"
android:layout_width="match_parent"
android:layout_toRightOf="@id/button_id"
android:layout_alignParentTop="true"
android:layout_height="wrap_content"
android:drawSelectorOnTop="true"
/>
<ListView android:id="@android:id/list"
android:layout_width="5dp"
android:layout_height="5dp"
android:layout_alignParentBottom="true"
/>
<!-- android:layout_width="wrap_content"
android:layout_height="wrap_content" -->
</RelativeLayout>
最佳答案
错误在这里:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android">
android:layout_width="20dp"
android:layout_height="5dp"
android:id="@+id/tablerow01">
查看以 LinearLayout 开头的行的末尾。您留下了一个 > 来关闭该标记,这意味着 XML 解析器将后面的属性视为文本 block ,即 LinearLayout 标记的子级。只需删除多余的 > 就可以了。
关于java - 在android应用程序中获取消息: Binary XML file line #2: You must supply a layout_width attribute,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4674885/