Implementation of the custom bottom navigation bar with indicator, indicator navigation bar

Source: Internet
Author: User
Tags getcolor

Implementation of the custom bottom navigation bar with indicator, indicator navigation bar

Reprinted please indicate the source: http://blog.csdn.net/zhaokaiqiang1992

This article is intended to provide you with a custom bottom navigation bar with an indicator.

Let's take a look at the effect.



Pay attention to the following aspects when using this custom control:

1. There are no layout files and resource files. You only need a java file to call them.

2. It can be used flexibly, and a code can be added to the project.

3. Currently, only version 4.0 and later are supported. The color value is the color value that comes with the system. To use it in a lower version, replace the color value by yourself.

4. Supports smart adaptation. You can automatically adjust the layout according to the number of buttons at the bottom.

5. The main content area must be implemented using Fragment. by attaching it to Viewpager, the left and right sides of the interface can be slide.


The implementation of the main program is given below, and the notes are clear.

Package com. example. indicatornavigationbar; import android. app. activity; import android. content. context; import android. support. v4.view. viewPager; import android. support. v4.view. viewPager. onPageChangeListener; import android. util. displayMetrics; import android. view. gravity; import android. view. view; import android. view. view. onClickListener; import android. view. viewGroup; import android. view. animation. animation; import android. view. animation. translateAnimation; import android. widget. imageView; import android. widget. imageView. scaleType; import android. widget. linearLayout; import android. widget. textView;/***** @ ClassName: com. mengle. activity. optional * @ Description: Bottom navigation bar with indicator * @ author zhaokaiqiang * @ date 11:07:40 **/public class IndicatorNavigationBar extends LinearLayout implementsOnClickListener, OnPageChangeListener {// default height of the navigation bar, the indicator height is not included. Unit: dpprivate static final int HEIGHT_NAVIGATION_BAR = 40; // The default height of the indicator. Unit: dpprivate static final int HEIGHT_INDICATOR = 4; private Context context; private ViewPager viewPager; // indicator private ImageView ivBottomLine; // The currently displayed indexprivate int currIndex = 0; // store the mobile location private int positions []; // the number of titles private int titleCount; public IndicatorNavigationBar (Context context) {super (context); this. context = context;}/***** @ Description: attached to the parent Layout * @ param viewGroup * the navigation text displayed at the bottom of the parent Layout * @ param titles * @ param viewPager * bound ViewPager object * @ return void * /public void attachToParent (ViewGroup viewGroup, string [] titles, ViewPager viewPager) {this. viewPager = viewPager; titleCount = titles. length; // initialize the main layout setLayoutParams (new LayoutParams (LayoutParams. MATCH_PARENT, dip2px (HEIGHT_NAVIGATION_BAR + HEIGHT_INDICATOR); setBackgroundColor (getResources (). getColor (android. r. color. transparent); setOrientation (VERTICAL); // navigation bar layout LinearLayout ll_navigation = new LinearLayout (context); ll_navigation.setLayoutParams (new LayoutParams (LayoutParams. MATCH_PARENT, dip2px (HEIGHT_NAVIGATION_BAR); ll_navigation.setBackgroundColor (getResources (). getColor (android. r. color. holo_blue_light); ll_navigation.setOrientation (HORIZONTAL); // generate the navigation button (TextView) for (int I = 0; I <titles. length; I ++) {TextView textView = new TextView (context); textView. setLayoutParams (new LayoutParams (0, dip2px (HEIGHT_NAVIGATION_BAR), 1); textView. setText (titles [I]); textView. setGravity (Gravity. CENTER); textView. setTextSize (16); textView. setTextColor (getResources (). getColor (android. r. color. white); textView. setId (I); textView. setOnClickListener (this); ll_navigation.addView (textView);} // Add the navigation this. addView (ll_navigation); // indicator layout LinearLayout ll_indicator = new LinearLayout (context); ll_indicator.setLayoutParams (new LayoutParams (LayoutParams. MATCH_PARENT, dip2px (HEIGHT_INDICATOR); ll_indicator.setBackgroundColor (getResources (). getColor (android. r. color. holo_blue_light); ll_indicator.setOrientation (HORIZONTAL); // indicator ivBottomLine = new ImageView (context); ivBottomLine. setImageResource (android. r. color. holo_orange_light); ivBottomLine. setScaleType (ScaleType. MATRIX); ivBottomLine. setLayoutParams (new LinearLayout. layoutParams (getScreenWidth (context)/titleCount, dip2px (HEIGHT_INDICATOR); ll_indicator.addView (ivBottomLine); // Add indicator this. addView (ll_indicator); viewGroup. addView (this); viewPager. setOnPageChangeListener (this); // initialize the Moving position positions = new int [titleCount]; positions [0] = 0; int distance = (int) (getScreenWidth (context)/titleCount ); for (int I = 1; I <titleCount; I ++) {positions [I] = distance * I ;}@ Overridepublic void onClick (View v) {viewPager. setCurrentItem (v. getId () ;}@ Overridepublic void merge (int arg0) {}@ Overridepublic void merge (int position, float positionOffset, int positionOffsetPixels) {}@ Overridepublic void onPageSelected (int position) {Animation animation = new TranslateAnimation (currIndex * positions [1], positions [position], 0, 0); currIndex = position; animation. setFillAfter (true); animation. setDuration (300); ivBottomLine. startAnimation (animation);} private int dip2px (float dpValue) {final float scale = context. getResources (). getDisplayMetrics (). density; return (int) (dpValue * scale + 0.5f);} // obtain the screen width private int getScreenWidth (Context context) {DisplayMetrics dm = new DisplayMetrics (); (Activity) context ). getWindowManager (). getdefadisplay display (). getMetrics (dm); return dm. widthPixels ;}}


You can download the DEMO of this project on my github.

Https://github.com/ZhaoKaiQiang/IndicatorNavigationBar



Android developed the navigation bar above and the navigation bar controls at the bottom to achieve

There are several implementation methods:
1. Use Layout_weight to set the upper and lower sizes. The ListView in the middle is the same. Meanwhile, the listView in the middle can set its height to a fixed value.
2. You can also use the ViewPager control, or use layout_weight to split the page proportionally.

Ecshop customizes a navigation bar and now wants to publish relevant information on the navigation bar, but does not know how to publish it,

How to modify the bottom navigation bar to connect to a specified article

Some customers cannot find the modified location in the navigation bar at the bottom of the response, or they will not connect to the specified article. Now, let's know how to do this in the template:

For example, how does the company profile modify the content?
Step 1: Prepare the content.
The preparation content is to create a new article called Company Profile. This descriptive content is generally an article, so you can create an article in the background Article management-article list.

Click the magnifier

Then you can see the link to the specific article.

Step 2: replace the link.
In the background custom navigation
Replace the link with the link you just found.

This is done.



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.