【问题标题】:Two buttons side-by-side with auto fill horizontally并排的两个按钮,水平自动填充
【发布时间】:2020-12-26 02:12:32
【问题描述】:

这是我想要实现的目标:如果按钮标题很短,则按钮的宽度应该相等,但较长的文本应该以牺牲另一个按钮为代价来拉伸相应的按钮。

这是我的布局,适用于 Fragment 或 Activity 的布局。但它没有正确显示在 AlertDialog 的自定义视图中使用

<androidx.gridlayout.widget.GridLayout
    android:layout_width="match_parent"
    app:columnCount="2"
    app:rowCount="1"
    app:alignmentMode="alignBounds"
    app:useDefaultMargins="true"
    android:layout_height="wrap_content">

    <com.google.android.material.button.MaterialButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="0dp"
        app:layout_columnWeight="1"
        app:layout_gravity="fill_horizontal"
        android:text="Cancel" />

    <com.google.android.material.button.MaterialButton
        app:layout_columnWeight="1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_gravity="fill_horizontal"
        android:text="Very long button title" />
</androidx.gridlayout.widget.GridLayout>

这是失败的结果,显示在布局检查器中。关于如何在这种情况下使这种布局起作用的任何想法?

【问题讨论】:

    标签: android android-layout android-alertdialog android-gridlayout


    【解决方案1】:

    在这里复制布局以存档您想要的结果。

    <androidx.constraintlayout.widget.ConstraintLayout 
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <androidx.constraintlayout.widget.Guideline
            android:id="@+id/guideline"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            app:layout_constraintGuide_percent="0.5" />
    
        <Button
            android:id="@+id/button"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginTop="32dp"
            android:layout_marginEnd="8dp"
            android:text="Button"
            app:layout_constraintEnd_toStartOf="@+id/guideline"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
        <Button
            android:id="@+id/button2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginTop="32dp"
            android:layout_marginEnd="8dp"
            android:text="large text Button"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="@+id/guideline"
            app:layout_constraintTop_toTopOf="parent" />
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    【讨论】:

      猜你喜欢
      • 2014-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-14
      • 2020-12-20
      • 1970-01-01
      • 2020-10-27
      相关资源
      最近更新 更多