2014-02-23 2 views
1

내 레이아웃은 Android Studio에서 생성 한 것 (작업 표시 줄이 비어 있음)을 기반으로합니다. 이것은 안드로이드 스튜디오를 생성 한 것처럼,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이 변경되지 (I 단 하나 개의 자바 파일이) activity_stats.xml에 내 fragment_list.xml을 팽창하는 기본 코드입니다. 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에서 연구했지만 성공하지는 못했습니다. 하드 코드 된 문자열과 치수를 무시하십시오. 수정 해 드리겠습니다.

EDIT : network.xml 인스턴스의 수가 사용자 설정에 따라 달라 지므로 XML이 아닌 프로그래밍 방식으로 자식을 추가해야합니다. XML로

답변

2

당신이 onCreateView 기능 곳에서 자식 레이아웃 (프로그래밍 방식) 코드에서

를 참조로 부모 레이아웃에 include 요소를 사용한다 다중 레이아웃을 포함하려면 당신은 루트를 얻는다 View 이런 컨테이너 레이아웃을 얻으려고한다

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

및 그 선형 레이아웃에 레이아웃 LayoutInflator

layout.addView(newView); 

를 사용하고 마지막으로 부모를 반환 만들고 아이의보기를 얻기 위해이 같은 추가 할 다른 뷰의 인스턴스를 추가 보기

+0

감사하지만 사용자 설정에 따라 네트워크 수가 다를 수 있으므로 프로그래밍 방식으로 레이아웃을 포함해야합니다. – Andrius

+0

잘 자네가 XML을 보여줬고 코드에서 시도한 것이 아니라 내가 이것을 공유했다. 당신이 코드에서 처음 시도한 것을 질문에 추가하면 나는 당신을 안내 할 것입니다 –

+0

나는 그것을 강조하기 위해 질문을 편집했습니다. Java 코드의 networkContainer 요소에 하나 이상의 network.xml 레이아웃을 포함해야합니다. 번호는 사용자 설정에 따라 다르므로 XML 포함을 사용할 수 없습니다. – Andrius

관련 문제