private RadioGroup rg; private RadioButton bt1,bt2,bt3,bt4; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.mainfragment01, container, false); bt1 = view.findViewById(R.id.bt1); bt2 = view.findViewById(R.id.bt2); bt3 = view.findViewById(R.id.bt3); bt4 = view.findViewById(R.id.bt4); rg = view.findViewById(R.id.rg); getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.frag,new Fragment01()).commit(); bt1.setTextColor(Color.RED); bt2.setTextColor(Color.BLACK); bt3.setTextColor(Color.BLACK); bt4.setTextColor(Color.BLACK); rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) { switch (i){ case R.id.bt1: getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.frag,new Fragment01()).commit(); bt1.setTextColor(Color.RED); bt2.setTextColor(Color.BLACK); bt3.setTextColor(Color.BLACK); bt4.setTextColor(Color.BLACK); break; case R.id.bt2: getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.frag,new Fragment02()).commit(); bt1.setTextColor(Color.BLACK); bt2.setTextColor(Color.RED); bt3.setTextColor(Color.BLACK); bt4.setTextColor(Color.BLACK); break; case R.id.bt3: getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.frag,new Fragment03()).commit(); bt1.setTextColor(Color.BLACK); bt2.setTextColor(Color.BLACK); bt3.setTextColor(Color.RED); bt4.setTextColor(Color.BLACK); break; case R.id.bt4: getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.frag,new Fragment04()).commit(); bt1.setTextColor(Color.BLACK); bt2.setTextColor(Color.BLACK); bt3.setTextColor(Color.BLACK); bt4.setTextColor(Color.RED); break; } } }); return view;}
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <FrameLayout android:id="@+id/frag" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="9" /> <RadioGroup android:id="@+id/rg" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <RadioButton android:id="@+id/bt1" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:button="@null" android:gravity="center" android:text="首页" android:textSize="20sp" /> <RadioButton android:id="@+id/bt2" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:button="@null" android:gravity="center" android:text="自选" android:textSize="20sp" /> <RadioButton android:id="@+id/bt3" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:button="@null" android:gravity="center" android:text="行情" android:textSize="20sp" /> <RadioButton android:id="@+id/bt4" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:button="@null" android:gravity="center" android:text="咨询" android:textSize="20sp" /> </RadioGroup> </LinearLayout>