Android Fragment初探:静态Fragment组成Activity

一直习惯了在Activity中写所有事件处理代码,直到认真学习Fragment时,才发现,Activity完全可以由多个Fragment组成。

对Fragment的了解还不够深入,先从静态Fragment开始练习,把Fragment当成单纯的Activity控件。

就来写一个最常用的“标题+正文”布局吧!

 

activity_main.xml:

 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2                 xmlns:tools="http://schemas.android.com/tools"
 3                 android:layout_width="match_parent"
 4                 android:layout_height="match_parent"
 5                 tools:context=".MainActivity">
 6 
 7     <fragment
 8         android:id="@+id/fragment_title"
 9         android:name="com.example.hopecapital.myapplication.TitleFragment"
10         android:layout_width="fill_parent"
11         android:layout_height="45dp" />
12 
13     <fragment
14         android:layout_below="@id/fragment_title"
15         android:id="@+id/id_fragment_content"
16         android:name="com.example.hopecapital.myapplication.ContentFragment"
17         android:layout_width="fill_parent"
18         android:layout_height="fill_parent" />
19 
20 </RelativeLayout>

主界面的布局,需要写上用到的fragment,并规定好他们的位置。

 

MainActivity.java:

 1 package com.example.hopecapital.myapplication;
 2 
 3 import android.app.Activity;
 4 import android.os.Bundle;
 5 
 6 
 7 public class MainActivity extends Activity {
 8 
 9     @Override
10     protected void onCreate(Bundle savedInstanceState) {
11         super.onCreate(savedInstanceState);
12         setContentView(R.layout.activity_main);
13     }
14 
15 }

这里只需要加载布局即可。

 

接下来是两个fragment的布局和java代码。

fragment_title.xml:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3                 android:layout_width="match_parent"
 4                 android:layout_height="45dp">
 5 
 6     <ImageButton
 7         android:id="@+id/btn_menu"
 8         android:layout_width="wrap_content"
 9         android:layout_height="wrap_content"
10         android:layout_centerVertical="true"
11         android:layout_marginLeft="3dp"
12         android:background="@mipmap/ic_launcher" />
13 
14     <TextView
15         android:layout_width="fill_parent"
16         android:layout_height="fill_parent"
17         android:gravity="center"
18         android:text="标题"
19         android:textSize="20sp"
20         android:textStyle="bold" />
21 
22 </RelativeLayout>

 

TitleFragment.java:

 1 package com.example.hopecapital.myapplication;
 2 
 3 import android.app.Fragment;
 4 import android.os.Bundle;
 5 import android.view.LayoutInflater;
 6 import android.view.View;
 7 import android.view.ViewGroup;
 8 import android.widget.ImageButton;
 9 import android.widget.Toast;
10 
11 public class TitleFragment extends Fragment {
12     private ImageButton Menu;
13 
14     @Override
15     public View onCreateView(LayoutInflater inflater, ViewGroup container,
16                              Bundle savedInstanceState){
17         View view = inflater.inflate(R.layout.fragment_title,container,false);
18         Menu = (ImageButton)view.findViewById(R.id.btn_menu);
19 
20         Menu.setOnClickListener(new View.OnClickListener() {
21             @Override
22             public void onClick(View v) {
23 
24                 Toast.makeText(getActivity(),"titleFragment!",Toast.LENGTH_SHORT).show();
25             }
26         });
27         return view;
28     }
29 }

 

fragment_content.xml:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3                 android:layout_width="match_parent"
 4                 android:layout_height="match_parent">
 5 
 6     <TextView
 7         android:layout_width="match_parent"
 8         android:layout_height="match_parent"
 9         android:gravity="center"
10         android:text="正文区域"
11         android:textSize="20sp" />
12 
13 </RelativeLayout>

 

ContentFragment.java:

 1 package com.example.hopecapital.myapplication;
 2 
 3 
 4 import android.app.Fragment;
 5 import android.os.Bundle;
 6 import android.view.LayoutInflater;
 7 import android.view.View;
 8 import android.view.ViewGroup;
 9 
10 
11 /**
12  * A simple {@link Fragment} subclass.
13  */
14 public class ContentFragment extends Fragment {
15 
16 
17     public ContentFragment() {
18         // Required empty public constructor
19     }
20 
21 
22     @Override
23     public View onCreateView(LayoutInflater inflater, ViewGroup container,
24                              Bundle savedInstanceState) {
25         return inflater.inflate(R.layout.fragment_content,container,false);
26     }
27 
28 
29 }

 

至此,Activity由两部分Fragment分别控制,需要改哪里直接找对应Fragment即可。

posted @ 2015-07-24 14:10  菊草叶与圆企鹅  阅读(899)  评论(0编辑  收藏  举报