Skillfully using drawable to achieve the effect of the Android UI element spacing

Source: Internet
Author: User

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 use of design principles such as grids can help align UI elements, improve UI consistency, and make it easier for users to get the content that is contained in the UI. In short, the grid is a fairly powerful design tool.

Developers need to add some extra space between UI elements when using grid design principles, such as padding, margin, or spacing (choose which spacing to use based on your design options). These spacing facilitates the readability of the overall UI while setting a clear separation band between different blocks. 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, in order to separate the UI from the business logic, we use XML to define the UI. This is useful for comparing fixed uis, but it is a bit difficult when these UI elements need to determine the hidden or displayed state based on business logic. This article presents some Android development techniques to deal with Dynamic grid UI based on this situation.

UI with no spacing

First, let's look at a simple example. We create a simple one LinearLayout . We then TextView built one under (show "Application Logo"), where LinearLayout we placed 3 levels in turn Button . Finally get as shown in:

<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>

Add a space after the UI

The UI shown is a grid-based design. There was no space between the elements in the UI. To give the user a better distinction between these UI elements, we give the ID a property to add, @id/buttons_container LinearLayout android:layout_marginTop="@dimen/spacing_medium" and add the attributes to the two IDs, @id/btn_first and @id/btn_second Button android:layout_marginRight="@dimen/spacing_medium" the UI effect is as follows:

After adding the spacing, the overall UI works much better and more readable. But there are some problems when we hide something dynamically View  . We assume that the third one Button will determine its presentation based on whether Google Play Services is installed on the user's device. If this device does not have Google Play Services, then we will Button View.GONE  set the visibility property of this to the View.GONE, resulting effect such as:

The result is the same as we expected, the third one Button is no longer displayed, but the second Button right side does not align with the right side of the top TextView . This problem occurs because the view that owns the margin attribute considers the margin to have an adjoining view in the corresponding direction. For example, each owning right/top margin view will assume 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 more straightforward solution is to manually change the margin value in the Java code, but honestly this is not a good solution. Another scenario is to use a layout that automatically handles the spacing between elements. GridLayout to meet this requirement. But this layout makes the egg ache is that the spacing between elements cannot be customized, 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 the elements. This attribute has not been found, has been very low-key, but its effect is quite magical. 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 the newly created drawable to LinearLayout divider so that the 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 in 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. If you can thoroughly understand Android Drawable , your code may be much leaner.

Note: The Divider property setting in article LinearLayout is added after Android API 11, which means that the Android API 11 before the device 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.