【问题标题】:custom button on android studio 4.2android studio 4.2上的自定义按钮
【发布时间】:2020-11-04 10:32:02
【问题描述】:

我是 android studio 的新手并尝试自定义我的按钮,我在 drawable 文件夹中创建了一个新的 xml 文件,但 solidpadding 属性不起作用。 ..

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

    <solid android:color="#69EA64"/>
    <size android:height="100dp"
        android:width="150dp"/>
    <corners android:radius="50dp"/>
    <padding android:bottom="60dp"/>
    <stroke android:color="#FF0"
        android:width="5dp"
        android:dashGap="3dp"
        android:dashWidth="15dp"/>
    <gradient android:angle="45"
        android:startColor="#FF00"
        android:centerColor="#49A530"
        android:endColor="#FF0"/>
</shape>

这是我的 XML 代码:

<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button"
        android:background="@drawable/custom_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="100dp"
        android:text="Button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

请帮忙...

【问题讨论】:

  • 请同时发布您的 XML 代码。

标签: android xml button


【解决方案1】:

要使 solid 正常工作,请删除 gradient

要使padding 改用inset。要使用的整个代码:

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

    <shape
        android:shape="rectangle">

        <solid android:color="#69EA64" />
        <size
            android:width="150dp"
            android:height="100dp" />
        <corners android:radius="50dp" />
        <stroke
            android:width="5dp"
            android:color="#FF0"
            android:dashWidth="15dp"
            android:dashGap="3dp" />
    </shape>

</inset>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-21
    • 1970-01-01
    • 2015-01-08
    • 1970-01-01
    • 1970-01-01
    • 2016-08-08
    • 2014-01-27
    • 1970-01-01
    相关资源
    最近更新 更多