如何在材质按钮上编写多行文本?

Hil*_*ard 4 xml user-interface android materialbutton

我需要制作一个多行文本材质按钮。我遵循了这个问题的答案,但事实证明材质按钮的工作方式不同。

<com.google.android.material.button.MaterialButtonToggleGroup
    android:id="@+id/toggle_parent_child"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginStart="16dp"
    android:layout_marginEnd="16dp"
    android:theme="@style/Theme.MaterialComponents"
    android:visibility="gone"
    app:checkedButton="@id/button_parent"
    app:singleSelection="true">

    <com.google.android.material.button.MaterialButton
        android:id="@+id/button_parent"
        style="@style/Login.Button.ToggleButton"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Parent\n Device" />

    <com.google.android.material.button.MaterialButton
        android:id="@+id/button_child"
        style="@style/Login.Button.ToggleButton"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Child \n Device" />
Run Code Online (Sandbox Code Playgroud)

我得到这个:

图像

但我需要每个单词都在不同的行中,如下所示:

图像

Lou*_*uis 6

可能会晚一些,但这是我的解决方案

class MonsterButtonToggleGroup : MaterialButtonToggleGroup {

    constructor(context: Context) : super(context)
    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)
    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)


    override fun addView(child: View?, index: Int, params: ViewGroup.LayoutParams?) {
        super.addView(child, index, params)
        if (child is MaterialButton)
            child.maxLines = 2
    }

}
Run Code Online (Sandbox Code Playgroud)