【发布时间】: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_parent和layout_width:1dp或0.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