简体   繁体   English

轮廓的 MaterialButton 不显示任何边框

[英]Outlined MaterialButton doesn't show any border

I'm trying to build a layout with a bordered button as shown below (Expected behavior).我正在尝试构建一个带有边框按钮的布局,如下所示(预期行为)。 From Material design documentation I read about Outlined Material Button that seemed to be perfect for my purpose.从 Material design 文档中,我读到了 Outlined Material Button,它似乎非常适合我的目的。 I defined the button in my layout, providing stroke width and stroke color but it isn't showing any border, what am I doing wrong?我在布局中定义了按钮,提供了笔触宽度和笔触颜色,但没有显示任何边框,我做错了什么?

<com.google.android.material.button.MaterialButton
            android:id="@+id/material_text_button"
            style="@style/Widget.MaterialComponents.Button.OutlinedButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginEnd="8dp"
            android:text="Change name"
            android:background="@android:color/transparent"
            android:textSize="12sp"
            app:layout_constraintBottom_toBottomOf="@+id/listTextView2"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@+id/listTextView2"
            app:layout_constraintTop_toTopOf="@+id/listTextView2"
            app:strokeColor="@color/green"
            app:strokeWidth="10dp" />

Expected behavior:预期行为:

在此处输入图片说明

Current behavior:当前行为:

在此处输入图片说明

Just add this attribute to your MaterialButton只需将此属性添加到您的MaterialButton

android:theme="@style/Theme.MaterialComponents"

avoid to add it to your main theme.避免将其添加到您的主题中。

example:例子:

<com.google.android.material.button.MaterialButton
            android:id="@+id/material_text_button"
            style="@style/Widget.MaterialComponents.Button.OutlinedButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:theme="@style/Theme.MaterialComponents"
            android:layout_marginStart="8dp"
            android:layout_marginEnd="8dp"
            android:text="Change name"
            android:background="@android:color/transparent"
            android:textSize="12sp"
            app:layout_constraintBottom_toBottomOf="@+id/listTextView2"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@+id/listTextView2"
            app:layout_constraintTop_toTopOf="@+id/listTextView2"
            app:strokeColor="@color/green"
            app:strokeWidth="10dp" />

Use this in your build.gradle app在您的 build.gradle 应用程序中使用它

    implementation 'com.google.android.material:material:1.2.0-alpha02'

I think it is a bug in google material.我认为这是谷歌材料中的一个错误。

Update: I Tested just right now.更新:我刚刚测试过。 Use dependency in Gradle app below在下面的 Gradle 应用程序中使用依赖项

implementation 'com.google.android.material:material:1.2.0-alpha05

and in your material button XML, add style并在您的材料按钮 XML 中,添加样式

style="@style/Widget.MaterialComponents.Button.OutlinedButton"

it might not show in your xml preview but it will show when you run the app它可能不会显示在您的 xml 预览中,但会在您运行应用程序时显示

try removing transparent background and style from your xml.尝试从您的 xml 中删除透明背景和样式。 and also make sure to change the style to MaterialComponents.并确保将样式更改为 MaterialComponents。

  <style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar"> 

 <com.google.android.material.button.MaterialButton
        android:id="@+id/btn_login"
        android:layout_width="match_parent"
        android:layout_height="55dp"
        android:layout_marginStart="20dp"
        android:layout_marginTop="20dp"
        android:textSize="16sp"
        android:layout_marginEnd="20dp"
        android:text="works"
        android:textAllCaps="false"
        android:textColor="@color/white"
        app:strokeColor="@color/white"
        app:strokeWidth="2dp" />

Dependency依赖

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

Adding android:backgroundTint="@color/colorPrimary"添加 android:backgroundTint="@color/colorPrimary"

<com.google.android.material.button.MaterialButton
     android:id="@+id/material_text_button"
     style="@style/Widget.MaterialComponents.Button.OutlinedButton"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="Pick Date"
     android:background="@drawable/register_edittext_background"
     android:textSize="12sp"

     app:backgroundTint="@color/colorPrimary"
/>

This worked for me这对我有用

 <style name="oval_button_outline" 
     parent="Widget.MaterialComponents.Button.OutlinedButton">
    <item name="cornerFamily">rounded</item>
    <item name="cornerRadius">16dp</item>
    <item name="android:textColor">@color/dark_color</item>
</style>



 <com.google.android.material.button.MaterialButton
        android:id="@+id/start_login"
        style="@style/oval_button_outline"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fontFamily="sans-serif-medium"
        android:textStyle="normal"
        android:textSize="14sp"
        android:letterSpacing="0.09"
        android:lineSpacingExtra="2sp"
        android:gravity="center_horizontal"
        android:text="@string/login"
        android:textColor="@color/dark_color"
        android:theme="@style/Theme.MaterialComponents"
        app:strokeColor="@color/dark_color"
        app:strokeWidth="2dp" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM