自定义AppCompatButton在Android 5(Api 21)上获得错误的颜色

时间:2018-11-12 08:55:52

标签: android xml android-layout kotlin

因此,我已经为我们的应用实现了自定义按钮,该实现在Android 6及更高版本上完美运行,但是在Android 5上遇到了问题,直到我按下按钮一次,背景颜色才会被应用。然后看起来应该是。

实现如下:

class MpButton @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = R.style.ButtonStyle) : AppCompatButton(context, attrs, defStyleAttr) {

private val toScale = 0.9f
private val fromScale = 1f

init {
    var style = 0
    var allCaps = false

    attrs?.let {
        val a = context.obtainStyledAttributes(it, R.styleable.MpButton)
        style = a.getInt(R.styleable.MpButton_MpButtonColor, 0)
        allCaps = a.getBoolean(R.styleable.MpButton_MpButtonAllcaps, false)
        a.recycle()
    }

    when (style) {
        WHITE -> {
            this.setBackgroundResource(R.drawable.mp_button_white)
            this.setTextColor(ContextCompat.getColor(context, R.color.white))
        }
        RED -> {
            this.setBackgroundResource(R.drawable.mp_button_red)
            this.setTextColor(ContextCompat.getColor(context, R.color.white))
        }
        BLACK -> {
            this.setBackgroundResource(R.drawable.mp_button_black)
            this.setTextColor(ContextCompat.getColor(context, R.color.mp_black))
        }
        BLUE -> {
            this.setBackgroundResource(R.drawable.mp_button_blue)
            this.setTextColor(ContextCompat.getColor(context, R.color.white))
        }
        YELLOW -> {
            this.setBackgroundResource(R.drawable.mp_button_yellow)
            this.setTextColor(ContextCompat.getColor(context, R.color.white))
        }
        else -> {
            this.setBackgroundResource(R.drawable.mp_button_green)
            this.setTextColor(ContextCompat.getColor(context, R.color.white))
        }
    }

    this.gravity = Gravity.CENTER
    val padding = Utils.convertDpToPixel(context, 5)
    this.setPadding(padding, padding, padding, padding)
    this.isAllCaps = allCaps
}

override fun dispatchTouchEvent(event: MotionEvent): Boolean {
    when(event.action) {
        MotionEvent.ACTION_DOWN -> {
            this.animate().scaleX(toScale).scaleY(toScale).setDuration(100).start()
        }
        MotionEvent.ACTION_UP -> {
            this.animate().scaleX(fromScale).scaleY(fromScale).setDuration(100).start()
        }
    }
    return super.dispatchTouchEvent(event)
}

companion object {
    const val WHITE = 1
    const val RED = 2
    const val BLACK = 3
    const val BLUE = 4
    const val YELLOW = 5
}
}

有关信息,我还将为其中一种颜色添加XML:

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@color/mp_black">
<item>
    <selector
        android:exitFadeDuration="@android:integer/config_mediumAnimTime"
        android:enterFadeDuration="@android:integer/config_shortAnimTime">
        <item android:state_enabled="true" android:state_pressed="false">
            <shape android:shape="rectangle">
                <corners android:radius="30dp"/>
                <solid android:color="@color/jungle_green" />
            </shape>
        </item>
        <item android:state_enabled="true" android:state_pressed="true">
            <shape android:shape="rectangle">
                <corners android:radius="30dp"/>
                <solid android:color="@color/forest" />
            </shape>
        </item>
        <item android:state_enabled="false">
            <shape android:shape="rectangle">
                <corners android:radius="30dp"/>
                <solid android:color="@color/mp_gray_5" />
            </shape>
        </item>
    </selector>
</item>
</ripple>

作为最后一个错误,请复制视图XML的一部分以提供尽可能多的信息:

<se.motesplatsen.app.ui.controls.MpButton
    android:id="@+id/btnStartLogin"
    android:layout_width="220dp"
    android:layout_height="44dp"
    android:text="@string/DEFAULT_LOGIN"
    android:layout_marginBottom="10dp"
    android:layout_marginTop="20dp"
    app:MpButtonAllcaps="true"
    app:layout_constraintVertical_chainStyle="packed"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@id/btnStartBecomeMember"/>

<se.motesplatsen.app.ui.controls.MpButton
    android:id="@+id/btnStartBecomeMember"
    android:layout_width="220dp"
    android:layout_height="44dp"
    android:layout_marginTop="25dp"
    android:text="@string/BECOME_MEMBER_BUTTON"
    app:MpButtonColor="1"
    app:MpButtonAllcaps="true"
    app:layout_constraintVertical_chainStyle="packed"
    app:layout_constraintTop_toBottomOf="@id/tvStartDesc"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintBottom_toTopOf="@id/btnStartLogin"/>

更新和解决方案!: 终于有了这个工作!解决方法是删除

android:exitFadeDuration="@android:integer/config_mediumAnimTime" android:enterFadeDuration="@android:integer/config_shortAnimTime"

我猜测它会以某种方式干扰某些Android版本上的波纹动画。因此,对于其他遇到我问题的人,请进行检查,以免选择器中没有带有淡入淡出的Ripple togheter:)!

2 个答案:

答案 0 :(得分:0)

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<corners
    android:radius="5dp"
    />
<solid
    android:color="@color/Transparent"
    />

仅将其用作您在文件处于按下状态时使用的背景

答案 1 :(得分:0)

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <shape >
        <solid android:color="@android:color/white"/>
        <size android:height="1dp"/>
    </shape>
</item>
<item  android:bottom="2dip" android:top="2dip" android:left="2dip" android:right="2dip">
    <shape>
        <solid android:color="@android:color/Transparent"/>
    </shape>
</item>

这就像您的正确按钮