Android中自定义控件SegmentedGroup

GitHub:https://github.com/Kaopiz/android-segmented-control

一 、添加依赖

implementation 'info.hoang8f:android-segmented:1.0.6'

二、布局中使用

<info.hoang8f.android.segmented.SegmentedGroup
        app:sc_border_width="2dp"
        app:sc_checked_text_color="#f00"
        app:sc_corner_radius="5dp"
        app:sc_tint_color="#fff"
        android:background="@color/colorAccent"
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="40dp">

        <RadioButton
            android:id="@+id/button1"
            android:textSize="18sp"
            android:button="@null"
            style="@style/RadioButton"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="CSDN"/>

        <RadioButton
            android:id="@+id/button2"
            android:textSize="18sp"
            android:button="@null"
            style="@style/RadioButton"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="Android"/>

    </info.hoang8f.android.segmented.SegmentedGroup>

 三、其他设置

1. 当按方法检查按钮时,您也可以更改色调颜色和标题颜色setTintColor

mSg.setTintColor(R.color.radio_button_selected_color);
mSg.setTintColor(Color.parseColor("#FFD0FF3C"), Color.parseColor("#FF7B07B2"));

2. 如果你不指定border_width和/或corner_radius,将使用默认值(border_width为1dp,corner_radius为5dp)。

3. 其他的逻辑方法就按照RadioGroup使用即可。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可自定义样式、功能全面的分段控件。项目地址:https://github.com/klongmitre/android-segmented-control-view效果图:如何使用xml直接创建<org.mitre.ascv.AndroidSegmentedControlView         android:id="@ id/androidSegmentedControlView"         android:layout_width="match_parent"         android:layout_height="wrap_content"         ascv:ascv_defaultSelection="0"         ascv:ascv_unselectedTextColor="@color/test_attr_unselected_text_color"         ascv:ascv_selectedTextColor="@color/test_attr_selected_text_color"         ascv:ascv_selectedColor="@color/test_attr_selected_color"         ascv:ascv_unselectedColor="@color/test_attr_unselected_color"         ascv:ascv_items="@array/three_state_option"/>2. java添加监听器,监听item的切换ascv = (AndroidSegmentedControlView)this.findViewById(R.id.androidSegmentedControlView); ascv.setIdentifier("ascv01"); //ascv.setItems(new String[]{"Test1aaaaa", "Test2", "Test3"}, new String[]{"1", "2", "3"}); ascv.setOnSelectionChangedListener(new OnSelectionChangedListener(){     @Override     public void newSelection(String identifier, String value) {//当item切换时触发  Toast.makeText(MainActivity.this, "identifier:" identifier "  value:" value, Toast.LENGTH_SHORT).show();     } });参数identifier是当有多个分段控件时,同时使用一个监听器时,用于区别是哪个触发了事件。属性说明属性名类型使用说明ascv_unselectedTextColorreference未选的item的文字颜色ascv_unselectedColorreference未选的item的背景颜色,不包括边框ascv_selectedColorreference选的item背景的颜色以及边框的颜色ascv_selectedTextColorreference选的item的文字颜色ascv_itemsreference控件item上显示的文字ascv_valuesreference控件item的值,会被传给监听器。未设置时,默认使用ascv_items。ascv_equalWidthboolean当item上的文字,即ascv_items设置的文字,长度不一致时,item的宽度是否还等长。ascv_stretchboolean是否被拉伸。即控件是否填充整个父容器。ascv_defaultSelectioninteger默认哪个item被选,下标从0开始ascv_identifierstring控件的ID
Android Studio,您可以通过创建自定义视图类来实现自定义控件。下面是一些步骤,帮助您开始创建自己的自定义控件: 1. 创建一个新的Java类,该类将扩展现有的Android视图类(例如,TextView、Button等),或者直接扩展View类以创建一个全新的自定义视图。 2. 在类实现构造函数和必要的方法。您可能需要重写onDraw()方法来处理绘制自定义视图的逻辑。还可以重写其他方法(例如,onMeasure()、onLayout()等),以便根据需要对自定义视图进行测量和布局。 3. 在XML布局文件使用您的自定义控件。在布局文件添加一个对应于您自定义控件类的标签,并根据需要设置属性。例如,如果您的自定义控件具有自定义属性,您可以在XML设置这些属性。 4. 在Java代码使用您的自定义控件。在Activity或Fragment,通过findViewById()方法找到布局文件自定义控件,并使用它们。 5. 可选步骤:如果您的自定义控件需要处理用户交互事件(例如,点击事件),您可以重写相应的方法(例如,onTouchEvent())来实现所需的功能。 这只是一个简单的指南,让您了解如何在Android Studio创建自定义控件。在实际开发过程,您可能需要更多的步骤和代码来实现您的具体需求。您可以参考Android开发文档和其他教程,以获取更详细的指导和示例代码。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值