RadioButton 带下划线切换的案例
xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout android:id="@+id/relOrder" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@color/White" xmlns:android="http://schemas.android.com/apk/res/android"> <RadioGroup android:id="@+id/rgTabTitle" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:paddingBottom="@dimen/text_padding_small" android:paddingTop="@dimen/text_padding_small" > <RadioButton android:id="@+id/rbDoneOrder" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1.0" android:checked="true" android:button="@null" android:gravity="center" android:padding="0dp" android:text="@string/order_done" android:textColor="@drawable/sel_tab_text_color" android:textSize="@dimen/content_text_size" /> <RadioButton android:id="@+id/rbGoingOrder" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1.0" android:button="@null" android:gravity="center" android:padding="0dp" android:text="@string/order_going" android:textColor="@drawable/sel_tab_text_color" android:textSize="@dimen/content_text_size" /> </RadioGroup> <LinearLayout android:id="@+id/indicatorLayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/rgTabTitle" android:orientation="horizontal" android:weightSum="2" > <View android:id="@+id/indicatorView" android:layout_width="0dp" android:layout_height="@dimen/indicator_height" android:layout_weight="1.0" android:background="@color/bg_deep_color" /> </LinearLayout> <View android:id="@+id/line1" android:layout_width="match_parent" android:layout_height="@dimen/divider_line_width" android:layout_below="@id/indicatorLayout" android:background="@color/divider_line_color" /> <FrameLayout android:id="@+id/frContractsHistory" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@id/line1" android:layout_marginTop="0dp" android:layout_marginRight="@dimen/outer_border_width" android:layout_marginLeft="@dimen/outer_border_width" android:background="@color/White" > </FrameLayout> </RelativeLayout>
code:
mIndicator = mRootView.findViewById(R.id.indicatorView);//underlne WindowManager wm = (WindowManager) context //the the width of screen .getSystemService(Context.WINDOW_SERVICE); DisplayMetrics outMetrics = new DisplayMetrics(); wm.getDefaultDisplay().getMetrics(outMetrics); mIndicatorStepDistance = outMetrics.widthPixels; mFm =getFragmentManager(); FragmentTransaction mFragmentTrans =mFm.beginTransaction(); mFragmentTrans.add(R.id.frContractsHistory, new OrderContractsLvFragment(R.layout.list_item_contract_done)).commit(); mRgTabs.setOnCheckedChangeListener(new OnCheckedChangeListener(){ @Override public void onCheckedChanged(RadioGroup container, int checkedId) { FragmentTransaction mFragmentTrans =mFm.beginTransaction(); TranslateAnimation anim = null; switch(checkedId){ case R.id.rbDoneOrder: Toast.makeText(mContext, "done", Toast.LENGTH_LONG).show(); mFragmentTrans.replace(R.id.frContractsHistory, new OrderContractsLvFragment(R.layout.list_item_contract_done)); mFragmentTrans.commit(); anim = new TranslateAnimation(mIndicatorStepDistance * mIndicatorPosition, 0, 0, 0); mIndicatorPosition = 0; break; case R.id.rbGoingOrder: Toast.makeText(mContext, "going", Toast.LENGTH_LONG).show(); mFragmentTrans.replace(R.id.frContractsHistory, new OrderContractsLvFragment(R.layout.list_item_contract_ongoing)); mFragmentTrans.commit(); anim = new TranslateAnimation(mIndicatorStepDistance * mIndicatorPosition, mIndicatorStepDistance, 0, 0); mIndicatorPosition = 1; break; } if (anim != null) { anim.setDuration(300); anim.setFillAfter(true); mIndicator.startAnimation(anim); } } });