我试图从 4 个选项(例如工具 1、工具 2、工具 3、工具 4)中获取单个用户输入。 早些时候我使用了 radio-group,后来我发现了一个很棒的 UI 设计。所以我尝试使用 Material Toggle Button 来元素化。 https://material.io/components/buttons/android#toggle-button
所以我想我会将它们放置为:
<块引用> Tool 1 Tool 2
Tool 3 Tool 4
但正如我所提到的,我有 4 个按钮,它们被放置在一行(水平)上并且不显示全文。 例如:
<块引用>工具 1 工具 2 工具 3 到...
我尝试添加约束布局/表格布局等布局,但它们不起作用,我认为不支持。 例如:
<com.google.android.material.button.MaterialButtonToggleGroup
android:id="@+id/toggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Constraint-layout
....>
<Button ...../>
<Button ...../>
<Button ...../>
<Button ...../>
</Constraint-layout>
</com.google.android.material.button.MaterialButtonToggleGroup>
答案 0 :(得分:0)
根据MaterialButtonToggleGroup documentation,它是这样说的:
<块引用>一组相关的、可切换的 MaterialButton 的通用容器。该组中的 MaterialButton 将显示在一行中。
还有这个:
<块引用>MaterialButtonToggleGroup 是一个 LinearLayout。如果使用 VERTICAL,建议使用 android:layout_width="MATCH_PARENT" 并删除 android:insetBottom android:insetTop。
据说您无法使用任何 MaterialButtonToggleGroup
属性来根据需要更改方向。对此有一个解决方法,但我不确定这样做的可能性有多大。
您可以在 XML 代码中执行此操作:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:orientation="horizontal">
<com.google.android.material.button.MaterialButtonToggleGroup
android:id="@+id/toggleButton"
android:layout_width="wrap_content"
android:orientation="vertical"
android:layout_height="wrap_content">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1"
style="?attr/materialButtonOutlinedStyle"
/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 3"
style="?attr/materialButtonOutlinedStyle"
/>
</com.google.android.material.button.MaterialButtonToggleGroup>
<com.google.android.material.button.MaterialButtonToggleGroup
android:id="@+id/toggleButton1"
android:layout_width="wrap_content"
android:orientation="vertical"
android:layout_height="wrap_content">
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 2"
style="?attr/materialButtonOutlinedStyle"
/>
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 4"
style="?attr/materialButtonOutlinedStyle"
/>
</com.google.android.material.button.MaterialButtonToggleGroup>
</LinearLayout>
然后你会得到这个:
您需要解决的下一个问题是处理每个切换组上的点击事件。因为当您点击一组时,另一组也需要做出回应。这需要与听众一起完成。这里有文档:https://developer.android.com/reference/com/google/android/material/button/MaterialButtonToggleGroup#addonbuttoncheckedlistener
答案 1 :(得分:0)
这对 MaterialToggleGroup
和 MaterialButton
是不可能的,对于具有不规则排列的多列和多行,我建议您使用 TableLayout
,并使用选择器 Drawable
来反映哪个项目被选中,