【问题标题】:Why is my android button not showing the correct background? [duplicate]为什么我的 android 按钮没有显示正确的背景? [复制]
【发布时间】:2020-09-29 07:11:37
【问题描述】:

我正在尝试为我的 android 应用程序设置一个片段的样式,当我应用 android:background xml 属性时,它被忽略了。这是我得到的与正确样式的编辑文本相反的结果。

背景不正确的按钮:

具有正确背景的EditText:

这是我的按钮的 XML:

<com.google.android.material.button.MaterialButton
            android:id="@+id/register_fragment_submit_button"
            style="@style/Widget.MaterialComponents.Button.UnelevatedButton"
            android:text="@string/register_register_button"
            android:layout_marginTop="@dimen/register_fragment_submit_button_margin_top"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:background="@drawable/register_button_bg"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/register_email_edittext" />

这是我正确样式的 EditText 的代码:

<EditText
            android:id="@+id/register_email_edittext"
            style="@style/register_fragment_edittext"
            android:layout_marginTop="@dimen/register_fragment_email_edittext_margin_top"
            android:autofillHints="@string/register_screen_email_autofill"
            android:hint="@string/register_email_edittext_hint"
            android:inputType="textEmailAddress"
            android:text="@={viewmodel.newUserEmail$app_debug}"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/register_password_edittext" />

这是它的样式:

<style name="register_fragment_edittext">
        <item name="android:paddingLeft">@dimen/register_fragment_edittext_padding_left</item>
        <item name="android:background">@drawable/edittext_background</item>
        <item name="android:layout_width">@dimen/register_fragment_edittext_width</item>
        <item name="android:layout_height">@dimen/edit_text_height</item>
        <item name="android:textColorHint">@color/colorAccent</item>
</style>

如果有人好奇,我会将可绘制的 XML 代码留在底部。如果这很重要,我将其放入我的应用程序级 build.gradle 文件中以进行材料设计: implementation 'com.google.android.material:material:1.2.0' 有没有人有什么建议?谢谢。

EditText 可绘制:

<shape xmlns:android="http://schemas.android.com/apk/res/android">

    <corners android:radius="@dimen/custom_edittext_bg_drawable_corners"/>
    <solid android:color="@color/colorPrimary"/>
    <stroke android:color="@color/colorSecondary" android:width="@dimen/custom_edittext_bg_border_width"/>

</shape>

按钮可绘制:

<shape xmlns:android="http://schemas.android.com/apk/res/android">

    <corners android:radius="@dimen/custom_edittext_bg_drawable_corners"/>
    <solid android:color="@color/colorSecondary"/>
    <stroke android:color="@color/colorAccent" android:width="@dimen/custom_edittext_bg_border_width"/>

</shape>

【问题讨论】:

  • 在您的情况下,您可以只使用 MaterialButton 的标准属性。您不需要自定义背景。

标签: android material-ui android-button material-components-android


【解决方案1】:

正如@Mariusz 回答中所述,不支持 Material Button 的所有属性。

另一方面,使用最新版本的材料设计,您可以为材料按钮设置自定义背景

材料库:

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

你必须设置背景色调属性“null”,把它放在你的样式文件夹中

<style name="my_button" parent="Widget.MaterialComponents.Button">
    <item name="android:background">@drawable/register_button_bg</item>
    <item name="textAllCaps">false</item>
    <item name="backgroundTint">@null</item>
    <item name="android:insetTop">0dp</item>
    <item name="android:insetBottom">0dp</item>
</style>

将您的按钮样式设置为“my_button”

    <com.google.android.material.button.MaterialButton
                android:id="@+id/register_fragment_submit_button"
                style="@style/my_button"
   ....
                 />

【讨论】:

  • 非常感谢!我现在明白了一点。
【解决方案2】:

正如Material Button Documentation中所述

支持 MaterialButton 的所有属性。不要使用 android:background 属性。 MaterialButton 管理自己的背景drawable,设置一个新的背景意味着MaterialButton 不能再保证它引入的新属性会正常工作。如果更改默认背景,MaterialButton 无法保证良好定义的行为。

所以我要做的就是将 MaterialButton 更改为 Button 或 AppCompatButton

编辑

如果你有适当版本的金属组件(1.2.0-alpha06 或更高版本),你可以使用android:background 属性,因为它写在这里:

How to set a gradient background in a Material Button from Material Components?

【讨论】:

  • 感谢您的帮助!
  • 这并不完全正确。现在 MaterialButton 支持 android:background 属性。
  • 所以要么文档不是最新的,要么我错过了一些东西:/我知道有公共方法setBackgroundDrawable,但我认为如果背景可绘制不能从属性工作,那么它仍然不受支持
  • @GabrieleMariotti 我已将链接添加到您的答案中
  • 感谢您的详细回答,我不知道这些变化,这些天很难跟上最新的变化;)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-09
  • 1970-01-01
  • 2016-02-22
  • 2019-08-18
  • 2021-07-19
  • 1970-01-01
相关资源
最近更新 更多