2014-02-23 81 views
1

我的布局基于Android Studio创建的内容 - 使用操作栏留空。这是默认的代码膨胀我fragment_list.xml到activity_stats.xml(我只有一个的Java文件)将XML文件中的多个子布局添加到另一个布局

@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实例的数量将根据用户设置而有所不同。

回答

2

以XML

要包含你应该使用include元素父布局指孩子布局

代码(编程)

onCreateView功能在多个布局你得到根View尝试得到像这样的容器布局

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

,然后在该线性布局创建并添加你想要添加这样 获得为孩子查看其他视图实例布局中使用LayoutInflator

layout.addView(newView); 

最后返回父查看

+0

谢谢,但我需要以编程方式包含布局,因为可以有不同数量的网络,具体取决于用户设置。 – Andrius

+0

欢迎你很好地展示了XML,而不是你在代码中尝试过的东西,所以我分享了它。添加你在代码中试过的东西,然后我会引导你 –

+0

我编辑了这个问题来强调这一点。我需要在Java代码中将一个或多个network.xml布局包含到networkContainer元素中。数量取决于用户设置,所以我不能使用XML包含。 – Andrius

相关问题