Android Program Development Fragment implementation of the bottom navigation bar instance Code _android

Source: Internet
Author: User

The popular application of the navigation is generally divided into two kinds, one is the bottom navigation, one is the side bar.

Description

Ide:as,android Studio;
Simulator: genymotion;

The effect of the implementation, see the figure below.

Concrete implementation

In order to understand this implementation process, we post the code more than one write, so that more convenient to understand [the final release of the complete code implementation]. Look at the interface of the map to do more rough, but the skeleton of the implementation process has, want to more perfect design, then self-improvement bar ^0^.

Layout

By looking at the above effect chart, you find that any one of the options pages is composed of three parts:

Top of the Actionbar after removing the title bar;

A fragmentlayout is used in the middle to put the corresponding fragment;

At the bottom a large linearlayout is placed with four styles (Imagview + TextView) of the small item.

(1) Complete and specific code, see: Show_main_lay.xml, through the comments can be seen in the layout of the three parts of the composition.

<?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=" match_parent "android:background=" @color Coloraccent "android:orientation=" vertical "> <!--1. Top title bar--> <include android:id= "@+id/show_main_title" layout= "@layout/title_layout"/> <!--2. Storage of four fragment--> <framelayout android:id= "@+id/content" android:layout_width= "Match_parent" Android:layout_ height= "0DP" android:layout_weight= "1" android:background= "@color/whitesmoke" ></FrameLayout> <!--3. The bottom four Options menu--> <linearlayout android:layout_width= "match_parent" android:layout_height= "60DP" Android: background= "#FFFFFF" > <!--four parts are the same: ImageView + textview--> <relativelayout android:id= "@+id/first_layout
"Android:layout_width=" 0DP "android:layout_height=" Match_parent "android:layout_weight=" 1 "> <LinearLayout Android:layout_width= "Match_parent" android:layout_height="Wrap_content" android:layout_centervertical= "true" android:orientation= "vertical" > <imageview android:id= "@ +id/first_image "android:layout_width=" wrap_content "android:layout_height=" Wrap_content "android:layout_gravity=" "Center_horizontal" android:src= "@android:d rawable/ic_menu_compass"/> <textview android:id= "@+id/first_text "Android:layout_width=" wrap_content "android:layout_height=" wrap_content "android:layout_gravity=" Center_ Horizontal "android:layout_margintop=" 5DP "android:text=" message "android:textcolor=" #7597B3 "/> </linearlayout > </RelativeLayout> <relativelayout android:id= "@+id/second_layout" android:layout_width= "0DP" Android:
layout_height= "Match_parent" android:layout_weight= "1" > <linearlayout android:layout_width= "match_parent" android:layout_height= "Wrap_content" android:layout_centervertical= "true" android:orientation= "vertical" > < ImageView android:id= "@+id/second_image" android:layout_width= "Wrap_content" WRap_content "android:layout_gravity=" Center_horizontal "android:src=" @android:d Rawable/ic_menu_agenda "/> <
TextView android:id= "@+id/second_text" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:layout_gravity= "Center_horizontal" android:layout_margintop= "5DP" android:text= "Found" android:textColor= "#
7597b3 "/> </LinearLayout> </RelativeLayout> <relativelayout android:id=" @+id/third_layout "
Android:layout_width= "0DP" android:layout_height= "Match_parent" android:layout_weight= "1" > <LinearLayout
Android:layout_width= "Match_parent" android:layout_height= "Wrap_content" android:layout_centervertical= "true" android:orientation= "vertical" > <imageview android:id= "@+id/third_image" android:layout_width= "Wrap_content" "Android:layout_height=" wrap_content "android:layout_gravity=" Center_horizontal "android:src=" @android:d rawable/ Ic_menu_edit "/> <textview android:id= @+id/third_text" android:layout_width= "Wrap_content" Android:layout_height= "Wrap_content" android:layout_gravity= "Center_horizontal" android:layout_margintop= "5DP" Android: text= "Record" android:textcolor= "#7597B3"/> </LinearLayout> </RelativeLayout> <relativelayout android : id= "@+id/fourth_layout" android:layout_width= "0DP" android:layout_height= "Match_parent" android:layout_weight= "1 "> <linearlayout android:layout_width=" match_parent "android:layout_height=" Wrap_content "Android:layout_ Centervertical= "true" android:orientation= "vertical" > <imageview android:id= "@+id/fourth_image" Android:
Layout_width= "Wrap_content" android:layout_height= "wrap_content" android:layout_gravity= "Center_horizontal" android:src= "@android:d rawable/ic_menu_myplaces"/> <textview android:id= "@+id/fourth_text" Android:layout_ Width= "Wrap_content" android:layout_height= "wrap_content" android:layout_gravity= "Center_horizontal" Android: layout_margintop= "5DP" android:text= "my" android:textcolor= "#7597B3"/> </LinearLayout> </relativElayout> </LinearLayout> </LinearLayout> 

