FrameLayout+底部按钮

来源:互联网 发布:淘宝上韩国运动品牌 编辑:程序博客网 时间:2024/05/29 04:45

1.在drawable下创建selector类

<selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:state_checked="true" android:drawable="@color/colorBlue"></item>    <item android:state_checked="false" android:drawable="@color/colorBlack"></item></selector>

2.在values->color文件下写两个颜色

<color name="colorBlue">#0ff</color><color name="colorBlack">#000</color>

3.主布局布局文件

<!--主布局--><LinearLayout    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical">    <FrameLayout        android:id="@+id/fl"        android:layout_width="match_parent"        android:layout_height="0dp"        android:layout_weight="9"></FrameLayout>    <RadioGroup        android:id="@+id/rg"        android:layout_width="match_parent"        android:layout_height="0dp"        android:orientation="horizontal"        android:layout_weight="1">        <RadioButton            android:id="@+id/rb1"            android:layout_width="match_parent"            android:layout_height="match_parent"            android:button="@null"            android:layout_weight="1"            android:checked="true"            android:textColor="#fff"            android:background="@drawable/selector"            android:text=""            android:gravity="center"            android:textSize="25sp"/>        <RadioButton            android:id="@+id/rb2"            android:layout_width="match_parent"            android:layout_height="match_parent"            android:button="@null"            android:layout_weight="1"            android:textColor="#fff"            android:background="@drawable/selector"            android:text=""            android:gravity="center"            android:textSize="25sp"/>        <RadioButton            android:id="@+id/rb3"            android:layout_width="match_parent"            android:layout_height="match_parent"            android:layout_weight="1"            android:button="@null"            android:gravity="center"            android:textColor="#fff"            android:background="@drawable/selector"            android:text=""            android:textSize="25sp" />        <RadioButton            android:id="@+id/rb4"            android:layout_width="match_parent"            android:layout_height="match_parent"            android:button="@null"            android:layout_weight="1"            android:textColor="#fff"            android:background="@drawable/selector"            android:text="我的"            android:gravity="center"            android:textSize="25sp"/>    </RadioGroup></LinearLayout>

4.写n个fragment.xml和Fragment.java

......

5.MainActivity类

public class MainActivity extends AppCompatActivity {    private FrameLayout fl;    private RadioGroup rg;    private Fragment01 fragment01;    private Fragment02 fragment02;    private Fragment03 fragment03;    private Fragment04 fragment04;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        //初始化组件       fl = (FrameLayout) findViewById(R.id.fl);        rg = (RadioGroup) findViewById(R.id.rg);        //获取Fragment       fragment01 = new Fragment01();        fragment02 = new Fragment02();        fragment03 = new Fragment03();        fragment04 = new Fragment04();        //开启管理者,添加Fragment       FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();        transaction.add(R.id.fl,fragment01);        transaction.add(R.id.fl,fragment02);        transaction.add(R.id.fl,fragment03);        transaction.add(R.id.fl,fragment04);        //进行显隐切换       transaction.show(fragment01).hide(fragment02).hide(fragment03).hide(fragment04).commit();        //RadioGroup设置监听       rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {            @Override            public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {                switch (i){                    case R.id.rb1:                        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();                        transaction.show(fragment01).hide(fragment02).hide(fragment03).hide(fragment04).commit();                        break;                    case R.id.rb2:                        FragmentTransaction transaction2 = getSupportFragmentManager().beginTransaction();                        transaction2.show(fragment02).hide(fragment01).hide(fragment03).hide(fragment04).commit();                        break;                    case R.id.rb3:                        FragmentTransaction transaction3 = getSupportFragmentManager().beginTransaction();                        transaction3.show(fragment03).hide(fragment02).hide(fragment01).hide(fragment04).commit();                        break;                    case R.id.rb4:                        FragmentTransaction transaction4 = getSupportFragmentManager().beginTransaction();                        transaction4.show(fragment04).hide(fragment02).hide(fragment03).hide(fragment01).commit();                        break;                }            }        });    }}


原创粉丝点击