布局 主界面
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:fitsSystemWindows="true" > <!-- 用一个帧布局来占一个位置,目的是给fragment用 --> <FrameLayout android:id="@+id/fl_content" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"></FrameLayout> <include android:id="@+id/include2" layout="@layout/dibu" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_alignParentStart="true"></include> </LinearLayout>
//布局图片的
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:background="#fff" android:layout_width="match_parent" android:layout_height="60dp" android:orientation="horizontal"> <RadioGroup android:id="@+id/rg" android:layout_width="match_parent" android:layout_height="60dp" android:orientation="horizontal"> <RadioButton android:checked="true" android:id="@+id/btn1" android:layout_width="wrap_content" android:layout_height="60dp" android:layout_weight="1" android:background="@drawable/select1" android:button="@null" /> <RadioButton android:id="@+id/btn2" android:layout_width="wrap_content" android:layout_height="60dp" android:layout_weight="1" android:background="@drawable/select2" android:button="@null" /> <RadioButton android:id="@+id/btn3" android:layout_width="wrap_content" android:layout_height="60dp" android:layout_weight="1" android:background="@drawable/select3" android:button="@null" /> <RadioButton android:id="@+id/btn4" android:layout_width="wrap_content" android:layout_height="60dp" android:layout_weight="1" android:background="@drawable/select4" android:button="@null" /> </RadioGroup> </LinearLayout> </LinearLayout>选择器 需要几个写几个 按你图片来
selector
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/abx" android:state_checked="true"></item> <item android:drawable="@drawable/abw"></item> </selector>
Main主界面
package com.example.samsung.yingyindemo.activity; import android.os.Bundle; 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.view.View; import android.widget.FrameLayout; import android.widget.RadioButton; import android.widget.RadioGroup; import com.example.samsung.yingyindemo.R; import com.example.samsung.yingyindemo.fragment.Fragment1; import com.example.samsung.yingyindemo.fragment.Fragment2; import com.example.samsung.yingyindemo.fragment.Fragment3; import com.example.samsung.yingyindemo.fragment.Fragment4; import butterknife.BindView; import butterknife.ButterKnife; import butterknife.OnClick; public class MainActivity extends AppCompatActivity { @BindView(R.id.fl_content) FrameLayout flContent; @BindView(R.id.btn1) RadioButton btn1; @BindView(R.id.btn2) RadioButton btn2; @BindView(R.id.btn3) RadioButton btn3; @BindView(R.id.btn4) RadioButton btn4; @BindView(R.id.rg) RadioGroup rg; private FragmentManager manager; private FragmentTransaction transaction; private Fragment fragment1; private Fragment f2; private Fragment f3; private Fragment f4; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); /** * 拿到事务管理器并开启事务 */ FragmentManager manager = getSupportFragmentManager(); FragmentTransaction transaction = manager.beginTransaction(); /** * 启动默认选中第一个 */ rg.check(R.id.btn1); Fragment f1 = new Fragment1(); transaction.replace(R.id.fl_content, f1); transaction.commit(); } @OnClick({R.id.fl_content, R.id.btn1, R.id.btn2, R.id.btn3, R.id.btn4, R.id.rg}) public void onViewClicked(View view) { manager = getSupportFragmentManager(); transaction = manager.beginTransaction(); switch (view.getId()) { case R.id.fl_content: break; case R.id.btn1: hideFragment(transaction); fragment1 = new Fragment1(); transaction.replace(R.id.fl_content, fragment1); transaction.commit(); break; case R.id.btn2: hideFragment(transaction); f2 = new Fragment2(); transaction.replace(R.id.fl_content, f2); transaction.commit(); break; case R.id.btn3: hideFragment(transaction); f3 = new Fragment3(); transaction.replace(R.id.fl_content, f3); transaction.commit(); break; case R.id.btn4: hideFragment(transaction); f4 = new Fragment4(); transaction.replace(R.id.fl_content, f4); transaction.commit(); break; default: break; } } private void hideFragment(FragmentTransaction transaction) { if (fragment1 != null) { //transaction.hide(f1);隐藏方法也可以实现同样的效果,不过我一般使用去除 transaction.remove(fragment1); } if (f2 != null) { //transaction.hide(f2); transaction.remove(f2); } if (f3 != null) { //transaction.hide(f3); transaction.remove(f3); } if (f4 != null) { //transaction.hide(f4); transaction.remove(f4); } } }