赞
踩
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout
- 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"
- android:orientation="vertical"
- tools:context=".activity.MainActivity">
-
- <FrameLayout
- android:id="@+id/frag"
- android:layout_width="match_parent"
- android:layout_height="0dp"
- android:layout_weight="1"
- />
- <RadioGroup
- android:id="@+id/group"
- android:layout_width="match_parent"
- android:layout_height="66dp"
- android:orientation="horizontal"
- >
- <RadioButton
- android:id="@+id/q1"
- android:layout_width="0dp"
- android:layout_weight="1"
- android:text="首页"
- android:button="@null"
- android:gravity="center"
- android:textSize="20dp"
- android:background="@drawable/selects"
- android:layout_height="66dp" />
- <RadioButton
- android:id="@+id/q2"
- android:layout_width="0dp"
- android:layout_weight="1"
- android:text="服务"
- android:button="@null"
- android:gravity="center"
- android:textSize="20dp"
- android:background="@drawable/selects"
- android:layout_height="66dp" />
- <RadioButton
- android:id="@+id/q3"
- android:layout_width="0dp"
- android:layout_weight="1"
- android:text="我的"
- android:button="@null"
- android:gravity="center"
- android:textSize="20dp"
- android:background="@drawable/selects"
- android:layout_height="66dp" />
- </RadioGroup>
-
- </LinearLayout>
- package com.bwie.moni7.activity;
-
- import android.support.v4.app.FragmentManager;
- import android.support.v4.app.FragmentTransaction;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.FrameLayout;
- import android.widget.RadioGroup;
-
- import com.bwie.moni7.R;
- import com.bwie.moni7.frag.FragmentOne;
- import com.bwie.moni7.frag.FragmentThree;
- import com.bwie.moni7.frag.FragmentTwo;
-
- public class MainActivity extends AppCompatActivity {
-
- private FrameLayout frag;
- private FragmentManager manager;
- private FragmentOne one;
- private FragmentTwo two;
- private FragmentThree three;
- private FragmentTransaction transaction;
- private RadioGroup group;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- frag = findViewById(R.id.frag);
- group = findViewById(R.id.group);
-
- manager = getSupportFragmentManager();
-
- one = new FragmentOne();
- two = new FragmentTwo();
- three = new FragmentThree();
-
- transaction = manager.beginTransaction();
-
- transaction.add(R.id.frag,one);
- transaction.add(R.id.frag,two);
- transaction.add(R.id.frag,three);
-
- transaction.show(one).hide(two).hide(three);
-
- transaction.commit();
-
- group.check(group.getChildAt(0).getId());
-
- group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
- @Override
- public void onCheckedChanged(RadioGroup group, int checkedId) {
- FragmentTransaction transaction1 = manager.beginTransaction();
- switch (checkedId){
- case R.id.q1:
- transaction1.show(one).hide(two).hide(three);
- break;
- case R.id.q2:
- transaction1.show(two).hide(one).hide(three);
- break;
- case R.id.q3:
- transaction1.show(three).hide(one).hide(two);
- break;
- }
- transaction1.commit();
-
-
- }
- });
-
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。