在“约束布局”中更改背景颜色时,将删除按钮笔触

时间:2019-04-09 20:49:59

标签: android xml class button kotlin

更改按钮颜色消除了我的笔画。我尝试了很多事情,包括:Change background of button with border,但没有成功。对于什么值,我正在使用约束布局。

我尝试过:

Button colorButton = (Button) findViewById(R.id.colorButton);
GradientDrawable background = (GradientDrawable) colorButton.getBackground();
background.setColor(getResources().getColor(R.color.some_color));

它不起作用,剩下的代码如下:

MainActivity.kt

/* This value is used to change the color of the bottom button to red later */

        val backgroundDrawable = bottomButton.getBackground()

/* Change bottomButton back to mainBtnColor */
                DrawableCompat.setTint(backgroundDrawable, mainBtnColor)

可绘制的XML

<?xml version="1.0" encoding="UTF-8"?>
<shape
        xmlns:android="http://schemas.android.com/apk/res/android">
    <stroke
            android:width="5dp"
            android:color="@color/colorAccent"/>
    <corners
            android:radius="150dp" />

    <padding
            android:left="5dp"
            android:right="5dp"
            android:top="20dp"
            android:bottom="20dp"/>

    <solid android:color="@color/colorPrimary"/>

</shape>

activity_main.xml

    <Button
            android:text="Practice Exams"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:id="@+id/topButton" app:layout_constraintEnd_toEndOf="parent" android:layout_marginEnd="20dp"
            android:layout_marginRight="20dp" app:layout_constraintStart_toStartOf="parent"
            android:layout_marginLeft="20dp" android:layout_marginStart="20dp" android:textSize="30sp"
            android:layout_marginBottom="20dp" app:layout_constraintBottom_toTopOf="@+id/middleButton"
            android:background="@drawable/btn_rounded"/>

我希望背景颜色会发生变化,但笔触仍会保留。实际发生的是将背景色和笔触替换为新的背景色。

0 个答案:

没有答案