代码如下:
1. xml文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/child_main_acitivity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:orientation="vertical">
<FrameLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_above="@+id/home_tab_host"
android:layout_weight="1"></FrameLayout>
<include layout="@layout/bottom_layout"
android:layout_width="match_parent"
android:layout_height="@dimen/dp_45"/>
</LinearLayout>
2.bottom_layout.xml
<?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="@dimen/dp_45"
android:background="@color/white"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="@dimen/line_height"
android:background="@color/line_color"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/content">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<View
android:layout_width="match_parent"
android:layout_height="1px"
android:background="@color/line_color"/>
</RelativeLayout>
<RadioGroup
android:id="@+id/rg"
android:layout_width="match_parent"
android:layout_height="@dimen/dp_45"
android:orientation="horizontal">
<!--首页-->
<RadioButton
android:id="@+id/rb_home"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:button="@null"
android:checked="true"
android:drawableTop="@drawable/home_fragment_selector"
android:gravity="center"
android:paddingTop="@dimen/dp_5"
android:drawablePadding="@dimen/dp_0"
android:text="@string/home"
android:textColor="@drawable/selector_menu_text"
android:textSize="@dimen/sp_12"/>
<!--消息-->
<RadioButton
android:id="@+id/rb_msg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:button="@null"
android:drawableTop="@drawable/msg_fragment_selector"
android:gravity="center"
android:paddingTop="@dimen/dp_5"
android:drawablePadding="@dimen/dp_0"
android:text="@string/message_txt"
android:textColor="@drawable/selector_menu_text"
android:textSize="@dimen/sp_12"/>
<!--客服-->
<RadioButton
android:id="@+id/rb_service"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:button="@null"
android:drawableTop="@drawable/service_fragment_selector"
android:gravity="center"
android:paddingTop="@dimen/dp_5"
android:drawablePadding="@dimen/dp_0"
android:text="@string/customer_service"
android:textColor="@drawable/selector_menu_text"
android:textSize="@dimen/sp_12"/>
<!--我的-->
<RadioButton
android:id="@+id/rb_mine"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:button="@null"
android:drawableTop="@drawable/mine_fragment_selector"
android:gravity="center"
android:paddingTop="@dimen/dp_5"
android:drawablePadding="@dimen/dp_0"
android:text="@string/mine"
android:textColor="@drawable/selector_menu_text"
android:textSize="@dimen/sp_12"/>
</RadioGroup>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="42dp"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#00000000"
android:visibility="invisible"/>
<Button
android:id="@+id/neiMsg"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#00000000"
android:visibility="invisible"/>
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#00000000"
android:visibility="invisible"/>
<Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#00000000"
android:visibility="invisible"/>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
3.图片选择器:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/kefu_dianji" android:state_pressed="true"/>
<item android:drawable="@drawable/kefu_dianji" android:state_selected="true"/>
<item android:drawable="@drawable/kefu_dianji" android:state_checked="true"/>
<item android:drawable="@drawable/kefu"/>
</selector>
4. acitivity.java文件
public class MainActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener, View.OnClickListener {
public static final String FLAG = "MainActivity";
private LinearLayout child_main_acitivity;
private FrameLayout content;
private RadioGroup mRg;
private RadioButton mRb_home;
private RadioButton mRb_msg;
private RadioButton mRb_service;
private RadioButton mRb_mine;
private Button mNeiMsg;
private FragmentManager fragmentManager;
private FragmentTransaction ft;
private String fragmentTag = "";
private Fragment fragment = null;
private ChildHomeFragment homeFragment; //首页
private MessageFragment msgFragment; //消息
private CustomerServiceFragment csFragment; //客服
private MineFragment userFragment; //我的
private Long firstTime = 0L;
@Override
public int getLayOutID() {
return R.layout.activity_main_child;
}
@Override
protected void initView(Bundle savedInstanceState) {
initFragmentManager();//放的位置不正确,会导致部分白屏一闪而过!
child_main_acitivity = (LinearLayout) findViewById(R.id.child_main_acitivity);
content = (FrameLayout) findViewById(R.id.content);
mRg = (RadioGroup) findViewById(R.id.rg);
mRb_home = (RadioButton) findViewById(R.id.rb_home);
mRb_msg = (RadioButton) findViewById(R.id.rb_msg);
mRb_service = (RadioButton) findViewById(R.id.rb_service);
mRb_mine = (RadioButton) findViewById(R.id.rb_mine);
mNeiMsg = (Button) findViewById(R.id.neiMsg);
}
@Override
protected void initData() {
}
@Override
protected void initEvent() {
mRg.setOnCheckedChangeListener(this);
mRb_home.setOnClickListener(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
FragmentFactory.map.clear();
}
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
Fragment currentFragment = fragmentManager.findFragmentByTag(fragmentTag);
FragmentTransaction tr = fragmentManager.beginTransaction();
tr.hide(currentFragment);
switch (checkedId) {
case R.id.rb_home:
fragment = fragmentManager.findFragmentByTag(ChildHomeFragment.TAG);
fragmentTag = ChildHomeFragment.TAG;
if (fragment == null) {
fragment = new ChildHomeFragment();
}
break;
case R.id.rb_msg:
fragment = fragmentManager.findFragmentByTag(MessageFragment.TAG);
fragmentTag = MessageFragment.TAG;
if (fragment == null) {
fragment = new MessageFragment();
}
break;
case R.id.rb_service:
fragment = fragmentManager.findFragmentByTag(CustomerServiceFragment.TAG);
fragmentTag = CustomerServiceFragment.TAG;
if (fragment == null) {
fragment = new CustomerServiceFragment();
}
break;
case R.id.rb_mine:
fragment = fragmentManager.findFragmentByTag(MineFragment.TAG);
fragmentTag = MineFragment.TAG;
if (fragment == null) {
fragment = new MineFragment();
}
break;
}
if (fragment != null && fragment.isAdded()) {
tr.show(fragment);
} else {
tr.add(R.id.content, fragment, fragmentTag);
}
Constant.SWITCH_FLAG = false;
tr.addToBackStack(null);
tr.commitAllowingStateLoss();
}
//初始化
private void initFragmentManager() {
fragmentTag = ChildHomeFragment.TAG;
fragmentManager = getSupportFragmentManager();
ft = fragmentManager.beginTransaction();
fragment = fragmentManager.findFragmentByTag(ChildHomeFragment.TAG);
fragment = new ChildHomeFragment();
ft.add(R.id.content, fragment, fragmentTag);
ft.addToBackStack(null);
ft.commitAllowingStateLoss();
}
@Override
public void onAttachFragment(Fragment fragment) {
super.onAttachFragment(fragment);
Constant.SWITCH_FLAG = false;
if (homeFragment == null && fragment instanceof ChildHomeFragment) {
homeFragment = (ChildHomeFragment) fragment;
} else if (msgFragment == null && fragment instanceof MessageFragment) {
msgFragment = (MessageFragment) fragment;
} else if (csFragment == null && fragment instanceof CustomerServiceFragment) {
csFragment = (CustomerServiceFragment) fragment;
} else if (userFragment == null && fragment instanceof MineFragment) {
userFragment = (MineFragment) fragment;
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.rb_home:
Logger.e(TAG, "-------------" + Constant.SWITCH_FLAG);
if (Constant.SWITCH_FLAG == true) {
Constant.APP_THEME_COLOR = "#005699";
overridePendingTransition(0, 0);
finish();
} else {
Constant.SWITCH_FLAG = true;
}
break;
default:
break;
}
}
@Override
public void onBackPressed() {
onStateNotSaved();
long secondTime = System.currentTimeMillis();
if (secondTime - firstTime > 1500) {
ToastUtil.show(this, "再按一次退出");
firstTime = secondTime;
} else {
BaseApplication.getInstance().quiteApplication();
}
}
}
照着代码copy,然后再改一下,就可以用了.如果能够帮助到你,请点个赞,谢谢!