一、程序图
二、布局文件
(1)main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <FrameLayout android:id="@+id/contain" android:layout_width="fill_parent" android:layout_height="20px" android:layout_weight="9" > </FrameLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="20px" android:layout_weight="1" android:orientation="horizontal" > <Button android:id="@+id/fg_one_btn" android:layout_width="20px" android:layout_height="fill_parent" android:layout_margin="3px" android:layout_weight="1" android:text="First" /> <Button android:id="@+id/fg_two_btn" android:layout_width="20px" android:layout_height="fill_parent" android:layout_weight="1" android:layout_margin="3px" android:text="Second" /> <Button android:id="@+id/fg_three_btn" android:layout_width="20px" android:layout_height="fill_parent" android:layout_margin="3px" android:layout_weight="1" android:text="Three"/> </LinearLayout> </LinearLayout>
(2) first.xml
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:background="#123"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="FirstFragment"/>
<Button
android:id="@+id/hello"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="点击"/>
</LinearLayout>
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:background="#000"
android:gravity="center">
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="SecondFragment"
android:textColor="#fff"/>
</LinearLayout>
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:background="#bd0708"
android:gravity="center">
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="ThreeFragment"/>
</LinearLayout>
三、程序
(1)FragmentActivity.java
import com.liu.cn.fragment.FirstFragment;
import com.liu.cn.fragment.SecondFragment;
import com.liu.cn.fragment.ThirdFragment;
import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class FragmentActivity extends Activity implements OnClickListener{
Button one,two,three;
LayoutInflater inflater;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
inflater=(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
init();
}
public View getLayoutView(int layoutId){
View v=inflater.inflate(layoutId, null);
return v;
}
private void init(){
one=(Button) this.findViewById(R.id.fg_one_btn);
two=(Button) this.findViewById(R.id.fg_two_btn);
three=(Button) this.findViewById(R.id.fg_three_btn);
one.setOnClickListener(this);
two.setOnClickListener(this);
three.setOnClickListener(this);
normalColor();
//初始化第一个fragment
FirstFragment ff=new FirstFragment();
switchView(ff, one);
}
//正常颜色
private void normalColor(){
one.setBackgroundColor(Color.BLUE);
two.setBackgroundColor(Color.BLUE);
three.setBackgroundColor(Color.BLUE);
}
//fragment之间的转换,并且改变对应的按钮颜色
private void switchView(Fragment fg,View v){
v.setBackgroundColor(Color.GRAY);
getFragmentManager().beginTransaction().
replace(R.id.contain, fg).commit();
}
public void onClick(View v) {
normalColor();
int id=v.getId();
switch (id) {
case R.id.fg_one_btn:
FirstFragment ff=new FirstFragment();
switchView(ff, v);
break;
case R.id.fg_two_btn:
SecondFragment sf=new SecondFragment();
switchView(sf, v);
break;
case R.id.fg_three_btn:
ThirdFragment tf=new ThirdFragment();
switchView(tf, v);
break;
default:
break;
}
}
}
(2) BaseFragment .java
import com.liu.cn.R.layout;
import android.R.integer;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
//定义一个抽象基类BaseFragment,当3个fragment之间有公用的方法时,可把方法写在这里,其他fragment继承
//Basefragment,方法可以之间调用,好处就在这儿,用的久了就体现出来了
public abstract class BaseFragment extends Fragment{
private FragmentActivity activity;
public View LayoutView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//强转,调用fragmentActivity中的getLayoutView方法;
activity=(FragmentActivity) getActivity();
//下面俩行不能颠倒,否则空指针错误,原因是找不到布局文件
LayoutView=activity.getLayoutView(getLayoutId());
createChild(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return LayoutView;
}
public abstract void createChild( Bundle savedInstanceState);
public abstract int getLayoutId();
}
(3) FirstFragment.java
import com.liu.cn.BaseFragment;
import com.liu.cn.R;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
//在找布局文件中的组件时,可按下面的按钮示例初始化,并做其他的事情,其他的fragment初始化都一样
//在这里就不多啰嗦了,剩下的俩个fragment中你们可以练习下
public class FirstFragment extends BaseFragment{
@Override
public void createChild(Bundle savedInstanceState) {
Button firstBtn=(Button) LayoutView.findViewById(R.id.hello);
firstBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Toast.makeText(getActivity(),
"点击了第一个Framgment中的按钮", 0).show();
}
});
}
@Override
public int getLayoutId() {
// TODO Auto-generated method stub
return R.layout.first;
}
}
import android.os.Bundle;
import com.liu.cn.BaseFragment;
import com.liu.cn.R;
public class SecondFragment extends BaseFragment{
@Override
public void createChild(Bundle savedInstanceState) {
}
@Override
public int getLayoutId() {
// TODO Auto-generated method stub
return R.layout.second;
}
}
import android.os.Bundle;
import com.liu.cn.BaseFragment;
import com.liu.cn.R;
public class ThirdFragment extends BaseFragment {
@Override
public void createChild(Bundle savedInstanceState) {
}
@Override
public int getLayoutId() {
// TODO Auto-generated method stub
return R.layout.three;
}
}