Fragment 的基本使用

fragment1 的布局

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ff00ff"
    android:orientation="vertical" >
  
    <TextView
        android:id="@+id/fr1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:text="fragment1" />
  
</LinearLayout>

 

fragment2 的布局

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#0000ff"
    android:orientation="vertical" >
  
    <TextView
        android:id="@+id/fr2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:text="fragment2"
        android:textColor="#ffffff" />
  
</LinearLayout>

activty_main 的布局

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<LinearLayout 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=".MainActivity"
    android:orientation="horizontal"
     >
    <fragment
          
        android:name="com.sanya.fragment.Fragment1"
        android:layout_width="fill_parent"
        android:layout_weight="1"
        android:layout_height="match_parent" />
  
    <fragment
        android:name="com.sanya.fragment.Fragment2"
        android:layout_width="fill_parent"
        android:layout_weight="1"
        android:layout_height="match_parent" />
  
</LinearLayout>

java  代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.sanya.fragment;
  
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
  
public class Fragment1 extends Fragment {
  
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
          
             
        return  inflater.inflate(R.layout.fragment1, container, false);
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.sanya.fragment;
  
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
  
public class Fragment2 extends Fragment {
  
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
          
             
        return  inflater.inflate(R.layout.fragment2, container, false);
    }
}

 

 

 

 

 

 

 

 

 



来自为知笔记(Wiz)


posted on 2014-04-28 13:56  转折点人生  阅读(129)  评论(0)    收藏  举报