当包含外部 xml 文件时,LayoutInflater 会抛出 RuntimeException:“您必须提供layout_width 属性。”
我试图在我的 Activity 的 XML 布局文件 (upgrades.xml) 中包含一个包含 ListView 的 ScrollView (upgrade_content.xml)。当我使用
包含外部文件时,我收到 RuntimeException 并显示消息“您必须提供布局宽度属性”。
如果我从upgrade_content.xml复制代码并将其直接粘贴到upgrades.xml中,它就可以正常工作。
Upgrades.xml (这会引发异常):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/upgrades_layout"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:background="@drawable/menu_screen_bg"
android:orientation="vertical">
<include layout="@layout/upgrades_content"/>
</LinearLayout>
upgrades_content.xml:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:fillViewport="true"
android:layout_weight="1">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal">
<!-- Activity Title -->
<RelativeLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp">
<TextView
android:id="@+id/upgrades_title_text"
android:text="@string/upgrade_store_string"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:textSize="@dimen/TitleTextSize"
style="@style/ActivityTitleTextStyle" />
</RelativeLayout>
<!-- Upgrades List -->
<LinearLayout
android:background="@drawable/old_paper_bg"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="@dimen/OldPaperScrollViewMargin"
android:layout_marginRight="@dimen/OldPaperScrollViewMargin"
android:layout_marginBottom="@dimen/OldPaperScrollViewMargin"
android:padding="@dimen/OldPaperScrollViewPadding">
<ListView
android:id="@android:id/list"
android:layout_width="wrap_content"
android:layout_height="fill_parent">
</ListView>
</LinearLayout>
</LinearLayout>
</ScrollView>
UpgradeActivity.java:
public class UpgradeActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.upgrades);
setListAdapter(ArrayAdapter.createFromResource(this,
R.array.upgrade_string_array,
R.layout.upgrade_layout_list_item));
}
}
upgrade_layout_list_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="@dimen/LabelTextSize" />
编辑: 这段代码抛出的 RuntimeException 不会导致强制关闭,所以我在设置 Eclipse 捕获的异常方面可能有点过于自由。
I'm trying to include a ScrollView (upgrade_content.xml) containing a ListView in my Activity's XML layout file (upgrades.xml). When I include the external file using <include layout="@layout/upgrades_content"/>
, I get a RuntimeException with the message "You must supply a layout_width attribute."
If I copy the code from upgrade_content.xml and paste it directly into upgrades.xml, it works fine.
upgrades.xml (This throws the exception):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/upgrades_layout"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:background="@drawable/menu_screen_bg"
android:orientation="vertical">
<include layout="@layout/upgrades_content"/>
</LinearLayout>
upgrades_content.xml:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:fillViewport="true"
android:layout_weight="1">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal">
<!-- Activity Title -->
<RelativeLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="10dp">
<TextView
android:id="@+id/upgrades_title_text"
android:text="@string/upgrade_store_string"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:textSize="@dimen/TitleTextSize"
style="@style/ActivityTitleTextStyle" />
</RelativeLayout>
<!-- Upgrades List -->
<LinearLayout
android:background="@drawable/old_paper_bg"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="@dimen/OldPaperScrollViewMargin"
android:layout_marginRight="@dimen/OldPaperScrollViewMargin"
android:layout_marginBottom="@dimen/OldPaperScrollViewMargin"
android:padding="@dimen/OldPaperScrollViewPadding">
<ListView
android:id="@android:id/list"
android:layout_width="wrap_content"
android:layout_height="fill_parent">
</ListView>
</LinearLayout>
</LinearLayout>
</ScrollView>
UpgradeActivity.java:
public class UpgradeActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.upgrades);
setListAdapter(ArrayAdapter.createFromResource(this,
R.array.upgrade_string_array,
R.layout.upgrade_layout_list_item));
}
}
upgrade_layout_list_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="@dimen/LabelTextSize" />
EDIT:
The RuntimeException thrown by this code doesn't cause a Force Close, so I might have just been a little too liberal in which exceptions I set Eclipse to catch.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试在
标记下的upgrade.xml
中指定布局高度和宽度,如下所示:将宽度和高度值设置为满足您的需要:
fill_parent
或wrap_content
try to specify the layout height and width in
upgrade.xml
under your<include>
tag like this:Put the width and height values to whatever fill your need:
fill_parent
orwrap_content