Quickly Learn to Adjust Element Spacing in Android Apps - Using Kotlin Extension Code to Set Margins

  Introduction

Today, I want to share a super simple trick with you,
which allows you to adjust the spacing of elements in an Android app using code.
This method is not only convenient but also makes your design more beautiful.
Even if you are a beginner engineer, you can easily master it!
Follow along with me to learn!

  Basic Method

There are various ways to adjust element spacing in Android, and this article will introduce the method of using Kotlin extension code to set margins.
Before that, let’s understand
that setting view margins in XML
is as simple as one line:
android:layout_marginLeft="30dp"

In some cases,
when the client requires you to dynamically set the spacing between Android View elements,
you can usually use the following method:

This method requires instantiating a LayoutParams
and setting the top, bottom, left, and right margins before setting it to your View.
But if you need to use this margin-setting method in multiple places,
it will make the code lengthy and hard to maintain.

To solve this problem,
you can use Kotlin Extension to implement the margin-setting method,
making your code more concise and easier to maintain.

  Using Kotlin Extension to Achieve This

  Step 0. Here is the complete extension function

This code is ready to use.
Copy it into your project,
and call it directly on your view!
vb.btConfirmZero.margin(top = 0F)
vb.btConfirmOne.margin(bottom = 30F, right = 2F)
vb.btConfirmTwo.margin(bottom = 10F, left = 3F)
If you understand this, you can skip directly to step 5.


  Step 1. How to Implement It?

First, create a function as follows




After that, we can call it like this


Explanation of the above code

This is actually to accommodate different ViewGroup.LayoutParams
So that in the future, if there are more instances that inherit ViewGroup.LayoutParams and want to operate
It can be more flexible


  Step2. Write a method to convert dp to px

  • This is very simple Mainly to match the use of pixels when setting margins So a conversion method was written

  Step3. Modify the layout parameters through the extended layoutParams

Explanation of the above code

Here we use the newly written View.layoutParams to operate the view we want to set

Previously in Java, setting parameters always required inputting all four parameters (left, top, right, bottom) at once
Therefore, here we use
left: Float? = null, top: Float? = null, right: Float? = null, bottom: Float? = null
to default all four margin positions to null
Then use Kotlin's null safety feature to check, such as
left?.run { leftMargin = convertDpToPixel(this) }
Only set the margin value if there is a value
We don't have to worry about null exceptions and can flexibly input only the positions we want to change


  Step5. Finally, use it easily

```

It looks like you haven’t pasted the Markdown content yet. Please provide the text you want translated, and I’ll get started on the translation for you.

You might also enjoy