e-commerce project must have a shopping cart column, and the shopping cart button in the upper right corner and there must be a red circle showing the number of small head, as shown below:
In the face of such an interface, we have a lot of choices to build the framework, here I only say 2 kinds of I think it is possible. First: Radiobutton+badgeview with display 1, the bottom button navigation bar layout:
<?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=" wrap_content "android:background=" @color/t Itle_bg_color "android:orientation=" vertical "> <view style=" @style/viewhorizontalline "Android Oid:layout_above= "@+id/rg_tab_bar"/> <radiogroup android:id= "@+id/rg_tab_bar" Android:layout_w Idth= "Match_parent" android:layout_height= "Wrap_content" android:layout_alignparentbottom= "true" a
ndroid:checkedbutton= "@+id/rbhome" android:gravity= "center_vertical" android:orientation= "Horizontal" android:paddingleft= "6DP" android:paddingright= "6DP" android:paddingtop= "6DP" > <radiobu Tton android:id= "@+id/rbhome" style= "@style/raidobuttonstyle" android:drawabletop= "@dr Awable/btn_tabbar_home_selector "
android:text= "@string/tab_home"/> <radiobutton android:id= "@+id/rbbuy" style= "@style/raidobuttonstyle" android:drawabletop= "@drawable/btn_tabbar_buy_selector" Android: text= "@string/tab_buy"/> <radiobutton android:id= "@+id/rbcategory" style= "@style/R Aidobuttonstyle "android:drawabletop=" @drawable/btn_tabbar_category_selector "android:text=" @strin G/tab_category "/> <radiobutton android:id=" @+id/rbcart "style=" @style/raidobuttons Tyle "android:drawabletop=" @drawable/btn_tabbar_cart_selector "android:text=" @string/tab_cart "/&G
T <radiobutton android:id= "@+id/rbusercenter" style= "@style/raidobuttonstyle" Android :d rawabletop= "@drawable/btn_tabbar_user_center_selector" android:text= "@string/tab_user_center"/> < ;/radiogroup> <linearlayout android:layout_width= "match_parent" android:layout_height= "55DP" Androi D:layout_alignparentbottom= "true" android:background= "@color/transparent" android:numcolumns= "5" a ndroid:orientation= "Horizontal" > <button style= "@style/badgeviewbuttonstyle"/> <button styl
E= "@style/badgeviewbuttonstyle"/> <button style= "@style/badgeviewbuttonstyle"/> <button Android:id= "@+id/btn_msg" style= "@style/badgeviewbuttonstyle"/> <button style= "@styl E/badgeviewbuttonstyle "/> </LinearLayout> </RelativeLayout>
2, the layout of Activity_main:
<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout
xmlns:android= "http://schemas.android.com/apk/ Res/android "
xmlns:tools=" Http://schemas.android.com/tools "
android:orientation=" vertical "
Android : layout_width= "match_parent"
android:layout_height= "match_parent"
tools:context= ". Mainactivity "
android:cliptopadding=" true "
android:fitssystemwindows=" true ">
<framelayout
android:id= "@+id/fragmentcontent"
android:layout_width= "match_parent"
android:layout_height= " 0DP "
android:layout_weight=" 1 "
android:background=" @color/white "/>
<!--<include layout= "@layout/bottom_tab_bar"/>-->
</LinearLayout>
3, Badgeview tool class:
Import Android.content.Context;
Import android.content.res.Resources;
Import Android.graphics.Color;
Import Android.graphics.Typeface;
Import android.graphics.drawable.ShapeDrawable;
Import Android.graphics.drawable.shapes.RoundRectShape;
Import Android.support.v7.widget.AppCompatTextView;
Import Android.util.AttributeSet;
Import Android.util.TypedValue;
Import android.view.Gravity;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.view.ViewGroup.LayoutParams;
Import android.view.ViewParent;
Import Android.view.animation.AccelerateInterpolator;
Import android.view.animation.AlphaAnimation;
Import android.view.animation.Animation;
Import Android.view.animation.DecelerateInterpolator;
Import Android.widget.FrameLayout;
Import Android.widget.TabWidget;
/** * Description:com.mmbao.saas.util class * Created by Administrator on 2018/1/25. * Maxim:there is no smoke without fire */public class Badgeview extends Appcompattextview {public static final in T position_toP_left = 1;
public static final int position_top_right = 2;
public static final int position_bottom_left = 3;
public static final int position_bottom_right = 4;
public static final int position_center = 5;
private static final int default_margin_dip = 5;
private static final int default_lr_padding_dip = 5;
private static final int default_corner_radius_dip = 8;
private static final int default_position = position_top_right; private static final int default_badge_color = Color.parsecolor ("#CCFF0000");
color.red;
private static final int default_text_color = Color.White;
private static Animation fadeIn;
private static Animation FadeOut;
Private context context;
Private View target;
private int badgeposition;
private int Badgemarginh;
private int BADGEMARGINV;
private int badgecolor;
Private Boolean Isshown;
Private Shapedrawable BADGEBG;
private int targettabindex; Public Badgeview (Context COntext) {This (context, (AttributeSet) null, Android.
R.attr.textviewstyle); } public Badgeview (context context, AttributeSet Attrs) {This (context, Attrs, Android.
R.attr.textviewstyle); }/** * Constructor-* <p> * Create a new Badgeview instance attached to a target {@link Androi
D.view.view}. * * @param context context for this view.
* @param target the View to attach the badge to. */Public Badgeview (context context, View target) {This (context, NULL, Android.
R.attr.textviewstyle, target, 0); }/** * Constructor-* <p> * Create a new Badgeview instance attached to a target {@link Androi
D.widget.tabwidget} * tab at a given index.
* * @param context context for this view.
* @param target the tabwidget to attach the badge.
* @param index The position of the tab within the target. */Public Badgeview (context context, Tabwidget tarGET, int index) {This (context, NULL, Android.
R.attr.textviewstyle, target, index); } public Badgeview (context context, AttributeSet Attrs, int. defstyle) {This (context, attrs, Defstyle, NULL,
0); } public Badgeview (context context, AttributeSet attrs, int defstyle, View target, int tabIndex) {SUPER (cont
Ext, attrs, defstyle);
Init (context, target, tabIndex);
} private void init (context context, View target, int tabIndex) {this.context = context;
This.target = target;
This.targettabindex = TabIndex;
Apply defaults badgeposition = default_position;
Badgemarginh = Diptopixels (DEFAULT_MARGIN_DIP);
BADGEMARGINV = Badgemarginh;
Badgecolor = Default_badge_color;
Settypeface (Typeface.default_bold);
int paddingpixels = Diptopixels (DEFAULT_LR_PADDING_DIP);
Setpadding (paddingpixels, 0, paddingpixels, 0); SetTextColor (Default_text_color);
FadeIn = new Alphaanimation (0, 1);
Fadein.setinterpolator (New Decelerateinterpolator ());
Fadein.setduration (200);
FadeOut = new Alphaanimation (1, 0);
Fadeout.setinterpolator (New Accelerateinterpolator ());
Fadeout.setduration (200);
Isshown = false;
if (this.target! = null) {ApplyTo (this.target);
} else {show ();
}} private void ApplyTo (View target) {layoutparams LP = Target.getlayoutparams ();
Viewparent parent = Target.getparent ();
Framelayout container = new Framelayout (context); if (target instanceof Tabwidget) {//Set Target to the relevant tab child container target = ((T
Abwidget) target). Getchildtabviewat (Targettabindex);
This.target = target; (viewgroup) target). AddView (Container, New Layoutparams (Layoutparams.fill_parent, layoutparams.fill_p
arent)); This.setvisibility (View.gone);
Container.addview (this);
} else {//TODO Verify that the parent is indeed a viewgroup viewgroup group = (viewgroup) parent;
int index = Group.indexofchild (target);
Group.removeview (target);
Group.addview (container, index, LP);
Container.addview (target);
This.setvisibility (View.gone);
Container.addview (this);
Group.invalidate ();
}}/** * make the badge visible in the UI.
*/public void Show () {show (false, NULL);
}/** * Make the badge visible in the UI.
* * @param animate flag to apply the default fade-in animation.
*/public void Show (Boolean animate) {Show (animate, FadeIn);
}/** * Make the badge visible in the UI.
* * @param anim Animation to apply to the view when made visible. */public void Show (anImation Anim) {Show (true, Anim);
}/** * make the badge non-visible in the UI.
*/public void hide () {hide (false, null);
}/** * make the badge non-visible in the UI.
* * @param animate flag to apply the default fade-out animation.
*/public void Hide (Boolean animate) {Hide (animate, fadeOut);
}/** * make the badge non-visible in the UI.
* * @param anim Animation to apply to the view when made non-visible.
*/public void Hide (Animation anim) {Hide (true, anim);
}/** * Toggle The badge visibility in the UI.
*/public void Toggle () {Toggle (false, NULL, NULL);
}/** * Toggle The badge visibility in the UI.
* * @param animate flag to apply the default Fade-in/out animation.
*/public void Toggle (Boolean animate) {Toggle (animate, FadeIn, FadeOut); }/** * Toggle the badge visibilitY in the UI.
* * @param animin Animation to apply to the view when made visible.
* @param animout Animation to apply to the view when made non-visible.
*/public void Toggle (Animation animin, Animation animout) {Toggle (True, Animin, animout); private void Show (Boolean animate, Animation anim) {if (getbackground () = = null) {if (BADGEBG
= = null) {BADGEBG = Getdefaultbackground ();
} setbackgrounddrawable (BADGEBG);
} applylayoutparams ();
if (animate) {this.startanimation (anim);
} this.setvisibility (view.visible);
Isshown = true;
} private void Hide (Boolean animate, Animation anim) {this.setvisibility (view.gone);
if (animate) {this.startanimation (anim);
} Isshown = false; } private void Toggle (Boolean animate, Animation animin, Animation animout) {if (Isshown) {Hide (animate && (animout! = null), animout);
} else {Show (animate && (animin! = null), animin); }}/** * Increment the Numeric badge label.
If the current badge label cannot is converted to * a integer value, its label would be set to "0".
* * @param offset the increment offset.
*/public int increment (int offset) {Charsequence txt = getText ();
int i;
if (txt! = null) {try {i = Integer.parseint (txt.tostring ());
} catch (NumberFormatException e) {i = 0;
}} else {i = 0;
} i = i + offset;
SetText (string.valueof (i));
return i; }/** * Decrement the numeric badge label.
If the current badge label cannot is converted to * a integer value, its label would be set to "0".
* * @param offset the decrement offset. */public int decrement (int offset) {return increment (-offset);
} private Shapedrawable Getdefaultbackground () {int r = diptopixels (DEFAULT_CORNER_RADIUS_DIP);
float[] Outerr = new Float[]{r, R, R, R, R, R, R, r};
Roundrectshape rr = new Roundrectshape (outerr, NULL, NULL);
Shapedrawable drawable = new shapedrawable (RR);
Drawable.getpaint (). SetColor (Badgecolor);
return drawable; } private void Applylayoutparams () {framelayout.layoutparams LP = new Framelayout.layoutparams (layoutparams .
Wrap_content, layoutparams.wrap_content); Switch (badgeposition) {case POSITION_TOP_LEFT:lp.gravity = Gravity.left |
Gravity.top;
Lp.setmargins (Badgemarginh, BADGEMARGINV, 0, 0);
Break Case POSITION_TOP_RIGHT:lp.gravity = Gravity.right |
Gravity.top; Lp.setmargins (0, BADGEMARGINV, Badgemarginh, 0);
Break Case POSITION_BOTTOM_LEFT:lp.gravity = Gravity.left |
Gravity.bottom;
Lp.setmargins (badgemarginh, 0, 0, BADGEMARGINV);
Break Case POSITION_BOTTOM_RIGHT:lp.gravity = Gravity.right |
Gravity.bottom;
Lp.setmargins (0, 0, Badgemarginh, BADGEMARGINV);
Break
Case POSITION_CENTER:lp.gravity = Gravity.center;
Lp.setmargins (0, 0, 0, 0);
Break
Default:break;
} setlayoutparams (LP);
}/** * Returns the target View This badge have been attached to.
*/Public View Gettarget () {return target;
}/** * is this badge currently visible in the UI? */@Override PU