compile 'com.jakewharton:butterknife:7.0.1'
compile 'org.greenrobot:eventbus:3.0.0'
MainActivity布局
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="liu.radiobuttonandfragment.MainActivity">
<FrameLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@mipmap/nav_footer_white"
android:gravity="center"
android:orientation="horizontal">
<RadioGroup
android:id="@+id/rgTools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rbHome"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="right"
android:layout_weight="1"
android:button="@null"
android:checked="true"
android:drawableTop="@drawable/selector_main_btn_home"
android:gravity="center"
android:paddingLeft="0dp"
android:text="首页"/>
<RadioButton
android:id="@+id/rbShop"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:button="@null"
android:checked="false"
android:drawableTop="@drawable/selector_main_btn_shopcart"
android:gravity="center"
android:text="购物车"/>
<RadioButton
android:checked="false"
android:id="@+id/rbMessage"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:button="@null"
android:drawableTop="@drawable/selector_main_btn_message"
android:gravity="center"
android:text="消息"/>
<RadioButton
android:checked="false"
android:id="@+id/rbMine"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:button="@null"
android:drawableTop="@drawable/selector_main_btn_mine"
android:gravity="center"
android:text="我的"/>
</RadioGroup>
</LinearLayout>
</LinearLayout>
复制代码
主要代码
复制代码
private void initFragment() {
//首页
HomeFragment homeFragment =new HomeFragment()
//购物车
ShopcartFragment shopcartFragment =new ShopcartFragment()
//消息
MessageFragment messageFragment =new MessageFragment()
//个人中心
MineFragment mineFragment =new MineFragment()
//添加到数组
mFragments = new Fragment[]{homeFragment,shopcartFragment,messageFragment,mineFragment}
//开启事务
FragmentTransaction ft =
getSupportFragmentManager().beginTransaction()
//添加首页
ft.add(R.id.content,homeFragment).commit()
//默认设置为第0个
setIndexSelected(0)
}
private void setIndexSelected(int index) {
if(mIndex==index){
return
}
FragmentManagerfragmentManager = getSupportFragmentManager()
FragmentTransaction ft = fragmentManager.beginTransaction()
//隐藏
ft.hide(mFragments[mIndex])
//判断是否添加
if(!mFragments[index].isAdded()){
ft.add(R.id.content,mFragments[index]).show(mFragments[index])
}else {
ft.show(mFragments[index])
}
ft.commit()
//再次赋值
mIndex=index
}
@OnClick({R.id.rbHome, R.id.rbShop, R.id.rbMessage, R.id.rbMine})
public void onClick(View view) {
switch (view.getId()) {
case R.id.rbHome:
setIndexSelected(0)
break
case R.id.rbShop:
setIndexSelected(1)
break
case R.id.rbMessage:
setIndexSelected(2)
break
case R.id.rbMine:
setIndexSelected(3)
break
}
}
复制代码
直接添加就可以了,放个tpanel,之后在panel选中的情况下,直接往里面放RadioButton就可以了。
排版RadioGroup的RadioButton
1)实际上只要明白在radiogroup里面也可以使用RelativeLayout,LinearLayout这样的布局的;首先设置radiogroup的orientation属性为vertical;
2)然后再第一个radiobutton前面加上LinearLayout,orientation属性设置为horizontal,</LinearLayout>标签放在一行最后一个radiobutton后面;
3)同样的把使用LinearLayout把后面几个radiobutton包裹住,orientation属性设置为horizontal;
4)运行一下就可以发现就达到了想要的结果了。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)