1、布局文件
<?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="com.example.viewpager_fragmentdemo.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rd1"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:background="#000"
android:button="@null"
android:checked="true"
android:gravity="center"
android:text="首页"
android:textColor="#fff"
android:textSize="20sp" />
<View
android:layout_width="2dp"
android:layout_height="match_parent"
android:background="#fff"></View>
<RadioButton
android:id="@+id/rd2"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:background="#000"
android:button="@null"
android:gravity="center"
android:text="发现"
android:textColor="#fff"
android:textSize="20sp" />
</RadioGroup>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="2dp"
android:gravity="center">
<TextView
android:id="@+id/tv1"
android:layout_width="70dp"
android:layout_height="2dp" />
<TextView
android:id="@+id/tv2"
android:layout_width="70dp"
android:layout_height="2dp" />
</LinearLayout>
</LinearLayout>
<FrameLayout
android:id="@+id/fl"
android:layout_width="match_parent"
android:layout_height="match_parent"></FrameLayout>
</LinearLayout>
2、MainActivity代码
package com.example.viewpager_fragmentdemo;
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.CompoundButton;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.TextView;
import com.example.viewpager_fragmentdemo.fragment.Fragment1;
import com.example.viewpager_fragmentdemo.fragment.Fragment2;
public class MainActivity extends AppCompatActivity {
private RadioButton rd1;
private RadioButton rd2;
private FrameLayout fl;
private TextView tv1;
private TextView tv2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rd1 = findViewById(R.id.rd1);
rd2 = findViewById(R.id.rd2);
fl = findViewById(R.id.fl);
tv1 = findViewById(R.id.tv1);
tv2 = findViewById(R.id.tv2);
boolean checked = rd1.isChecked();
if (checked){
FragmentManager supportFragmentManager1 = getSupportFragmentManager();
FragmentTransaction fragmentTransaction1 = supportFragmentManager1.beginTransaction();
fragmentTransaction1.replace(R.id.fl,new Fragment1());
fragmentTransaction1.commit();
rd1.setTextColor(getResources().getColor(R.color.colorAccent));
tv1.setBackgroundColor(getResources().getColor(R.color.colorAccent));
}
rd1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
setTitleColor();
FragmentManager supportFragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fl,new Fragment2());
fragmentTransaction.commit();
rd2.setTextColor(getResources().getColor(R.color.colorAccent));
tv2.setBackgroundColor(getResources().getColor(R.color.colorAccent));
}
});
rd2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
setTitleColor();
FragmentManager supportFragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fl,new Fragment1());
fragmentTransaction.commit();
rd1.setTextColor(getResources().getColor(R.color.colorAccent));
tv1.setBackgroundColor(getResources().getColor(R.color.colorAccent));
}
});
}
public void setTitleColor(){
rd1.setTextColor(getResources().getColor(R.color.colorWhite));
rd2.setTextColor(getResources().getColor(R.color.colorWhite));
tv1.setBackgroundColor(getResources().getColor(R.color.colorWhite));
tv2.setBackgroundColor(getResources().getColor(R.color.colorWhite));
}
}
3、建立Fragment省略