Please attach the source screenshot:


(2) for the first part of the layout of the top title bar, code see: Title_layout.xml:

<?xml version= "1.0" encoding= "Utf-8"?>
<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Match_parent"
android:layout_height= "56DP"
android:background= "@color/colorprimarydark"
android:orientation= "Horizontal" >
<imageview
Android:id= "@+id/title_imv"
Android:layout_width= "30DP"
android:layout_height= "30DP"
Android:layout_centervertical= "true"
android:layout_marginleft= "10DP"
android:src= "@android:d rawable/ic_menu_more"/>
<textview
Android:id= "@+id/title_text_tv"
Android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content"
Android:layout_centerinparent= "true"
android:text= "Home"
Android:textsize= "20SP"
Android:textcolor= "@color/bg_color"/>
</RelativeLayout>

See screenshot below:


(3) in the second part of the layout, we set up 4 more. XML layout files, respectively, named: Fg1.xml, Fg2.xml, Fg3.xml, Fg4.xml, the content of only changes in the TextView text description, such as the first page, changed to the second page. Only one of the fg1.xml is given below:

<?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=" Match_parent "
android: gravity= "center"
android:background= "@color/aquamarine"
android:orientation= "vertical" >
< TextView
android:layout_width= "wrap_content"
android:layout_height= "Wrap_content"
Second page "
android:textsize=" 22sp/>
</LinearLayout>

As shown in figure:


(4) Here also gives the colors.xml content of the list of values, the color is relatively complete but the name is not easy to remember:

