Android tab页选项卡效果Fragment实现

1、先看效果图



思路: 1、先建布局,上面是FramentLayout容器,下面是RadioGroup容器。

2、通过RadioGroup的OnChange方法改变容器存哪个Fragment,顺便改变RadioButton的背景。

3、Fragment里就可以关联自己的Layout。


2、top页的xml.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/fragments_top_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/rg_top_buttons" />
    <RadioGroup
        android:id="@+id/rg_top_buttons"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal" >

        <RadioButton
            android:id="@+id/rb_top_one"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:button="@null"
            android:checked="true"
            android:gravity="center_horizontal"
            android:text="One"
            android:textSize="10sp" />

        <RadioButton
            android:id="@+id/rb_top_two"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:button="@null"
            android:gravity="center_horizontal"
            android:text="two"
            android:textSize="10sp" />

        <RadioButton
            android:id="@+id/rb_top_three"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:button="@null"
            android:gravity="center_horizontal"
            android:text="three"
            android:textSize="10sp" />
    </RadioGroup>
</RelativeLayout>
3、TopActivity的主要代码

	/**
	 * 按钮组.
	 */
	private RadioGroup mButtonGrounp;
	/**
	 * 按钮id集合.
	 */
	private int radioIds[] = null;
	/**
	 * 碎片集合.
	 */
	private Fragment fragments[] = null;
radioIds = new int[] { R.id.rb_top_one, R.id.rb_top_two,
				R.id.rb_top_three};
		fragments = new Fragment[] { new OneFragment(), new TwoFragment(),
				new ThreeFragment()};
mButtonGrounp.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				for (int i = 0; i < radioIds.length; i++) {
					RadioButton checkedRb = (RadioButton) findViewById(radioIds[i]);
					if (checkedId == radioIds[i]) {
						checkedRb.setBackgroundColor(getResources().getColor(
								R.color.reb));
						changeFragment(fragments[i]);
					} else {
						checkedRb.setBackgroundColor(getResources().getColor(
								R.color.green));
					}
				}
			}
		});

	private void changeFragment(Fragment fragment) {
		FragmentManager fragmentManager = this.getSupportFragmentManager();
		FragmentTransaction fragmentTransaction = fragmentManager
				.beginTransaction();
		if (fragment != null) {
			if (fragment.isAdded()) {
				fragmentTransaction.show(fragment);
			} else {
				fragmentTransaction.replace(R.id.fragments_top_container,
						fragment);
			}
		}
		fragmentTransaction.commit();

	}
4、Fragment的主要代码.

public class OneFragment extends Fragment {
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View view = inflater.inflate(R.layout.fragment_one, container, false);
        return view;
    }}

Android开发交流群 QQ群号223919243 (未满)


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值