Anko如何改变按钮颜色?
Anko how to change button color?
我正在尝试更改 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)}
...
结果是这样的:
为什么它会超出按钮形状的边界?
我怎样才能改变按钮颜色?
您可以像这样使用 "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>
我正在尝试更改 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)}
...
结果是这样的:
为什么它会超出按钮形状的边界? 我怎样才能改变按钮颜色?
您可以像这样使用 "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>