Source Address: Cleverly use drawable to achieve the effect of Android UI element spacing
Most of the mobile UI or Web UI is designed based on the grid concept. These grids are usually made up of blocks, and then they are grouped together into a block. The design principle of using grids can help align UI elements and improve UI consistency. At the same time, it also makes it easier for users to get the content that is included in the UI. Nutshell. The grid is a fairly powerful design tool.
Developers need to add some extra space between UI elements when using grid design principles. For example, padding, margin, or spacing (choose which spacing to use depending on your design). These spacing facilitates the readability of the overall UI without having a clear separation band between different blocks at the same time. These pitches are not unfamiliar to our Android developers, and we use the padding and margin of the view to achieve similar results when designing the Android interface. In Android development. To separate the UI from the business logic, we use XML to define the UI.
This is very effective for a more fixed UI. This is a bit difficult, however, when these UI elements need to be based on business logic to determine the hidden or displayed state. This article presents some Android development tips for dealing with dynamic grid UI based on this scenario.
UI with no spacing
First, let's look at a simple example. We create a simple one LinearLayout .
We then TextView built one underneath (showing "application logo") LinearLayout , where we placed 3 levels in turn Button . The last obtained for example with what is seen:
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "Match_parent" android:layout_height= "wrap_content" android:orientation= "vertical" android:padding= "@dimen/spacing_medium" > & Lt TextView android:layout_width= "match_parent" android:layout_height= "128DP" android:background= "@color /light_gray "android:gravity=" center "android:text=" @string/application_logo "android:textappearance= "@android: Style/textappearance.material.display1"/> <linearlayout android:id= "@+id/buttons_container" Android:layout_width= "Match_parent" android:layout_height= "wrap_content" android:orientation= "Horizontal "> <button android:id=" @+id/btn_first "android:layout_width=" 0DP "Android:la yout_height= "Wrap_content" android:layout_weight= "1" android:background= "@drawable/purple" Android:text= "@string/bUtton_1 "/> <button android:id=" @+id/btn_second "android:layout_width=" 0DP " android:layout_height= "Wrap_content" android:layout_weight= "1" android:background= "@drawable/indigo" android:text= "@string/button_2"/> <button android:id= "@+id/btn_third" Android Oid:layout_width= "0DP" android:layout_height= "Wrap_content" android:layout_weight= "1" and roid:background= "@drawable/teal" android:text= "@string/button_3"/> </linearlayout></linearlayou T>
UI after joining the gap
The UI shown is a grid-based design. There was no space between the elements in the UI. In order for users to better differentiate these UI elements. We give the ID @id/buttons_container a LinearLayout join attribute android:layout_marginTop="@dimen/spacing_medium" , @id/btn_first and @id/btn_second Add the attribute to each of the two IDs Button android:layout_marginRight="@dimen/spacing_medium" . The UI effect at this point is, for example, what you see:
The overall UI effect is much better and more readable after the pitch is added. But there are some problems when we hide something dynamically View . If we have a third, we Button will decide whether or not Google Play Services is installed on the user's device.
If this device does not have Google Play Services, then we will Button set the visibility property of this to View.GONE the View.GONE。 resulting effect such as:
The effect came out as we expected, and the third one Button did not show again. But the second Button right side is not aligned with the right side of the top TextView . The solution to this problem is that the view with the margin attribute will feel that there is an adjacency view in the corresponding direction of the margin. For example, each owning right/top margin view will feel that its right/top direction has an adjoining view, so the corresponding margin will take effect, even if the adjacency view is hidden.
The tradeoff between setting the spacing--java and GridLayout
A straightforward solution is to manually change the corresponding margin value in the Java code, but honestly this is not a good solution. Another option is to use a layout that can proactively handle the spacing between elements. GridLayout meets this requirement. But this layout makes the egg ache is that the spacing between elements cannot be defined by themselves. Only the default spacing can be used.
Best practices for setting spacing--linearlayout divider
There is actually LinearLayout a property that handles the spacing between such elements.
This attribute has not been found, has been very low-key, but its effect is quite wonderful. So the third scenario we're talking about is using a fixed, high-width Drawable LinearLayout element Divider (divider):
<?xml version= "1.0" encoding= "Utf-8"?><shape xmlns:android= "http://schemas.android.com/apk/res/android" android:shape= "Rectangle" > <size android:width= "@dimen/spacing_medium" android:height= "@dimen/spacing_medium"/> < Solid android:color= "@android: Color/transparent"/></shape>
Now you can set this newly created drawable as the divider of linearlayout so that this drawable can create a gap between the elements:
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "Match_ Parent " android:layout_height=" wrap_content " android:divider=" @drawable/spacer_medium " android:o rientation= "vertical" android:padding= "@dimen/spacing_medium" android:showdividers= "Middle" > <!--TextView-- <linearlayout android:id= "@+id/buttons_container" android:layout_width= " Match_parent " android:layout_height=" wrap_content " android:divider=" @drawable/spacer_medium " android:orientation= "Horizontal" android:showdividers= "Middle" > <!--Buttons- </ Linearlayout></linearlayout>
Summarize
There are many features within the Android framework that can be used to implement some of the less common scenarios. And the final effect is unexpected.
Definition Drawable is one of the ways.
Let's say you can thoroughly understand Android inside Drawable . Your code may also be much leaner.
Note: The Divider property setting in article LinearLayout is added after Android API 11, which means that the Android API Before 11 equipment to use this divider need linearlayoutcompat.
This article is translated from: Grid Spacing on Android original Cyril Mottier
Skillfully using drawable to achieve the effect of the Android UI element spacing