【问题标题】:Custom background resource not working in android自定义背景资源在android中不起作用
【发布时间】:2020-11-29 03:52:00
【问题描述】:

我知道这是一件小事,但我不知道为什么它不起作用。

当我放置自定义可绘制图像时,它不起作用。我想要一个带角的透明背景按钮(中风)。

下面是图片。

<RelativeLayout 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="@drawable/welcome"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/app_logo"
        android:layout_width="300dp"
        android:layout_height="100dp"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="60dp"
        android:src="@drawable/applogo"/>

    <TextView
        android:id="@+id/app_slogan"
        android:layout_width="220dp"
        android:layout_height="wrap_content"
        android:layout_alignParentEnd="true"
        android:layout_alignParentTop="true"
        android:layout_marginEnd="80dp"
        android:layout_marginTop="140dp"
        android:text="Find, Discover, Choose and buy anything online"
        android:textAlignment="center"
        android:textSize="22sp"
        android:textStyle="bold|italic"
        android:textColor="@color/design_default_color_primary"

        />

        <Button
            android:id="@+id/main_login_btn"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentStart="true"
            android:layout_marginBottom="10dp"
            android:layout_marginStart="15dp"
            android:layout_marginEnd="15dp"
            android:background="@drawable/buttons"
            android:padding="20dp"
            android:textAllCaps="false"
            android:textSize="18sp"
            android:text="Already have a account ? Login"
            android:textColor="@android:color/white"

            />

    <Button
        android:id="@+id/main_join_now_btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dp"
        android:layout_above="@id/main_login_btn"
        android:layout_marginStart="15dp"
        android:layout_marginEnd="15dp"
        android:background="@drawable/input_design"
        android:padding="20dp"
        android:textAllCaps="false"
        android:textSize="18sp"
        android:text="Already have a account ? Login"
        android:textColor="@android:color/white"
        />


</RelativeLayout>

// 可绘制资源文件。

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

    <stroke
        android:color="#fff"
        android:width="3dp"
        />

    <corners
        android:radius="15dp"
        />
    

登录按钮的背景应该是 input_design.xml 但我不知道为什么它不起作用。 帮帮我 提前谢谢你。

【问题讨论】:

  • 尝试在按钮上添加android:backgroundTint="@null"
  • 不,它不起作用@Zain
  • drawable只有一个按钮框架,但在图片中,有一个紫色的背景色;你能告诉我们你实际得到了什么,你期望看到什么?
  • 是的,我知道。我不知道为什么会这样。我只想要一个没有背景颜色的按钮,边框颜色为白色,文本为白色。我所有项目的按钮颜色是我的默认蓝色我不知道为什么。

标签: android xml android-button


【解决方案1】:

要在按钮上实现透明背景,可以使用Widget.MaterialComponents.Button.OutlinedButton

您将需要在 build.gradle 中使用这一行(如果还没有的话):

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

您的按钮将如下所示:

<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/teal_700"
    tools:context=".FirstFragment">

    <Button
        android:id="@+id/button"
        style="@style/Widget.MaterialComponents.Button.OutlinedButton"
        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:cornerRadius="15dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:rippleColor="@color/white"
        app:strokeColor="@color/white"
        app:strokeWidth="3dp" />

</androidx.constraintlayout.widget.ConstraintLayout>

注意:

  • style="@style/Widget.MaterialComponents.Button.OutlinedButton"
  • android:textColor="@color/white"
  • app:cornerRadius="15dp"
  • app:rippleColor="@color/white"
  • app:strokeColor="@color/white"
  • app:strokeWidth="3dp"

这将产生:

【讨论】:

  • 请使用材料概述的按钮组件查看我的更新答案。这将是在不使用任何额外的 xml 形状等的情况下实现您正在寻找的首选解决方案。
  • Thanx,这对我有用.. 但是在 android studio 精致暗模式(主题)和亮模式主题的最新更新可用后告诉我一个,实现但为什么不绘制资源文件,当我创建它并用作背景为什么它不工作..帮助我
  • 您是在问为什么我刚刚回答的问题没有按您希望的方式工作吗?如果是这样,我不确定,但似乎 xml 没有被正确应用,并且您的主题正在接管,无论哪种方式,这都是新的方式,可能是更好的方式,在跟上我的旧东西时没有用观点。如果您想全面执行此操作,您可以在主题中创建一个样式并将其应用于该样式的所有按钮。
  • 它对我有用...早些时候我使用创建可绘制资源并将其作为背景但现在在 AS 的新更新后它不起作用...但是你的方法对我有用 谢谢你那
  • 是的,我不知道为什么它停止工作可能与主题和新组件有关,但通常将旧的做事方式与 Android 材料等较新的东西混合会导致事情无法正常工作预期的。在我看来,旧的方式也很糟糕。您可以更快地完成您想做的事情,而无需任何额外的 xml 文件,这些 xml 文件最终会使项目变得臃肿并到处乱窜。仅供参考,我尝试了你的旧方法,但无法让它发挥作用,因此我采用了我在一个项目中使用的另一种方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-17
  • 1970-01-01
  • 2021-09-08
  • 2023-04-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多