【问题标题】:Custom Button not completly shown自定义按钮未完全显示
【发布时间】:2020-06-21 13:59:26
【问题描述】:

我尝试将可绘制对象设置为按钮的背景,但现在只有边角是圆角的。

这里是drawable的代码:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <corners android:radius="1000dp"/>

    <solid android:color="@color/light_notlight_themebased_accentColor" />

    <padding
        android:bottom="7dp"
        android:top="7dp"
        android:left="7dp"
        android:right="7dp" />

    <stroke
        android:color="@color/ContrastAndText"
        android:width="10dp" />
</shape>

这里是按钮的代码:

<Button
            android:id="@+id/loginButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="20dp"
            android:background="@drawable/login_loginbutton_default"
            android:text="@string/login_loginButton"
            android:textAllCaps="true"
            android:textColor="@color/NavbarAccent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.498"
            app:layout_constraintStart_toStartOf="parent" />

笔画和颜色不显示(它保持背景色),只有角是圆角...

这是它的样子:

在 AndroidStudio 预览版(Canary 4.1)中:

设备上的最终结果:

我的错是什么?提前致谢!

【问题讨论】:

  • 你在使用 Material Components 库吗?是哪个版本的?
  • 是的,我是。 实现'com.google.android.material:material:1.1.0'

标签: android android-layout android-drawable android-button material-components-android


【解决方案1】:

如果您使用的是 MaterialComponents 主题,&lt;Button /&gt;&lt;com.google.android.material.button.MaterialButton /&gt; 之间没有区别。检查more details的这个答案。

如果您想在Button 中使用android:background 属性,您必须使用1.2.0-alpha06 或更高版本。

<MaterialButton
    app:backgroundTint="@null"
    android:background="@drawable/button_gradient"
    ... />

否则你必须使用AppCompatButton 组件。

检查您的形状,您不需要设置背景。 只需使用

   <com.google.android.material.button.MaterialButton
        app:cornerRadius="@dimen/..."
        app:strokeColor="@color/.."
        app:strokeWidth="@dimen/.."
        ..>
  

【讨论】:

  • 谢谢,我现在刚刚使用了 AppCompat 按钮,效果很好:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-29
  • 1970-01-01
  • 2013-06-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多