【问题标题】:How to create adjacent Material Buttons with single border in-between如何创建具有单个边框的相邻材质按钮
【发布时间】:2020-01-19 18:24:07
【问题描述】:

我想让两个 Material Buttons 直接相邻,它们之间有一个 1dp 宽的分隔线 like this (imgur)

我尝试在按钮上设置自定义背景可绘制对象,但 Material Buttons no longer allow this.

我的下一个尝试是使用 Material Buttons 的 stroke 属性,但我找不到将笔划应用于单边的方法。

我还尝试在按钮之间插入一个 1dp 宽的视图,这可能是一个解决方案,但我无法以编程方式设置该视图的高度和位置,而不会变得太复杂。

这两个按钮当前位于 ConstraintLayout 中,但我也尝试将它们嵌套在 ConstraintLayout 内的 RelativeLayout 中。

这是在 RelativeLayout 中的尝试:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:animateLayoutChanges="true"
    android:id="@+id/registration_cl">

    <RelativeLayout
        android:id="@+id/signin_social_rl"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toTopOf="@id/signin_btn_back">

        <com.google.android.material.button.MaterialButton
            android:id="@+id/signin_btn_facebook"
            style="@style/Widget.MaterialComponents.Button.UnelevatedButton.Icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginBottom="8dp"
            android:backgroundTint="#525352"
            android:backgroundTintMode="screen"
            android:fontFamily="@font/proximanova"
            android:padding="15dp"
            android:text="Facebook"
            android:textAllCaps="false"
            android:textColor="@color/white_ffffff"
            android:textSize="18sp"
            app:icon="@drawable/ic_facebook_icon_fb_socialshare_"
            app:iconTint="@color/white_ffffff"
            tools:alpha="1" />

        <View
            android:id="@+id/signin_social_div"
            android:layout_width="1dp"
            android:layout_height="50dp"
            android:backgroundTint="@color/white_ffffff"
            android:background="@color/white_ffffff"
            android:layout_toEndOf="@id/signin_btn_facebook"/>

        <com.google.android.material.button.MaterialButton
            android:id="@+id/signin_btn_google"
            style="@style/Widget.MaterialComponents.Button.UnelevatedButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:layout_marginBottom="8dp"
            android:layout_toEndOf="@id/signin_social_div"
            android:backgroundTint="#525352"
            android:backgroundTintMode="screen"
            android:fontFamily="@font/proximanova"
            android:padding="15dp"
            android:text="Google"
            android:textAllCaps="false"
            android:textColor="@color/white_ffffff"
            android:textSize="18sp"
            app:iconSize="8dp"
            tools:alpha="1" />

    </RelativeLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

除了在按钮之间以编程方式调整和定位视图之外,我还希望有一个更简单的解决方案来添加单边边框。

【问题讨论】:

  • 在两个按钮之间添加一个视图,使用layout_height:match_parentlayout_width:1dp0.5dp,只要符合您的要求即可。如果你能分享整个 XML,我可以提供进一步的帮助。
  • @kushpf 谢谢。我更新了代码以包含完整的 xml 树并尝试了您的建议。如果我将layout_height 设置为match_parent,则视图将成为整个屏幕的高度,所以我暂时将其设置为50dp。这就是导致我以编程方式计算高度和位置的解决方案的原因,但这似乎太复杂了,我宁愿改变设计也不愿处理它。
  • @id/signin_btn_back 在哪里定义?在您共享的布局中看不到。由于 signin_btn_back 的约束,relativelayout 的高度可能会受到影响。
  • @kushpf 它是 RelativeLayout 的同级,并且被限制在其父级的底部。所以它最终在屏幕底部,RelativeLayout 就在它的正上方。

标签: java android material-design android-button material-components-android


【解决方案1】:

您的视图不受RelativeLayout 的限制,这就是View 占满高度的原因。相反,使用LinearLayout 来实现结果会容易得多。以下代码应该会有所帮助:

<androidx.constraintlayout.widget.ConstraintLayout ...>

    <LinearLayout
        android:id="@+id/signin_social_rl"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toTopOf="@id/signin_btn_back">

        <com.google.android.material.button.MaterialButton
            android:id="@+id/signin_btn_facebook"
            style="@style/Widget.MaterialComponents.Button.UnelevatedButton.Icon"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            ... />

        <View
            android:id="@+id/signin_social_div"
            android:layout_width="1dp"
            android:layout_height="match_parent"
            ... />

        <com.google.android.material.button.MaterialButton
            android:id="@+id/signin_btn_google"
            style="@style/Widget.MaterialComponents.Button.UnelevatedButton"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            ... />

    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

使用LinearLayout 而不是RelativeLayour 也会使渲染速度更快。希望对您有所帮助!

【讨论】:

  • 感谢您的建议。我的印象是 RelativeLayout 更有效,但我已经对其进行了更多研究,我相信你是正确的。此解决方案完美运行,除了我还需要将 android:insetBottom="0dp"android:insetTop="0dp" 添加到两个 MaterialButtons 以使 LinearLayout 正确适合按钮。
【解决方案2】:

这不是您正在寻找的,但它非常简单。
您可以执行以下操作:

<com.google.android.material.button.MaterialButton
    app:strokeWidth="0dp"
    app:shapeAppearanceOverlay="@style/ShapeAppearanceOverlay.Button.Left"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="BUTTON 1"
    />

<View
    android:layout_width="1dp"
    android:layout_height="wrap_content"
    android:background="@android:color/transparent"/>

<com.google.android.material.button.MaterialButton
    app:strokeWidth="0dp"
    app:shapeAppearanceOverlay="@style/ShapeAppearanceOverlay.Button.Right"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="BUTTON 2"
    />

您可以使用strokeWidth=0dp 移除边框,您可以使用shapeAppearanceOverlay 属性应用不同的圆角半径(它需要1.1.0 版本)。

  <style name="ShapeAppearanceOverlay.Button.Left" parent="">
    <item name="cornerFamily">rounded</item>
    <item name="cornerSizeTopRight">0dp</item>
    <item name="cornerSizeTopLeft">16dp</item>
    <item name="cornerSizeBottomRight">0dp</item>
    <item name="cornerSizeBottomLeft">16dp</item>
  </style>

  <style name="ShapeAppearanceOverlay.Button.Right" parent="">
    <item name="cornerFamily">rounded</item>
    <item name="cornerSizeTopRight">16dp</item>
    <item name="cornerSizeTopLeft">0dp</item>
    <item name="cornerSizeBottomRight">16dp</item>
    <item name="cornerSizeBottomLeft">0dp</item>
  </style>

最终结果:

【讨论】:

  • 谢谢。我已经实施了您的解决方案,但结果没有区别。将分隔线视图的layout_height 设置为match_parent 会导致视图变成显示器的全高,并且按钮被推到屏幕外的某个地方。将高度设置为50dp 给了我this。您可以在示例中发布周围嵌套的 RelativeLayout 和 ConstraintLayout 吗?
  • 在我的示例中,我使用的是透明背景(不是白色)。当然这取决于您的完整布局。如果您不想计算高度,也可以在按钮中使用 android:layout_marginRight="1dp" 之类的内容。
  • 啊,我明白你为什么现在这样做了。很好的解决方案,但这个屏幕上的背景实际上是视频而不是纯色,所以我需要按钮之间的间隙特别是白色。
猜你喜欢
  • 1970-01-01
  • 2023-01-16
  • 1970-01-01
  • 2014-04-10
  • 2021-11-26
  • 2017-07-01
  • 2020-02-20
  • 1970-01-01
  • 2022-08-08
相关资源
最近更新 更多