将多个子布局从XML文件添加到另一个布局

时间:2014-02-23 09:43:22

标签: android xml android-layout

我的布局基于Android Studio创建的内容 - 空白,带有操作栏。这是将fragment_list.xml扩展到activity_stats.xml的默认代码(我只有一个Java文件)

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_list, container, false);
    return rootView;
}

Activity_stats不会改变,就像Android Studio生成它一样。这是fragment_list.xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.company.dummyname.stats$PlaceholderFragment" >
    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:clipToPadding="false"
        android:paddingTop="@dimen/network_margin_vertical"
        android:paddingLeft="@dimen/network_margin_horizontal"
        android:paddingRight="@dimen/network_margin_vertical" >
        <LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/networkContainer"/>
    </ScrollView>
</RelativeLayout>

我想向networkContainer添加多个子节点。子项应该是来自network.xml的布局,如下所示:

<?xml version="1.0" encoding="utf-8"?>

<android.support.v7.widget.GridLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:grid="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.company.dummyname.stats$PlaceholderFragment"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    grid:columnCount="2" >

    <ImageView
        android:layout_width="88dp"
        android:layout_height="70dp"
        android:background="#fff8cdac"
        grid:layout_row="0"
        grid:layout_column="0"
        grid:layout_rowSpan="2"
        android:src="@drawable/ic_launcher"/>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="8dp"
        grid:layout_row="0"
        grid:layout_column="1"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="9999.99"
            android:id="@+id/totalToday"
            android:textSize="32sp" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=" today"
            android:textSize="32sp" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="46dp"
        android:paddingLeft="8dp"
        grid:layout_row="1"
        grid:layout_column="1"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="9999.99"
            android:id="@+id/totalYesterday"
            android:textSize="24sp" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=" yesterday"
            android:textSize="24sp" />
    </LinearLayout>

    <TableLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        grid:layout_row="2"
        grid:layout_columnSpan="2">

        <TableRow
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">

            <TextView
                style="@style/StatsCell" />

            <TextView
                style="@style/StatsCell"
                android:text="Hits" />
        </TableRow>

        <TableRow
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

            <TextView
                style="@style/StatsCell"
                android:text="Today"
                android:layout_height="match_parent" />

            <TextView
                style="@style/StatsCell"
                android:id="@+id/todayHits"
                android:text="9999" />

        </TableRow>

    </TableLayout>

</android.support.v7.widget.GridLayout>

将会有更多数据和一行,但我删除它们以简化问题。目标是让许多network.xml实例具有不同的数据。如何插入它们并更改内部数据?我在Google上做过研究,但没有成功。请忽略硬编码的字符串和尺寸,我会修复它。

编辑:我需要以编程方式添加子项,而不是XML,因为network.xml实例的数量会因用户设置而异。

1 个答案:

答案 0 :(得分:2)

IN XML

要包含多个布局,您应该在父布局中使用include元素来引用子布局

IN CODE(PROGRAMATICALLY)

在你获得根onCreateView的{​​{1}}函数中尝试获取像这样的容器布局

View

然后在该线性布局中创建并添加您想要添加的其他视图的实例 获取子视图的视图使用LayoutInflator

LinearLayout layout = (LinearLayout) rootView.findViewById(R.id.networkContainer);

最后返回父查看