Android: MaterialButtonToggleGroup中的按钮不显示在屏幕上



我试图使用MaterialButtonGroup标签添加两个按钮到我的布局,但是它们没有显示在我的应用程序或Android Studio的设计视图中。

这是在我的build.gradle。我试了几个不同的版本。

implementation 'com.google.android.material:material:1.6.1'

这是我的xml

<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
>
<data>
<import type="android.view.View" />
<import type="androidx.core.content.ContextCompat" />
<import type="android.widget.Button"/>
</data>
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/backgroundWindow">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginTop="16dp"
app:cardBackgroundColor="@color/backgroundCollection"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textView_code"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Code"
android:textSize="18sp"
android:layout_marginTop="@dimen/margin_8dp"
android:layout_marginStart="@dimen/margin_8dp"
android:textColor="@color/black"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<include layout="@layout/layout_seperator"
app:layout_constraintTop_toBottomOf="@id/textView_code"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"/>
<com.google.android.material.button.MaterialButtonToggleGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:checkedButton="@id/b1"
app:selectionRequired="true"
app:singleSelection="true"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/textView_code"
app:layout_constraintEnd_toEndOf="parent">
<Button
style="?attr/materialButtonOutlinedStyle"
android:id="@+id/b1"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="OPT1" />
<Button
style="?attr/materialButtonOutlinedStyle"
android:id="@+id/b2"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="OPT2" />
</com.google.android.material.button.MaterialButtonToggleGroup>

</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>



</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
</layout>

这是我在app (Imgur link)中看到的

我试过将按钮移动到不同的布局,设置不同的宽度/高度,可见性,但到目前为止没有任何帮助。

我有同样的问题,按钮在屏幕上不可见。不过在设计预览版上是可以看到的。

解决方案是将组内的Button切换为com.google.android.material.button.MaterialButton

<com.google.android.material.button.MaterialButtonToggleGroup
android:id="@+id/toggle_button_group"
android:gravity="center_horizontal"
app:singleSelection = "true"
app:selectionRequired="true"
android:orientation="horizontal"
android:padding="4dp"
app:checkedButton="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.button.MaterialButton
style="?attr/materialButtonOutlinedStyle"
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1"
/>
<com.google.android.material.button.MaterialButton
style="?attr/materialButtonOutlinedStyle"
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Button 2"
/>
<com.google.android.material.button.MaterialButton
style="?attr/materialButtonOutlinedStyle"
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 3"
/>
</com.google.android.material.button.MaterialButtonToggleGroup>