1.先來張效果圖
2.自定義一個角標工具類BottomBarView 。
** * Created by Administrator on 2016/12/27. * 角標工具類 */ public class BottomBarView extends RelativeLayout { private Context context; private TextView bar_num; private int count = 0; public BottomBarView(Context context) { this(context, null); } public BottomBarView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public BottomBarView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); this.context = context; RelativeLayout rl = (RelativeLayout) LayoutInflater.from(context).inflate(R.layout.bottom_bar_view, this, true); bar_num = (TextView) rl.findViewById(R.id.bar_num); bar_num.setVisibility(GONE); } public void add() { bar_num.setVisibility(VISIBLE); count++; if (count < 100) { bar_num.setText(count + ""); } else { bar_num.setText("99+"); } } public void add(int n) throws Exception { if(n<0){ throw new Exception(BottomBarView.class.getSimpleName()+" add(int n).The param must be a positive num"); } bar_num.setVisibility(VISIBLE); count += n; if (count < 100) { bar_num.setText(count + ""); } else { bar_num.setText("99+"); } } public void delete() { if (count == 0) { bar_num.setVisibility(GONE); } else { count--; if (count == 0) { bar_num.setVisibility(GONE); } else if (count > 0 && count < 100) { bar_num.setVisibility(VISIBLE); bar_num.setText(count + ""); } else { bar_num.setVisibility(VISIBLE); bar_num.setText("99+"); } } } public void deleteAll() { count = 0; bar_num.setVisibility(GONE); } }
3.工具類的一個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="wrap_content" > <ImageView android:id="@+id/imggwc" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_toLeftOf="@+id/imggenduo" android:src="@drawable/chaoshi_shopping_nav_icon" /> <TextView android:id="@+id/bar_num" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="-12dp" android:layout_toRightOf="@+id/imggwc" android:background="@drawable/red_dot_bg" android:text="1" android:gravity="center" android:textColor="#FFFFFF" android:textSize="10dp" /> </RelativeLayout>
4.Activity的實現
public static BottomBarView fragment_bottom_bar; fragment_bottom_bar = (BottomBarView) findViewById(R.id.fragment_bottom_bar); //購物車數量角標數據 public static final void gwcsl() { Map<String, String> map = new HashMap<String, String>(); map.put(ConstantUtil.TOKEN, SpUtil.get(ConstantUtil.TOKEN, "")); NormalPostRequest npr = new NormalPostRequest(MyUrlUtils.getFullURL(BaseServerConfig.CSGWCSL), new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { try { String code = response.getString("code"); if (BaseServerConfig.CODE_SUCCESS.equals(code)) { //角標數 int jiaobiao = Integer.parseInt(response.getString("resultCode")); try { fragment_bottom_bar.deleteAll(); if (jiaobiao > 0) { fragment_bottom_bar.add(jiaobiao); } else { fragment_bottom_bar.deleteAll(); } } catch (Exception e) { e.printStackTrace(); } } else { } } catch (JSONException e) { } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { } }, map); BZApplication.getRequestQueue().add(npr); }
5.activity的xml布局
<RelativeLayout android:id="@+id/csgwcdj" android:layout_width="45dp" android:layout_height="match_parent" android:layout_toLeftOf="@+id/relative"> <com.zjtd.bzcommunity.view.BottomBarView android:id="@+id/fragment_bottom_bar" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerVertical="true" /> </RelativeLayout>
其實這個小功能很簡單,只是你們想得太復雜。。。。。。。
以上就是本文的全部內容,希望本文的內容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持億速云!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。