【问题标题】:Anko how to change button color?Anko如何改变按钮颜色?
【发布时间】:2018-10-21 19:04:02
【问题描述】:

我正在尝试更改 LinearLayout 中按钮的颜色:

class MoodActivityUI : AnkoComponent<MoodActivity> {
override fun createView(ui: AnkoContext<MoodActivity>) = with(ui) {
    frameLayout {
        verticalLayout {

            linearLayout {
                button("3"){
                    setBackgroundColor(Color.parseColor("red"))
                }.lparams{
                    width = dip(53)
                    height = dip(53)
                    leftMargin=dip(6)
                }

                button("2"){
                }.lparams{
                    width = dip(53)
                    height = dip(53)}

                button("1"){
                }.lparams{
                    width = dip(53)
                    height = dip(53)}
                ...

原来是这样的:

为什么它超出了按钮形状的边界? 如何更改按钮颜色?

【问题讨论】:

    标签: android-layout button android-button anko


    【解决方案1】:

    您可以像这样使用“themedButton”而不是“button”:

    themedButton("3", theme = R.style.MyButtonStyle) {
        }.lparams {
            width = dip(53)
            height = dip(53)
            leftMargin = dip(6)
        }
    

    styles.xml 中的样式

    <style name="MyButtonStyle" parent="Widget.AppCompat.Button">
        <item name="android:colorButtonNormal">#f00</item>
    </style>
    

    【讨论】:

    • 你的解决方案和这个有什么区别:button("- 3"){ background = ResourcesCompat.getDrawable(resources, R.drawable.minus_3_button, null)... 是其中之一更好那么另一个呢?
    • background = blah-blahh 完全取代了按钮的背景,不再有带圆角的灰色矩形(如上图所示)。但我的建议只是为现有按钮的背景着色。
    猜你喜欢
    • 1970-01-01
    • 2015-11-23
    • 2021-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-28
    • 2013-09-24
    • 1970-01-01
    相关资源
    最近更新 更多