底部栏加Fragment

来源:互联网 发布:怎样在淘宝网购物 编辑:程序博客网 时间:2024/06/08 18:19
package com.bwie.fanmeihua.fragment;import android.support.annotation.IdRes;import android.support.v4.app.Fragment;import android.support.v4.app.FragmentManager;import android.support.v4.app.FragmentTransaction;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.widget.FrameLayout;import android.widget.RadioGroup;import java.util.ArrayList;public class MainActivity extends AppCompatActivity {    ArrayList<Fragment> flist = new ArrayList<>();    private FragmentManager fragmentManager;    private FrameLayout fl;    private RadioGroup rg;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        fragmentManager = getSupportFragmentManager();        //获取控件        fl = (FrameLayout)findViewById(R.id.fl);        rg = (RadioGroup)findViewById(R.id.rg);        init();        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();        fragmentTransaction.add(R.id.fl,flist.get(0)).commit();        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {            @Override            public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {                    switch(i){                            case R.id.r1:                            fragmentManager.beginTransaction().replace(R.id.fl,new Fragment1()).commit();                            break;                        case R.id.r2:                            fragmentManager.beginTransaction().replace(R.id.fl,new Fragment2()).commit();                            break;                        case R.id.r3:                            fragmentManager.beginTransaction().replace(R.id.fl,new Fragment1()).commit();                            break;                        case R.id.r4:                            fragmentManager.beginTransaction().replace(R.id.fl,new Fragment1()).commit();                            break;                        case R.id.r5:                            fragmentManager.beginTransaction().replace(R.id.fl,new Fragment1()).commit();                            break;                        }            }        });    }    private void init() {        for (int i = 0; i <5; i++) {            flist.add(new Fragment1());        }    }}//布局
<?xml version="1.0" encoding="utf-8"?><RelativeLayout    xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"    android:layout_height="match_parent" tools:context="com.bwie.fanmeihua.fragment.MainActivity">    <FrameLayout        android:id="@+id/fl"        android:layout_above="@+id/rg"        android:layout_width="match_parent"        android:layout_height="match_parent"        ></FrameLayout>    <RadioGroup        android:id="@+id/rg"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_alignParentBottom="true"        android:orientation="horizontal">        <RadioButton            android:id="@+id/r1"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_weight="1"            android:button="@null"            android:gravity="center"            android:padding="10dp"            android:text="首页" />        <RadioButton            android:id="@+id/r2"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_weight="1"            android:button="@null"            android:gravity="center"            android:padding="10dp"            android:text="想法" />        <RadioButton            android:id="@+id/r3"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_weight="1.06"            android:button="@null"            android:gravity="center"            android:padding="10dp"            android:text="市场" />        <RadioButton            android:id="@+id/r4"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_weight="1"            android:button="@null"            android:gravity="center"            android:padding="10dp"            android:text="通知" />        <RadioButton            android:id="@+id/r5"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_weight="1"            android:button="@null"            android:gravity="center"            android:padding="10dp"            android:text="更多" />    </RadioGroup></RelativeLayout>