【android】RadioGroup+RadioButton+HorizontalScrollview实现顶部横向菜单栏

1.获取horizontalscrollview、radiogroup控件

//获取mRadioGroup horizontalscrollview布局
LayoutInflater inflater = (LayoutInflater)getSystemService(this.LAYOUT_INFLATER_SERVICE);
View view =inflater.inflate(R.layout.product_view, null);
mRadioGroup = (RadioGroup) view.findViewById(R.id.radio_group);
mHorizontalScrollView = (HorizontalScrollView) findViewById(R.id.horizontalscrollview);

2.设置控件参数

//parentItemArr为商品类别对象集合
for (int i =0;i//添加radiobutton及设置参数(方便动态加载radiobutton)
            RadioButton rb  = new RadioButton(MainActivity.this);
            //根据下标获取商品类别对象
            Result itemArr = (Result) ParentItemArr.get(i);
            rb.setText(itemArr.getName());
            rb.setTextSize(13);
            rb.setGravity(Gravity.CENTER);
            //根据需要设置显示初始标签的个数,这里显示4个
            rb.setLayoutParams(new ViewGroup.LayoutParams((int)(screenWidth/4.5), ViewGroup.LayoutParams.FILL_PARENT));
            rb.setBackgroundResource(R.drawable.radiobutton_bg_selector);
            //**原生radiobutton是有小圆点的,要去掉圆点而且最好按以下设置,设置为null的话在4.x.x版本上依然会出现**
            rb.setButtonDrawable(new ColorDrawable(Color.TRANSPARENT));
rb.setTextColor(MainActivity.this.getResources().getColorStateList(R.color.check_txt_color));
            //向radiogroup中添加radiobutton
            mRadioGroup.addView(rb);
        }
        //设置初始check对象(第一个索引从0开始)
        ((RadioButton)mRadioGroup.getChildAt(0)).setChecked(true);
        //监听check对象
        mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                int RadiobuttonId = group.getCheckedRadioButtonId();
                //获取radiobutton对象
                RadioButton bt = (RadioButton) group.findViewById(RadiobuttonId);
                //获取单个对象中的位置
                int index = group.indexOfChild(bt);
                //设置滚动位置,可使点击radiobutton时,将该radiobutton移动至第二位置
                mHorizontalScrollView.smoothScrollTo(bt.getLeft() - (int) (screenWidth / 4.5), 0);
                item_check_ID = index;
                //根据点击的radiobutton跳转至不同webview界面
                webUrlRefresh(ParentItemArr, index);
            }
        });
        //在mHorizontalScrollView加载mRadioGroup布局
        mHorizontalScrollView.addView(mRadioGroup);

3.布局文件清单
(1)、product_view.xml


<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="horizontal"
        android:id="@+id/radio_group">
RadioGroup>

(2)activity_main.xml(部分)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/scroll_view"
            android:layout_width="fill_parent"
            android:layout_height="30dp"
            android:orientation="horizontal"
            android:layout_below="@+id/nav_bar_top">
                <LinearLayout
                    android:layout_weight="7"
                    android:layout_width="0dp"
                    android:layout_height="fill_parent"
                    android:orientation="vertical"
                    android:background="@android:color/white"
                    android:gravity="center">
                    <HorizontalScrollView
                        android:id="@+id/horizontalscrollview"
                        android:layout_width="wrap_content"
                        android:layout_height="fill_parent"
                        android:fillViewport="false"
                        android:scrollbars="none">
                    HorizontalScrollView>
                LinearLayout>
             LinearLayout>

(3)radiobutton_bg_selector.xml


<selector  xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_enabled="true"
        android:state_checked="true"
        android:drawable="@drawable/item_click_short" />
    <item
        android:state_enabled="true"
        android:state_checked="false"
        android:drawable="@color/com_facebook_share_button_text_color" />
selector >

(4)check_txt_color.xml


<selector  xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:color="@color/click_txt" >item>
    <item android:state_checked="true" android:color="@color/click_txt" >item>
    <item android:state_checked="false" android:color="@color/logo_bg">item>
selector >

4.效果图

你可能感兴趣的:(android控件)