开发者

When including external xml file, LayoutInflater throws RuntimeException: "You must supply a layout_width attribute."

开发者 https://www.devze.com 2023-04-07 17:42 出处:网络
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/up

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
    xmln开发者_JAVA技巧s: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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消