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?
Explanation of the above code
-
Here we pass in a function type named block
It is actually a type of Kotlin variable (other languages might have it too, but Java does not support it yet)
(For an explanation of function types, refer to: click here)
Using function types allows you to operate with lambda (Java has lambda, but currently does not have function types)
```markdown
-
Here we use generics
and use type conversion to convert generics
It may cause errors because it is considered implicit type casting during compilation
Therefore, if there is no explicit class
Direct casting may encounter
`xxxxClass cannot be cast to zzzzClass` this kind of error
Or some compilers will directly prompt you with a `unchecked cast` warning
Of course, you can also pass in clazz: Class or similar to determine what the actual class is
But this will make the code more verbose
When there are many classes, you will write more code and keep doing repetitive things... etc.
So here we use `reified` as a solution
It is a usage provided by Kotlin to solve such problems
-> Using reified requires `inline`
Explanation of the above code
This is actually to accommodate different ViewGroup.LayoutParamsSo 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 setPreviously 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.