当包含外部 xml 文件时,LayoutInflater 会抛出 RuntimeException:“您必须提供layout_width 属性。”

发布于 2024-12-06 17:07:52 字数 3767 浏览 0 评论 0原文

我试图在我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

染墨丶若流云 2024-12-13 17:07:52

尝试在 标记下的 upgrade.xml 中指定布局高度和宽度,如下所示:

<include 
    layout="@layout/upgrades_content"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
/>

将宽度和高度值设置为满足您的需要:fill_parentwrap_content

try to specify the layout height and width in upgrade.xml under your <include> tag like this:

<include 
    layout="@layout/upgrades_content"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
/>

Put the width and height values to whatever fill your need: fill_parent or wrap_content

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文