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

标签 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>

我想将多个 child 添加到 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 实例。如何插入它们并更改其中的数据?我在谷歌上做了研究,但没有成功。请忽略硬编码的字符串和尺寸,我会解决的。

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

最佳答案

在 XML 中

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

在代码中(以编程方式)

在获取根 ViewonCreateView 函数中,尝试像这样获取容器布局

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

然后在该线性布局中创建并添加您要添加​​的其他 View 的实例,如下所示 获取子布局的 View 使用 LayoutInflator

layout.addView(newView);

最后返回父 View

关于android - 将多个子布局从 XML 文件添加到另一个布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21966593/

相关文章:

java - 由于错误而无法启动基本应用程序(UnsupportedOperationException : Can't convert to dimension: type=0x1)

mysql - filemaker pro通过php导出和导入到mysql

android - 如何在方向更改期间强制某些 View 旋转,但让其他 View 保持原样?

android - 如何在android中将checkedTextView的复选框和文本居中?

android - 编辑/删除谷歌日历事件并获取事件 ID

Android 欺骗电话号码?

javascript - Google Chrome 忽略 Cache-Control header 的原因

xml - Opencv,读取用于相机校准的 XML 文件时出错

xml - 图形 XML 到可视化表示

java - Android L 支持库工具栏出现在 View 下