MaterialButtonToggleGroup自定义:边框颜色,边框宽度,边框弧度,排列方式,文字按下抬起颜色,背景按下抬起颜色。
搜索好的都是教怎么用,而没有说明怎么替换颜色。我跟了一下style继承关系,替换了想要替换的颜色。
<com.google.android.material.button.MaterialButtonToggleGroup
android:id="@+id/toggleGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:theme="@style/MyMaterialButtonToggleGroup"
app:checkedButton="@id/btn1"
app:singleSelection="true">
<com.google.android.material.button.MaterialButton
android:id="@+id/btn1"
style="@style/MyOutlinedButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="选项一"
android:textSize="@dimen/sp18" />
<com.google.android.material.button.MaterialButton
android:id="@+id/btn2"
style="@style/MyOutlinedButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="选项二"
android:textSize="@dimen/sp18" />
<com.google.android.material.button.MaterialButton
android:id="@+id/btn3"
style="@style/MyOutlinedButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="选项三"
android:textSize="@dimen/sp18" />
com.google.android.material.button.MaterialButtonToggleGroup>
<style name="MyMaterialButtonToggleGroup" parent="Widget.MaterialComponents.MaterialButtonToggleGroup">
- "android:orientation"
>horizontal
- "cornerSizeTopLeft"
>@dimen/dp4
- "cornerSizeTopRight">@dimen/dp4
- "cornerSizeBottomLeft">@dimen/dp4
- "cornerSizeBottomRight">@dimen/dp4
style>
<style name="MyOutlinedButton" parent="Widget.MaterialComponents.Button.OutlinedButton">
- "strokeColor"
>@color/white
- "strokeWidth"
>1dp
- "android:textColor">@drawable/outline_text_color
- "backgroundTint">@drawable/outline_bg_color
style>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:alpha="1.00" android:color="@color/white" android:state_checkable="true" android:state_checked="true" android:state_enabled="true" />
<item android:alpha="1.00" android:color="@color/gray_C" android:state_checkable="true" android:state_checked="false" android:state_enabled="true" />
<item android:alpha="1.00" android:color="@color/white" android:state_enabled="true" />
<item android:alpha="1.00" android:color="@color/white" />
selector>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:alpha="0.3" android:color="@color/white" android:state_checked="true"/>
<item android:alpha="1.00" android:color="@android:color/transparent" android:state_checked="false"/>
selector>