Warm tip: This article is reproduced from serverfault.com, please click

xml-自定义背景资源在android中不起作用

(xml - Custom background resource not working in 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的背景,但是我不知道为什么它不起作用。帮帮我,谢谢你。

Questioner
Dhananjay pathak
Viewed
11
David Kroukamp 2020-11-29 16:03:02

要获得按钮上的透明背景,请使用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"

这将产生:

在此处输入图片说明