Set runtime margin to any view using Kotlin

2019-06-21 20:04发布

问题:

I am a beginner in Kotlin .I am not too much familier with this language. I am making one example and playing with code. I Just want to set runtime margin to any view. I also trying to google it but not getting any proper solution for this task.

Requirement

Set runtime margin to any View.

Description

I have taking one xml file which is contain on Button and I want to set runtime margin to this button.

Code

I also try below thing but it's not work.

class MainActivity : AppCompatActivity() {


//private lateinit var btnClickMe: Button
//var btnClickMe=Button();

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        //For setting runtime text to any view.
        btnClickMe.text = "Chirag"

        //For getting runtime text to any view
        var str: String = btnClickMe.text as String;

        //For setting runtimer drawable
       btnClickMe.background=ContextCompat.getDrawable(this,R.drawable.abc_ab_share_pack_mtrl_alpha)//this.getDrawable(R.drawable.abc_ab_share_pack_mtrl_alpha)


        /*
        //For Setting Runtime Margine to any view.
        var param:GridLayout.LayoutParams
        param.setMargins(10,10,10,10);

        btnClickMe.left=10;
        btnClickMe.right=10;
        btnClickMe.top=10;
        btnClickMe.bottom=10;
        */

        // Set OnClick Listener.
        btnClickMe.setOnClickListener {
            Toast.makeText(this,str,5000).show();
        }

    }

}

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
tools:context="chirag.iblazing.com.stackoverflowapp.MainActivity"
android:layout_height="match_parent">

<Button
    android:id="@+id/btnClickMe"
    android:text="Click Me"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

</LinearLayout>

How can I proceed?

回答1:

You need to get the layoutParams object from button and cast it to LinearLayout.LayoutParams as btnClickMe's parent is LinearLayout and set margins to whatever you want.

Check following code:

val param = btnClickMe.layoutParams as LinearLayout.LayoutParams
param.setMargins(10,10,10,10)
btnClickMe.layoutParams = param // Tested!! - You need this line for the params to be applied.

Hope it helps.



回答2:

This is how I would like to do in Kotlin -

fun View.margin(left: Float? = null, top: Float? = null, right: Float? = null, bottom: Float? = null) {
    layoutParams<ViewGroup.MarginLayoutParams> {
        left?.run { leftMargin = dpToPx(this) }
        top?.run { topMargin = dpToPx(this) }
        right?.run { rightMargin = dpToPx(this) }
        bottom?.run { bottomMargin = dpToPx(this) }
    }
}

inline fun <reified T : ViewGroup.LayoutParams> View.layoutParams(block: T.() -> Unit) {
    if (layoutParams is T) block(layoutParams as T)
}

fun View.dpToPx(dp: Float): Int = context.dpToPx(dp)
fun Context.dpToPx(dp: Float): Int = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, resources.displayMetrics).toInt()

now we just have to call this on a view like

textView.margin(left = 16F)