整体效果如下:
实现方式:通过Activity的FragmentManage去实现
首先要先去创建两个布局文件,分别为pay.xml和income.xml,代表两个片段的内容,下面我只是贴了其中一个布局文件的内容
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:background="#445543"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- </LinearLayout>
简单起见我只是把这两个布局文件的background给设置了一下,用于区分。
然后写两个类,分别为PayFragment和IncomeFragment,让它们都去继承Fragment类,重写onCreateView方法,
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = View.inflate(getActivity(), R.layout.pay, null);
return view;
}
Inflate()作用就是将xml定义的一个布局找出来,inflate方法中间那个参数是制定的布局文件。
完成上面的工作后我们就该总布局文件的编写了:
- <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"
- android:orientation="vertical"
- tools:context=".SecondActivity" >
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="horizontal">
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:id="@+id/pay"
- android:text="支出"/>
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:id="@+id/income"
- android:text="收入"/>
- </LinearLayout>
- <FrameLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:id="@+id/fl">
- </FrameLayout>
- </LinearLayout>
- import android.os.Bundle;
- import android.app.Activity;
- import android.support.v4.app.FragmentActivity;
- import android.support.v4.app.FragmentManager;
- import android.support.v4.app.FragmentTransaction;
- import android.view.Menu;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.RelativeLayout;
- public class SecondActivity extends FragmentActivity implements OnClickListener{
- private Button pay;
- private Button income;
- private RelativeLayout rl;
- private IncomeFragment incomeFragment;
- private PayFragment payFragment;
- FragmentManager fm;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_second);
- initview();
- }
- private void initview(){
- pay = (Button) findViewById(R.id.pay);
- income = (Button) findViewById(R.id.income);
- pay.setOnClickListener(this);
- income.setOnClickListener(this);
- fm = getSupportFragmentManager();
- }
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- switch (v.getId()) {
- case R.id.income:
- setTabSelection(0);
- break;
- case R.id.pay:
- setTabSelection(1);
- break;
- }
- }
- private void setTabSelection(int index){
- FragmentTransaction ft = fm.beginTransaction();
- hideFragment(ft);
- switch (index) {
- case 0:
- if(incomeFragment==null){
- incomeFragment = new IncomeFragment();
- ft.add(R.id.fl, incomeFragment);
- }else{
- ft.show(incomeFragment);
- }
- break;
- case 1:
- if(payFragment==null){
- payFragment = new PayFragment();
- ft.add(R.id.fl, payFragment);
- }
- ft.show(payFragment);
- break;
- }
- ft.commit();
- }
- //用于隐藏fragment
- private void hideFragment(FragmentTransaction ft){
- if(incomeFragment!=null){
- ft.hide(incomeFragment);
- }if(payFragment!=null){
- ft.hide(payFragment);
- }
- }
- }