使用rediobutton实现多个标题横向划动

<HorizontalScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="3dp"
    android:scrollbars="none">

    <RadioGroup
        android:id="@+id/rg_homefragment"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_marginTop="4dp"
        android:orientation="horizontal"
        android:paddingLeft="2dp"
        android:paddingRight="2dp">
    </RadioGroup>

</Horizo
//获取区域
public void getAreaData(final List<String> areaString) {
    radioGroup.removeAllViews();
    for (int i = 0; i < areaString.size(); i++) {
        RadioButton radioButton = new RadioButton(MainActivity.this);
        radioButton.setText(areaString.get(i));
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(75, ViewGroup.LayoutParams.MATCH_PARENT);
        radioButton.setLayoutParams(params);
        if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) {
            try {
                Field field = radioButton.getClass().getSuperclass().getDeclaredField("mButtonDrawable");
                field.setAccessible(true);
                field.set(radioButton, null);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) {
            radioButton.setButtonDrawable(null);
        }
        radioButton.setBackgroundResource(R.drawable.selector_homebg);
       /* if (i == 0) {
            radioButton.setBackgroundResource(R.drawable.selector_shop_show_left_bg);
        }
        radioButton.setBackgroundResource(R.drawable.selector_shop_show_centre_bg);

        if (i == areaString.size() - 1) {
            radioButton.setBackgroundResource(R.drawable.selector_shop_show_right_bg);
        }*/
        ColorStateList colors = getResources().getColorStateList(R.color.selector_home);
        radioButton.setTextColor(colors);
        radioButton.setGravity(Gravity.CENTER | Gravity.CENTER_VERTICAL);

        final int finalI = i;
        radioButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            }
        });
        radioGroup.addView(radioButton);
    }
    //设置第一个的背景
    radioGroup.getChildAt(0).setBackgroundResource(R.drawable.item_home_select);
    radioGroup.getChildAt(0).performClick();
}
ntalScrollView>
radioGroup = findViewById(R.id.rg_homefragment);
 list = new ArrayList<>();
 for (int i = 0; i < 10; i++) {
     list.add("title" + i);
 }
 getAreaData(list);
使用scroll-view让多个view横向滑动的方法如下: 1. 在scroll-view组件上设置scroll-x属性为true,表示横向滚动。 2. 在scroll-view组件上设置class属性为"moduleView",并在对应的wxss样式文件中添加.moduleView样式,设置white-space为nowrap,表示子view不换行。 3. 在子view组件上设置class属性为"modulItemView",并在对应的wxss样式文件中添加.modulItemView样式,设置display为inline-block,表示子view以行内块元素的方式排列。 示例代码如下: ```html <scroll-view scroll-x class="moduleView"> <view class="modulItemView">1</view> <view class="modulItemView">2</view> <view class="modulItemView">3</view> </scroll-view> ``` 在上述代码中,scroll-view组件设置了scroll-x属性为true,表示横向滚动。同时,scroll-view组件的class属性为"moduleView",子view组件的class属性为"modulItemView"。在对应的wxss样式文件中,添加了.moduleView样式和.modulItemView样式,分别设置了white-space为nowrap和display为inline-block。 这样,多个子view就可以在scroll-view组件中横向滑动了。 #### 引用[.reference_title] - *1* [uniapp - 使用 <scroll-view> 内置组件,实现容器 “横向 / 纵向“ 区域内滚动功能(详细示例教程)](https://blog.csdn.net/weixin_44198965/article/details/129857990)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [微信小程序 scroll-view设置scroll-x无法横向显示和滑动的解决方案](https://blog.csdn.net/wshpwangshiyu/article/details/130964682)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值