Android custom control backgrounds and their drawable for flattening

Source: Internet
Author: User

Flat?

It is said that flattening is the evil wind blowing from iOS and windowsphone, but it is undeniable that flattening is the most comfortable and natural expression I have ever seen. from a development standpoint, a flat design can liberate us from many screen adaptations and size adjustments (albeit just a little), paying more attention to functionality, while at the level of use, as long as literacy is not particularly low (no malice), the hint of the intended materialization is not so obvious. , of course, this is not to say that the proposed materialization is not good, in short: compared to other manifestations, flat bunker.

Let's do a flattening. It says that flat controls are actually very easy to develop. Here, let's do the following (the red part is the part of this article, and the blue one you can practice by yourself):
All the elements are very simple,It is essentially a view with a black border with no fill background (Button (1 in figure), ViewGroup (Figure 2)). define a flattened background for view (1 on the way)From the above analysis, we only need to define a flat background for the components, and in the layout as far as possible to achieve the simplicity of the flat effect. Here is a rough 0.5dp, filled with a white background:Bordered_black_blue_bg.xml
<?xml version= "1.0" encoding= "Utf-8"? ><shape xmlns:android= "Http://schemas.android.com/apk/res/android" >    <corners android:radius= "5DP"/>    <solid android:color= "@android: Color/white"/>    < Stroke        android:width= "0.5DP"        android:color= "@android: Color/black"/></shape>

If you have a friend who is not familiar with the use of shape in Android, you can refer to the followingThis classmate's summaryor refer to the official API documentation.

Our design also includes: When the user taps the button, the background color changes to blue. So, the above drawable resource is just the default state of the display, below we create a flat button pressed when the effect:

Bordered_black_blue_bg_pressed.xml

<?xml version= "1.0" encoding= "Utf-8"? ><shape xmlns:android= "Http://schemas.android.com/apk/res/android" >    <corners android:radius= "5DP"/>        <!--custom light blue--    <solid android:color= "@color/blue "/>    <stroke        android:width=" 0.5DP "        android:color=" @android: Color/black "/></shape>

Next we create a Selector object for the button based on the two drawable files above.
<?xml version= "1.0" encoding= "Utf-8"? ><selector xmlns:android= "http://schemas.android.com/apk/res/ Android ">    <item android:drawable=" @drawable/bordered_black_blue_bg_pressed "android:state_pressed=" True "/>    <item android:drawable=" @drawable/bordered_black_blue_bg "/></selector>
Define a flat background for ViewGroup

Observation 2 shows that the background of the viewgroup is consistent with the background of the view in the normal state. So the content of their drawable files is consistent. Bordered_black_bg.xml
<?xml version= "1.0" encoding= "Utf-8"? ><shape xmlns:android= "Http://schemas.android.com/apk/res/android" >    <corners android:radius= "5DP"/>    <solid android:color= "@android: Color/white"/>    < Stroke        android:width= "0.5DP"        android:color= "@android: Color/black"/></shape>

2The layout file for those small entries:
View_info_item.xml
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Match_parent "android:layout_height=" wrap_content "android:background=" @drawable/b ORDERED_BLACK_BG "android:gravity=" center_vertical "android:orientation=" horizontal "android:padding=" @dimen/defa        Ult_padding "> <textview android:layout_width=" wrap_content "android:layout_height=" Wrap_content " Android:ellipsize= "marquee" android:marqueerepeatlimit= "Marquee_forever" android:maxwidth= "@dimen/info _item_max_text_width "android:text=" Chinese address "/> <textview android:layout_width=" Wrap_conten T "android:layout_height=" wrap_content "android:layout_marginleft=" @dimen/margin_large "android:text= "N34, N34, N34, No. n34, \n34 Road, No. n34, Chaoyang District, Beijing \ n"
flat processing of the ListViewAccording to the above introduction, we can already make a very "band sense" of the flat button (or TextView, etc.), in order to make the graph2These small entries can be scrolled and easy to manage, we put it in the ListView, the following is the layout file: Fragment_department_detail.xml
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Android:orien tation= "vertical" android:padding= "@dimen/default_padding" ><!--This is my custom view, which corresponds to the blue part of it and can be used as your practice job--<o        Rg.xiaom.butler.view.TopNav android:layout_width= "match_parent" android:layout_height= "Wrap_content" android:layout_margintop= "@dimen/margin_large"/> <textview android:id= "@+id/textview1" Android:lay         Out_width= "Wrap_content" android:layout_height= "wrap_content" android:layout_margintop= "@dimen/margin_large" android:text= "Baby Home care Center" android:textsize= "@dimen/text_size_larger" android:textstyle= "b Old "/> <textview android:id=" @+id/textview2 "android:layout_width=" Wrap_content "Android:la yout_height= "Wrap_content" android:text= "Baby Home Early Education Center "android:textsize=" @dimen/text_size_large "/> <!--This button uses the flattened background defined previously @drawable/selector/selector_b UTTON_NORMAL_BG--<button android:id= "@+id/btn" android:layout_width= "Match_parent" Androi d:layout_height= "Wrap_content" android:layout_margintop= "@dimen/margin_large" android:background= "@drawable/ SELECTOR_BUTTON_NORMAL_BG "android:text=" Show Taxi Card "android:textcolor=" @android: Color/black "and    Roid:textsize= "@dimen/text_size_large" android:textstyle= "bold"/><!--note divider and dividerheight These two properties-- <listview android:id= "@+id/list" android:layout_width= "match_parent" android:layout_height= "Wrap_ Content "android:layout_weight=" 1 "android:background=" @null "android:divider=" @null "Android: dividerheight= "@dimen/list_dirver_height" android:paddingbottom= "@dimen/list_view_padding_v" android:padding top= "@dimen/list_view_padDing_v "> </ListView></LinearLayout> 

We know that the ListView is ugly by default, and there is no space between the item, simply can't bear to look directly at ~~~~~ in order to make the ListView can also catch the flattening of this spring breeze, we need to configure it.
    • Android:divider= "@null", the interval for configuring item within the ListView is @null, i.e.-no interval.
    • android:dividerheight= "@dimen/list_dirver_height", configure the height of the split to "distance" between the--item. In the XML above, the interval is 5DP
Populate the ListView with custom data, not here, inherit a baseadapter. Start the activity and don't say it,here is the source code of this blog post. Any download that requires points is a play (da) stream (sha) Mob (bi).

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.