着色自定义xml按钮有问题 [英] Having an issue coloring custome xml button

查看:80
本文介绍了着色自定义xml按钮有问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在给自定义按钮着色时遇到问题.出于某种原因,无论我要应用什么颜色更改(文本或背景),该按钮似乎都保持不变.

我注意到 button.xml 具有所需的颜色和正确的形状,尽管它没有在活动中显示按钮 background-color 属性

活动中的按钮

 <按钮android:id ="@@ id/button2"android:layout_width =" 162dp"android:layout_height =" 53dp"android:background =" @ drawable/button"android:text =" @ string/button"android:textAllCaps =" false"app:layout_constraintBottom_toBottomOf =" parent"app:layout_constraintEnd_toEndOf ="parent"app:layout_constraintHorizo​​ntal_bias ="0.132"app:layout_constraintStart_toStartOf =" parent"app:layout_constraintTop_toTopOf =" @ + id/messagTextView"app:layout_constraintVertical_bias ="0.804";/> 

自定义按钮形状

 <?xml version ="1.0"encoding ="utf-8"?< shape android:shape ="矩形";xmlns:android ="http://schemas.android.com/apk/res/android">< corners android:radius ="50sp"/>< solid android:color ="@@ color/redAdobeXD"/></形状> 

解决方案

如果您使用的是

需要注意的是:

  • style =""@ style/Widget.MaterialComponents.Button""
  • android:textColor =" @ color/white"
  • app:backgroundTint =#FF0000" (在您的情况下为 @ color/redAdobeXD )
  • app:cornerRadius ="50dp"

不需要额外的xml并将其设置在按钮等上(仅在更多自定义情况下).

您可以在 ContainedButton 上阅读更多有关可用选项的信息. 解决方案

If you are using material components (which is probably the case if this is a relatively new project) then you style the Button in another way:

<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:background="@color/white"
    tools:context=".FirstFragment">

    <Button
        android:id="@+id/button"
        style="@style/Widget.MaterialComponents.Button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="32dp"
        android:text="I am a Button!"
        android:textColor="@color/white"
        app:backgroundTint="#FF0000"
        app:cornerRadius="50dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Things to notice are:

  • style="@style/Widget.MaterialComponents.Button"
  • android:textColor="@color/white"
  • app:backgroundTint="#FF0000" (would be @color/redAdobeXD in your case)
  • app:cornerRadius="50dp"

There is no need for an extra xml and setting that on the button etc. (only in more custom cases).

You can read more about available options on the ContainedButton here

这篇关于着色自定义xml按钮有问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