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

本文介绍了一种在Android中使用RadioGroup动态添加RadioButton的方法,并详细展示了如何根据不同的API级别调整RadioButton的样式,确保了应用程序在不同版本的Android系统上具有一致的外观。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<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);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值