【发布时间】:2020-02-12 16:33:28
【问题描述】:
我正在尝试创建一个带有材质按钮的 MaterialButtonToggleGroup,我想使用自定义样式来实现。
我能够使用 xml 创建它,但是,我需要以编程方式实现相同的结果
这是我对 xml 所做的(我使用 LinearLayout 作为根视图):
<com.google.android.material.button.MaterialButtonToggleGroup
android:id="@+id/group_item_size"
style="@style/ButtonsToggleGroupStyle"
>
<com.google.android.material.button.MaterialButton
style="@style/ButtonStyle"
android:text="Small"
/>
<com.google.android.material.button.MaterialButton
style="@style/ButtonStyle"
android:text="Large"
/>
<com.google.android.material.button.MaterialButton
style="@style/ButtonStyle"
android:text="Family"
/>
</com.google.android.material.button.MaterialButtonToggleGroup>
以及样式代码:
<style name="ButtonStyle" parent="Widget.MaterialComponents.Button.UnelevatedButton">
<item name="cornerRadius">@dimen/margin_20dp</item>
<item name="android:padding">@dimen/margin_8dp</item>
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">match_parent</item>
<item name="android:layout_weight">1</item>
<item name="android:textSize">@dimen/text_size_14sp</item>
<item name="android:textAllCaps">false</item>
<item name="android:textColor">@drawable/drawable_button_toggle_group_text_selector</item>
<item name="android:backgroundTint">@drawable/drawable_button_toggle_group_selector</item>
</style>
<style name="ButtonsToggleGroupStyle">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_gravity">center</item>
<item name="android:orientation">horizontal</item>
<item name="android:layout_marginStart">@dimen/margin_32dp</item>
<item name="android:layout_marginTop">@dimen/margin_12dp</item>
<item name="android:layout_marginEnd">@dimen/margin_32dp</item>
</style>
提前感谢您的帮助?????????
【问题讨论】:
标签: android android-layout android-button material-components-android android-togglebutton