<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_checked="true" android:drawable="@drawable/ic_nav_home_press"/> <item android:state_checked="false" android:drawable="@drawable/ic_nav_home"/> <item android:drawable="@drawable/ic_nav_home"/> </selector>
<?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:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.baway.lizongshu.view.activity.MainActivity">
<FrameLayout
android:id="@+id/framelayout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
>
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioGroup
android:id="@+id/rg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/fenlei"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="分类"
android:button="@null"
android:checked="true"
android:drawableTop="@drawable/fenlei"
android:gravity="center"
android:textSize="12sp"
android:tag="0"
/>
<RadioButton
android:id="@+id/gouwuche"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="购物车"
android:button="@null"
android:drawableTop="@drawable/gouwuche"
android:gravity="center"
android:textSize="12sp"
android:tag="1"
/>
<RadioButton
android:id="@+id/qita"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="其他"
android:button="@null"
android:drawableTop="@drawable/qita"
android:gravity="center"
android:textSize="12sp"
android:tag="2"
/>
<RadioButton
android:id="@+id/wode"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="我的"
android:button="@null"
android:drawableTop="@drawable/wode"
android:gravity="center"
android:textSize="12sp"
android:tag="3"
/>
</RadioGroup>
</LinearLayout>
</LinearLayout>
public class FenleiFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fenlei, container, false);
return view;
}
}
public class MainActivity extends AppCompatActivity {
private RadioGroup rg;
private Fragment[] mfragments;
private FragmentManager fm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initview();
initdata();
}
private void initdata() {
//定义一个Fragment数组,存放四个Fragment
mfragments=new Fragment[4];
mfragments[0]=new FenleiFragment();
mfragments[1]=new GouwucheFragment();
mfragments[2]=new QitaFragment();
mfragments[3]=new WodeFragment();
//获得Fragment管理者
fm = getSupportFragmentManager();
//处理
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.framelayout,mfragments[0],"0");
ft.commit();
}
private void initview() {
rg=(RadioGroup) findViewById(R.id.rg);
//RadioGroup的监听事件
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
//找到当前选中的图标
RadioButton rb= (RadioButton) group.findViewById(checkedId);
//找到所选图标的标签并转换为整数类型放到下面的方法中
int i = Integer.parseInt(rb.getTag().toString().trim());
showAndHideFragment(i);
}
});
}
//展示和隐藏Fragment的方法
private void showAndHideFragment(int position) {
FragmentTransaction transaction = fm.beginTransaction();
//如果没有fragment就在framelayout里面加上
if (!mfragments[position].isAdded()){
transaction.add(R.id.framelayout,mfragments[position],""+position);
}
//把所有的fragment设为隐藏
for (Fragment fragment:mfragments){
transaction.hide(fragment);
}
//把选中的设为显示
transaction.show(mfragments[position]);
transaction.commit();
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有