&lt;?xml version= "1.0" encoding= "Utf-8"?&gt; &lt;resources&gt; &lt;color name= "colorprimary" &gt; #3F51B5 &lt;/color &gt; &lt;color name= "Colorprimarydark" &gt; #303F9F &lt;/color&gt; &lt;color name= "coloraccent" &gt; #FF4081 &lt;/color
&gt; &lt;color name= "Bg_color" &gt; #EDEDED &lt;/color&gt; &lt;color name= "Contents_text" &gt; #ff000000 &lt;/color&gt; &lt;color name= "Encode_view" &gt; #ffffffff &lt;/color&gt; &lt;color name= "Help_button_view" &gt; #ffcccccc &lt;/color &gt; &lt;color name= "Help_view" &gt; #ff404040 &lt;/color&gt; &lt;color name= "possible_result_points" &gt; #c0ffff00 &lt;/color&gt; &lt;color name= "Result_image_border" &gt; #ffffffff &lt;/color&gt; &lt;color name= "Result_minor_text" &gt; #ffc0c0c0 &lt;/color&gt; &lt;color name= "result_points" &gt; #c000ff00 &lt;/color&gt; &lt;color name= "Result_text" &gt; #ffffffff &lt;/color&gt; &lt;color name= "Result_view" &gt; #b0000000 &lt;/color&gt; &lt;color name= "Sbc_header_ Text "&gt; #ff808080 &lt;/color&gt; &lt;color name=" Sbc_header_view "&gt; #ffffffff &lt;/color&gt; &lt;cOlor name= "Sbc_list_item" &gt; #fffff0e0 &lt;/color&gt; &lt;color name= "Sbc_layout_view" &gt; #ffffffff &lt;/color&gt; &lt;color name= "Sbc_page_number_text" &gt; #ff000000 &lt;/color&gt; &lt;color name= "Sbc_snippet_text" &gt; #ff4b4b4b &lt;/color&gt; &lt;color name= "Share_text" &gt; #ff000000 &lt;/color&gt; &lt;color name= "Share_view" &gt; #ffffffff &lt; /color&gt; &lt;color name= "Status_view" &gt; #50000000 &lt;/color&gt; &lt;color name= "Status_text" &gt; #ffffffff &lt;/ color&gt; &lt;color name= "Transparent" &gt; #00000000 &lt;/color&gt; &lt;color name= "Viewfinder_frame" &gt; #ffffffff &lt;/color&gt; &lt;color name= "Viewfinder_laser" &gt; #ffff0000 &lt;/color&gt; &lt;color name= "Viewfinder_mask" &gt;# 60000000&lt;/color&gt; &lt;color name= "header" &gt; #58567D &lt;/color&gt; &lt;color name= "Grgray" &gt; #686868 &lt;/ color&gt; &lt;color name= "White" &gt; #FFFFFF &lt;/color&gt; &lt;!----&gt; &lt;color name= "Ivory" &gt; #FFFFF0 &lt;/
color&gt; &lt;!--ivory--&gt; &lt;color name= "Lightyellow" &gt; #FFFFE0 &lt;/color&gt; &lt;!--bright yellow--&gt;&lt;color name= "Yellow" &gt; #FFFF00 &lt;/color&gt; &lt;!--yellow--&gt; &lt;color name= "Snow" &gt; #FFFAFA &lt;/color&gt; &lt;!--Snow White--&gt; &lt;color name= "Floralwhite" &gt; #FFFAF0 &lt;/color&gt; &lt;!--flower White--&gt; &lt;color " LemonChiffon "&gt; #FFFACD &lt;/color&gt; &lt;!--lemon Silk--&gt; &lt;color name=" Cornsilk "&gt; #FFF8DC &lt;/color&gt; &lt;!- -Rice silk--&gt; &lt;color name= "Seashell" &gt; #FFF5EE &lt;/color&gt; &lt;!--Sea-color--&gt; &lt;color-name= "Lavenderblush" &gt;# Fff0f5&lt;/color&gt; &lt;!--mauve--&gt; &lt;color name= "papayawhip" &gt; #FFEFD5 &lt;/color&gt; &lt;!--wood color--&gt; &lt; Color name= "Blanchedalmond" &gt; #FFEBCD &lt;/color&gt; &lt;!--white apricot--&gt; &lt;color name= "Mistyrose" &gt; #FFE4E1 &lt;/ Color&gt; &lt;!--light Rose--&gt; &lt;color name= "Bisque" &gt; #FFE4C4 &lt;/color&gt; &lt;!--orange-yellow--&gt; &lt;color name= " Moccasin "&gt; #FFE4B5 &lt;/color&gt; &lt;!--buckskin color--&gt; &lt;color name=" Navajowhite "&gt; #FFDEAD &lt;/color&gt; &lt;!-- Nawa--&gt; &lt;color name= "Peachpuff" &gt; #FFDAB9 &lt;/color&gt; &lt;!--peach--&gt; &lt;color name= "GOld "&gt; #FFD700 &lt;/color&gt; &lt;!--golden--&gt; &lt;color name=" Pink "&gt; #FFC0CB &lt;/color&gt; &lt;!--pink--&gt; &lt;
Color name= "Lightpink" &gt; #FFB6C1 &lt;/color&gt; &lt;!--bright pink--&gt; &lt;color name= "Orange" &gt; #FFA500 &lt;/color&gt; &lt;!--Orange--&gt; &lt;color name= "Lightsalmon" &gt; #FFA07A &lt;/color&gt; &lt;!--bright flesh--&gt; &lt;color "Name=" &gt; #FF8C00 &lt;/color&gt; &lt;!--dark orange--&gt; &lt;color name= "Coral" &gt; #FF7F50 &lt;/color&gt; &lt;!--coral--&gt; &lt;
Color name= "Hotpink" &gt; #FF69B4 &lt;/color&gt; &lt;!--hot pink--&gt; &lt;color name= "tomato" &gt; #FF6347 &lt;/color&gt; &lt;!--tomato color--&gt; &lt;color name= "orangered" &gt; #FF4500 &lt;/color&gt; &lt;!--red Orange--&gt; &lt;color name= "Deeppink" &gt; #FF1493 &lt;/color&gt; &lt;!--deep pink--&gt; &lt;color name= "fuchsia" &gt; #FF00FF &lt;/color&gt; &lt;!--Magenta--&gt; &lt; Color name= "Magenta" &gt; #FF00FF &lt;/color&gt; &lt;!--red-purple--&gt; &lt;color name= "Red" &gt; #FF0000 &lt;/color&gt; &lt;!- -Red--&gt; &lt;color name= "Oldlace" &gt; #FDF5E6 &lt;/color&gt; &lt;!--old suit--&GT &lt;color name= "Lightgoldenyellow" &gt; #FAFAD2 &lt;/color&gt; &lt;!--bright golden yellow--&gt; &lt;color name= "linen" &gt; #FAF0E6 &lt;/color&gt; &lt;!--linen color--&gt; &lt;color name= "Antiquewhite" &gt; #FAEBD7 &lt;/color&gt; &lt;!--antique White--&gt; &lt;color Name= "Salmon" &gt; #FA8072 &lt;/color&gt; &lt;!--meat color--&gt; &lt;color name= "Ghostwhite" &gt; #F8F8FF &lt;/color&gt; &lt;! --Ghost White--&gt; &lt;color name= "Mintcream" &gt; #F5FFFA &lt;/color&gt; &lt;!--mint color--&gt; &lt;color name= "WhiteSmoke" &gt;# f5f5f5&lt;/color&gt; &lt;!--smoke white--&gt; &lt;color name= "Beige" &gt; #F5F5DC &lt;/color&gt; &lt;!--beige--&gt; &lt;color Name = "Wheat" &gt; #F5DEB3 &lt;/color&gt; &lt;!--light yellow--&gt; &lt;color name= "Sandybrown" &gt; #F4A460 &lt;/color&gt; &lt;!--Sandy Brown --&gt; &lt;color name= "Azure" &gt; #F0FFFF &lt;/color&gt; &lt;!--Azure--&gt; &lt;color name= "Honeydew" &gt; #F0FFF0 &lt;/ color&gt; &lt;!--Honey--&gt; &lt;color name= "AliceBlue" &gt; #F0F8FF &lt;/color&gt; &lt;!--Allisland--&gt; &lt;color name= " Khaki "&gt; #F0E68C &lt;/color&gt; &lt;!--Yellow-brown--&gt; &lt;color name=" Lightcoral "&gt; #F08080 &lt;/color&gt; &lt;!--bright Coral--&gt; &lt;color name= "Palegoldenrod" &gt; #EEE8AA &lt;/color&gt; &lt;!--cang Kylin color- &gt; &lt;color name= "Violet" &gt; #EE82EE &lt;/color&gt; &lt;!--Violet--&gt; &lt;color name= "DarkSalmon" &gt; #E9967A &lt;/ Color&gt; &lt;!--dark flesh--&gt; &lt;color name= "Lavender" &gt; #E6E6FA &lt;/color&gt; &lt;!--lavender--&gt; &lt;color name= " Lightcyan "&gt; #E0FFFF &lt;/color&gt; &lt;!--bright cyan--&gt; &lt;color name=" Burlywood "&gt; #DEB887 &lt;/color&gt; &lt;!-- Solid wood color--&gt; &lt;color name= "Plum" &gt; #DDA0DD &lt;/color&gt; &lt;!--plum--&gt; &lt;color name= "Gainsboro" &gt; #DCDCDC &lt; /color&gt; &lt;!--light grey--&gt; &lt;color name= "Crimson" &gt; #DC143C &lt;/color&gt; &lt;!--dark Crimson--&gt; &lt;color "
Palevioletred "&gt; #DB7093 &lt;/color&gt; &lt;!--pale violet--&gt; &lt;color name=" Goldenrod "&gt; #DAA520 &lt;/color&gt; &lt;!--golden Kylin color--&gt; &lt;color name= "Orchid" &gt; #DA70D6 &lt;/color&gt; &lt;!--lavender--&gt; &lt;color name= "Thistle" &gt;# D8bfd8&lt;/color&gt; &lt;!--thistle color--&gt; &lt;color name= "Lightgray" &gt; #D3D3D3 &lt;/color&Gt &lt;!--bright Gray--&gt; &lt;color name= "Lightgrey" &gt; #D3D3D3 &lt;/color&gt; &lt;!--bright Gray--&gt; &lt;color name= "Tan" &gt;# D2b48c&lt;/color&gt; &lt;!--Brown--&gt; &lt;color name= "Chocolate" &gt; #D2691E &lt;/color&gt; &lt;!--skillful color--&gt; &lt;
Color name= "Peru" &gt; #CD853F &lt;/color&gt; &lt;!--Peru color--&gt; &lt;color name= "indianred" &gt; #CD5C5C &lt;/color&gt; &lt;!--Indian red--&gt; &lt;color name= "mediumvioletred" &gt; #C71585 &lt;/color&gt; &lt;!--Violet--&gt; &lt;color-Name= " Silver "&gt; #C0C0C0 &lt;/color&gt; &lt;!--silver--&gt; &lt;color name=" Darkkhaki "&gt; #BDB76B &lt;/color&gt; &lt;!--dark brown-- &gt; &lt;color name= "Rosybrown" &gt; #BC8F8F &lt;/color&gt; &lt;!--maroon rose--&gt; &lt;color name= "Mediumorchid" &gt; #BA55D3 &lt;/color&gt; &lt;!--powder purple--&gt; &lt;color name= "Darkgoldenrod" &gt; #B8860B &lt;/color&gt; &lt;!--dark Golden--&gt; &lt; Color name= "Firebrick" &gt; #B22222 &lt;/color&gt; &lt;!--brick--&gt; &lt;color name= "Powderblue" &gt; #B0E0E6 &lt;/color &gt; &lt;!--powder blue--&gt; &lt;color name= "Lightsteelblue" &gt; #B0C4DE &lt;/color&gt; &lt;!--Bright steel blue--&gt; &lt;color name= "paleturquoise" &gt; #AFEEEE &lt;/color&gt; &lt;!--pale gem green--&gt; &lt;color name= "Greenyellow"
&gt; #ADFF2F &lt;/color&gt; &lt;!--yellow-green--&gt; &lt;color name= "LightBlue" &gt; #ADD8E6 &lt;/color&gt; &lt;!--bright blue--&gt; &lt;color name= "Darkgray" &gt; #A9A9A9 &lt;/color&gt; &lt;!--dark grey--&gt; &lt;color name= "Darkgrey" &gt; #A9A9A9 &lt;/color &gt; &lt;!--dark grey--&gt; &lt;color name= "Brown" &gt; #A52A2A &lt;/color&gt; &lt;!--Brown--&gt; &lt;color name= "Sienna" &gt;# a0522d&lt;/color&gt; &lt;!--ochre--&gt; &lt;color name= "DarkOrchid" &gt; #9932CC &lt;/color&gt; &lt;!--dark purple--&gt; &lt; Color name= "Palegreen" &gt; #98FB98 &lt;/color&gt; &lt;!--pale green--&gt; &lt;color name= "Darkviolet" &gt; #9400D3 &lt;/color &gt; &lt;!--dark violet--&gt; &lt;color name= "Mediumpurple" &gt; #9370DB &lt;/color&gt; &lt;!--Violet--&gt; &lt;color name= " LightGreen "&gt; #90EE90 &lt;/color&gt; &lt;!--bright green--&gt; &lt;color name=" Darkseagreen "&gt; #8FBC8F &lt;/color&gt; &lt;! --Dark--&gt; &lt;color name= "SaddleBrown" &gt; #8B4513 &lt;/color&gt; &lt;!--heavy Brown--&Gt &lt;color name= "Darkmagenta" &gt; #8B008B &lt;/color&gt; &lt;!--dark Magenta--&gt; &lt;color name= "darkred" &gt; #8B0000 &lt;/ color&gt; &lt;!--dark red--&gt; &lt;color name= "Blueviolet" &gt; #8A2BE2 &lt;/color&gt; &lt;!--Violet Blue--&gt; &lt;color name= " Lightskyblue "&gt; #87CEFA &lt;/color&gt; &lt;!--bright sky-blue--&gt; &lt;color name=" Skyblue "&gt; #87CEEB &lt;/color&gt; &lt;!-- sky-blue--&gt; &lt;color name= "Gray" &gt; #808080 &lt;/color&gt; &lt;!--grey--&gt; &lt;color name= "Grey" &gt; #808080 &lt;/color &gt; &lt;!--grey--&gt; &lt;color name= "Olive" &gt; #808000 &lt;/color&gt; &lt;!--Olive--&gt; &lt;color name= "Purple" &gt;# 800080&lt;/color&gt; &lt;!--purple--&gt; &lt;color name= "maroon" &gt; #800000 &lt;/color&gt; &lt;!--millet--&gt; &lt;color Name = "Aquamarine" &gt; #7FFFD4 &lt;/color&gt; &lt;!--green--&gt; &lt;color name= "Chartreuse" &gt; #7FFF00 &lt;/color&gt; &lt;! --Yellow green--&gt; &lt;color name= "Lawngreen" &gt; #7CFC00 &lt;/color&gt; &lt;!--green Grass--&gt; &lt;color name= "Mediumslateblue" &gt; #7B68EE &lt;/color&gt; &lt;!--dark blue--&gt; &lt;color name= "LightslategrAy "&gt; #778899 &lt;/color&gt; &lt;!--bright blue gray--&gt; &lt;color name=" Lightslategrey "&gt; #778899 &lt;/color&gt; &lt;!--bright blue grey- -&gt; &lt;color name= "Slategray" &gt; #708090 &lt;/color&gt; &lt;!--Gray stone color--&gt; &lt;color name= "Slategrey" &gt; #708090 &lt;/color&gt; &lt;!--Gray stone color--&gt; &lt;color name= "Olivedrab" &gt; #6B8E23 &lt;/color&gt; &lt;!--dark green Brown--&gt; &lt;color name = "Slateblue" &gt; #6A5ACD &lt;/color&gt; &lt;!--stone Blue--&gt; &lt;color name= "Dimgray" &gt; #696969 &lt;/color&gt; &lt;!-- Dark grey--&gt; &lt;color name= "Dimgrey" &gt; #696969 &lt;/color&gt; &lt;!--dark grey--&gt; &lt;color name= "Mediumaquamarine" &gt;#
66cdaa&lt;/color&gt; &lt;!--Green--&gt; &lt;color name= "Cornflowerblue" &gt; #6495ED &lt;/color&gt; &lt;!--chrysanthemum Blue--&gt; &lt;color name= "Cadetblue" &gt; #5F9EA0 &lt;/color&gt; &lt;!--military blue--&gt; &lt;color name= "Darkolivegreen" &gt; #556B2F &lt;/color&gt; &lt;!--dark olive green--&gt; &lt;color name= "Indigo" &gt; #4B0082 &lt;/color&gt; &lt;!--Indigo--&gt; &lt;color name= " Mediumturquoise "&gt; #48D1CC &lt;/color&gt; &lt;!--Emerald--&gt; &lt;color name=" darksLateblue "&gt; #483D8B &lt;/color&gt; &lt;!--dark blue--&gt; &lt;color name=" Steelblue "&gt; #4682B4 &lt;/color&gt; &lt;!-- Steel Blue--&gt; &lt;color name= "Royalblue" &gt; #4169E1 &lt;/color&gt; &lt;!--royal blue--&gt; &lt;color name= "Turquoise" &gt;#
40e0d0&lt;/color&gt; &lt;!--Green--&gt; &lt;color name= "Mediumseagreen" &gt; #3CB371 &lt;/color&gt; &lt;!--Aquamarine--&gt; &lt;color name= "Limegreen" &gt; #32CD32 &lt;/color&gt; &lt;!--Orange Green--&gt; &lt;color name= "Darkslategray" &gt; #2F4F4F &lt; /color&gt; &lt;!--Dark Tile grey--&gt; &lt;color name= "Darkslategrey" &gt; #2F4F4F &lt;/color&gt; &lt;!--dark Tile gray--&gt; &lt;color
Name= "Seagreen" &gt; #2E8B57 &lt;/color&gt; &lt;!--Sea Green--&gt; &lt;color name= "Forestgreen" &gt; #228B22 &lt;/color&gt; &lt;!--Forest Green--&gt; &lt;color name= "Lightseagreen" &gt; #20B2AA &lt;/color&gt; &lt;!--bright Sea blue--&gt; &lt;color " DodgerBlue "&gt; #1E90FF &lt;/color&gt; &lt;!--flashing blue--&gt; &lt;color name=" Midnightblue "&gt; #191970 &lt;/color&gt; &lt;! --Gray Blue--&gt; &lt;color name= "Aqua" &gt; #00FFFF &lt;/color&gt; &lt;!--light green--&gt; &lt;color name="Cyan" &gt; #00FFFF &lt;/color&gt; &lt;!--cyan--&gt; &lt;color name= "Springgreen" &gt; #00FF7F &lt;/color&gt; &lt;!--Spring Green-- &gt; &lt;color name= "Lime" &gt; #00FF00 &lt;/color&gt; &lt;!--acid Orange--&gt; &lt;color name= "Mediumspringgreen" &gt; #00FA9A &lt;/color&gt; &lt;!--Spring Green--&gt; &lt;color name= "darkturquoise" &gt; #00CED1 &lt;/color&gt; &lt;!--dark Gem Green--&gt; &lt; Color name= "Deepskyblue" &gt; #00BFFF &lt;/color&gt; &lt;!--deep Blue--&gt; &lt;color name= "Darkcyan" &gt; #008B8B &lt;/color &gt; &lt;!--dark cyan--&gt; &lt;color name= "Teal" &gt; #008080 &lt;/color&gt; &lt;!--duck color--&gt; &lt;color "Green" name= 008000&lt;/color&gt; &lt;!--Green--&gt; &lt;color name= "Darkgreen" &gt; #006400 &lt;/color&gt; &lt;!--dark green--&gt; &lt;color Name= "Blue" &gt; #0000FF &lt;/color&gt; &lt;!--blue--&gt; &lt;color name= "MediumBlue" &gt; #0000CD &lt;/color&gt; &lt;!-- Medium Blue--&gt; &lt;color name= "Darkblue" &gt; #00008B &lt;/color&gt; &lt;!--dark blue--&gt; &lt;color name= "Navy" &gt; #000080 &lt;/ Color&gt; &lt;!--Navy--&gt; &lt;color name= "Black" &gt; #000000 &lt;/color&gt; &lt;! --Black--&gt; &lt;/resources&gt;

