Anko is equivalent to attr style in xml

I need to create a borderless button. The easiest way in xml is to install Widget.AppCompat.Button.Borderless . I try to do this using Anko

 button(theme = R.style.Widget_AppCompat_Button_Borderless_Colored, text = "Send") { horizontalGravity = Gravity.END lparams(wrapContent, wrapContent) } 

But there is no effect. What am I doing wrong?

+5
source share
2 answers

Try using a third constructor that takes an attr resource:

 addView(Button(activity, null, R.attr.borderlessButtonStyle)) 

Alternatively, you can declare it as a component of DSL:

 fun ViewManager.borderlessButton(textRes: Int = 0) = borderlessButton(textRes) { } fun ViewManager.borderlessButton(textRes: Int = 0, init: Button.()->Unit) = ankoView({ Button(it, null, R.attr.borderlessButtonStyle) }, 0) { if (textRes != 0) setText(textRes) init() } 

Then your site might look like this:

 borderlessButton(android.R.string.ok) 

You can look at the Anko horizontalProgressBar and HORIZONTAL_PROGRESS_BAR_FACRTORY methods, which are declared in the same way.

0
source

The order is incorrect. Try the following:

 button("Your text", android.support.design.R.style.Base_Widget_AppCompat_Button_Borderless_Colored) { //your params } 

This works for me.

-1
source

Source: https://habr.com/ru/post/1263267/


All Articles