As shown in figure:


So here we have the interface layout is basically completed!

Implementation class of corresponding fragment

Next we need to write four corresponding fragment implementation class, the same copy 4 copies, use inflate load fragment can. That is, contains four similar fragment, respectively named: Firstfragment.java, Secondfragment.java, Thirdfragment.java, Fourthfragment.java. The following is mainly to show the specific code of Firstfragment.java:

Package com.example.dm.myapplication;
Import Android.os.Bundle;
Import android.support.v4.app.Fragment;
Import Android.view.LayoutInflater;
Import Android.view.View;
Import Android.view.ViewGroup;
/**
* Created by DM on 16-3-29.
* The first page
*
/public class Firstfragment extends Fragment {
@Override public
View Oncreateview ( Layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {
View view = Inflater.inflate ( R.layout.fg1, container, false);
return view;
}

As shown in figure:


The most important mainactivity

The small parts of each pieces are ready to order, and now it is only the main interface implementation class that blends them together to achieve the corresponding effect.

Mainactivity.java writing is also very simple, directly look at the code and comments on it, not much explanation: mainly contains several initialization methods, check processing, hide all fragment methods. See Mainactivity.java for details:

Package com.example.dm.myapplication;
Import android.content.Intent;
Import Android.os.Bundle; Import android.support.v4.app.FragmentActivity;
Note Here we import the V4 package, do not guide the app's package import Android.support.v4.app.FragmentManager;
Import android.support.v4.app.FragmentTransaction;
Import Android.view.View;
Import Android.widget.FrameLayout;
Import Android.widget.ImageView;
Import Android.widget.RelativeLayout;
Import Android.widget.TextView;
/** * Home Page content * Created by DM on 16-1-19. */public class Mainactivity extends Fragmentactivity implements View.onclicklistener {//Initialize top bar display private ImageView titl
ELEFTIMV;
Private TextView Titletv;
Define 4 fragment objects private firstfragment fg1;
Private Secondfragment Fg2;
Private Thirdfragment fg3;
Private Fourthfragment Fg4;
Frame layout object, used to store fragment object private Framelayout framelayout;
Define the related controls in each option private relativelayout firstlayout;
Private Relativelayout secondlayout;
Private Relativelayout thirdlayout;
Private Relativelayout fourthlayout; Private ImageView FirStimage;
Private ImageView Secondimage;
Private ImageView Thirdimage;
Private ImageView Fourthimage;
Private TextView Firsttext;
Private TextView Secondtext;
Private TextView Thirdtext;
Private TextView Fourthtext;
Define several colors private int whirt = 0xFFFFFFFF;
private int gray = 0XFF7597B3;
private int dark = 0xff000000;
Defines the Fragmentmanager object Manager private Fragmentmanager Fragmentmanager; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (
R.layout.show_main_lay);
Fragmentmanager = Getsupportfragmentmanager (); Initview (); Initialize interface control Setchioceitem (0); Initial page load displays the first tab}/** * Initialize page/private void Initview () {//Initialize page title bar TITLELEFTIMV = (ImageView) Findviewbyid (r.id.ti
TLE_IMV); Titleleftimv.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View v) {startactivity (new
Intent (Mainactivity.this, Loginactivity.class));
}
});
Titletv = (TextView) Findviewbyid (R.ID.TITLE_TEXT_TV);
Titletv.settext ("home page"); // Initializes the control Firstimage = (ImageView) Findviewbyid (r.id.first_image) of the bottom navigation bar;
Secondimage = (ImageView) Findviewbyid (r.id.second_image);
Thirdimage = (ImageView) Findviewbyid (r.id.third_image);
Fourthimage = (ImageView) Findviewbyid (r.id.fourth_image);
Firsttext = (TextView) Findviewbyid (R.id.first_text);
Secondtext = (TextView) Findviewbyid (R.id.second_text);
Thirdtext = (TextView) Findviewbyid (R.id.third_text);
Fourthtext = (TextView) Findviewbyid (R.id.fourth_text);
Firstlayout = (relativelayout) Findviewbyid (r.id.first_layout);
Secondlayout = (relativelayout) Findviewbyid (r.id.second_layout);
Thirdlayout = (relativelayout) Findviewbyid (r.id.third_layout);
Fourthlayout = (relativelayout) Findviewbyid (r.id.fourth_layout);
Firstlayout.setonclicklistener (Mainactivity.this);
Secondlayout.setonclicklistener (Mainactivity.this);
Thirdlayout.setonclicklistener (Mainactivity.this);
Fourthlayout.setonclicklistener (Mainactivity.this); @Override public void OnClick (View v) {switch (V.getid ()) {case R. Id.first_layout:setChioceItem (0);
Break
Case R.id.second_layout:setchioceitem (1);
Break
Case R.id.third_layout:setchioceitem (2);
Break
Case R.id.fourth_layout:setchioceitem (3);
Break
Default:break; }/** * Settings Click Tab event Handling * @param index tab: 0, 1, 2, 3/private void Setchioceitem (int index) {fragmenttransaction F
Ragmenttransaction = Fragmentmanager.begintransaction (); Clearchioce ();
Empty, reset options, hide all fragment hidefragments (fragmenttransaction);
Switch (index) {case 0://Firstimage.setimageresource (r.drawable.xxxx); modify Firsttext.settextcolor (dark) if necessary;
Firstlayout.setbackgroundcolor (gray); 
If Fg1 is empty, create one and add to the interface if (FG1 = = null) {FG1 = new firstfragment (); Fragmenttransaction.add (R.id.content, FG1);} else {
If it is not empty, it is shown directly fragmenttransaction.show (FG1);
} break;
Case 1://Secondimage.setimageresource (R.DRAWABLE.XXXX);
Secondtext.settextcolor (Dark);
Secondlayout.setbackgroundcolor (gray); if (FG2 = = null) {FG2 = new secondfragment (); Fragmenttransaction.add (r.id. Content, FG2);
else {fragmenttransaction.show (FG2);} break;
Case 2://Thirdimage.setimageresource (R.DRAWABLE.XXXX);
Thirdtext.settextcolor (Dark);
Thirdlayout.setbackgroundcolor (gray); if (FG3 = = null) {FG3 = new thirdfragment (); Fragmenttransaction.add (R.id.content, FG3);} else {Fragmenttransaction.sho
W (FG3);
} break;
Case 3://Fourthimage.setimageresource (R.DRAWABLE.XXXX);
Fourthtext.settextcolor (Dark);
Fourthlayout.setbackgroundcolor (gray); if (FG4 = = null) {FG4 = new fourthfragment (); Fragmenttransaction.add (R.id.content, FG4);} else {fragmenttransaction.sh
ow (FG4);
} break; } fragmenttransaction.commit (); Submit}/** * When one of the tabs is selected, the other tabs are reset to the default */private void Clearchioce () {//Firstimage.setimageresource (R.DRAWABLE.XXX);
Text.settextcolor (gray);
Firstlayout.setbackgroundcolor (Whirt);
Secondimage.setimageresource (R.DRAWABLE.XXX);
Secondtext.settextcolor (gray);
Secondlayout.setbackgroundcolor (Whirt);
Thirdimage.setimageresource (R.DRAWABLE.XXX); Thirdtext.settextColor (gray);
Thirdlayout.setbackgroundcolor (Whirt);
Fourthimage.setimageresource (R.DRAWABLE.XXX);
Fourthtext.settextcolor (gray);
Fourthlayout.setbackgroundcolor (Whirt); 
/** * Hidden Fragment * * @param fragmenttransaction/private void hidefragments (Fragmenttransaction fragmenttransaction) { if (fg1!= null) {fragmenttransaction.hide (FG1);} if (Fg2!= null) {fragmenttransaction.hide (FG2);} if (Fg3!= null)
{fragmenttransaction.hide (FG3);}
if (fg4!= null) {fragmenttransaction.hide (FG4);}} }

See figure:

Here our function is basically realized, is not quite simple.

Attention

Fragment related class import is the V4 package or app bag! We are here to import the V4 package, in the comments section of the code, has been given instructions;

In the complete code package, we also added the app launch interface and animation, login interface, these two parts of the content, here do not make specific instructions, and then continue to improve.

The above content is small force to share the Android program development fragment implementation of the bottom navigation bar instance code, hope to help everyone!

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.