Android small counter next to a button

前端 未结 3 972
情歌与酒
情歌与酒 2021-02-10 15:46

I would like to add a small counter next to a button to show the remaining quantity for some items, for example, the remaining number of tips remaining unused. Targeted layout w

相关标签:
3条回答
  • 2021-02-10 16:32

    use this - and keep updating the textviews text with the count

     <FrameLayout
         android:layout_width="wrap_content"
         android:layout_height="wrap_content">
    
     <ImageView
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:src="@drawable/your_phone_call_image"
         android:gravity="center"
         android:scaletype="matrix"/>
    
      <TextView
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="1"
         android:padding="5dp"
         android:gravity="top|right"  <!--this is important-->
         android:background="@drawable/your_counter_red_background"/>
     </FrameLayout>
    
    0 讨论(0)
  • 2021-02-10 16:42

    set Button's background Drawable to a custom Drawable like this one:

    enter image description here

    public class DecoratedTextViewDrawable extends LayerDrawable {
        private int mCnt = 0;
        private Paint mPaint;
        private TextView mParent;
        private ColorStateList mColors;
        private Rect mBounds;
    
        public DecoratedTextViewDrawable(TextView tv, Drawable[] layers, int cnt) {
            super(layers);
            mParent = tv;
            mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
            mPaint.setTextAlign(Align.CENTER);
            mPaint.setTextSize(tv.getTextSize());
            mPaint.setTypeface(Typeface.DEFAULT_BOLD);
            int[][] states = {
                    {android.R.attr.state_pressed}, {android.R.attr.state_focused}, {}
            };
            int[] colors = {
                    0xff0000aa, 0xff880000, 0xff00aa00
            };
            mColors = new ColorStateList(states, colors);
            mBounds = new Rect();
            setCnt(cnt);
        }
    
        public void setCnt(int cnt) {
            mCnt = cnt;
            String s = Integer.toString(cnt);
            mPaint.getTextBounds(s, 0, s.length(), mBounds);
            invalidateSelf();
        }
    
        @Override
        protected boolean onStateChange(int[] state) {
            invalidateSelf();
            return super.onStateChange(state);
        }
    
        @Override
        public boolean isStateful() {
            return true;
        }
    
        @Override
        public void draw(Canvas canvas) {
            super.draw(canvas);
    
            float x = mPaint.getTextSize() * 1.5f;
            float r = mPaint.getTextSize() * 0.9f;
            int base = mParent.getBaseline();
            int[] stateSet = getState();
    //        Log.d(TAG, "draw " + StateSet.dump(stateSet));
            int color = mColors.getColorForState(stateSet, 0xff000000);
            mPaint.setColor(color);
            canvas.drawCircle(x, base + mBounds.top + mBounds.height() / 2, r, mPaint);
            mPaint.setColor(0xffeeeeee);
            canvas.drawText(Integer.toString(mCnt), x, base, mPaint);
        }
    }
    

    you can use it like this:

    // Activity.onCreate method
    LinearLayout ll = new LinearLayout(this);
    ll.setOrientation(LinearLayout.VERTICAL);
    
    int NUM = 5;
    final int[] cnt = new int[NUM];
    Random r = new Random();
    for (int i = 0; i < NUM; i++) {
        cnt[i] = r.nextInt(20);
        Button b = new Button(this);
        b.setText("Click me");
        b.setTextSize(18);
        b.setTag(i);
    
        Drawable[] layers = {b.getBackground()};
        Drawable d = new DecoratedTextViewDrawable(b, layers, cnt[i]);
        b.setBackgroundDrawable(d);
        OnClickListener l = new OnClickListener() {
            @Override
            public void onClick(View v) {
                DecoratedTextViewDrawable  d = (DecoratedTextViewDrawable) v.getBackground();
                int idx = (Integer) v.getTag();
                d.setCnt(++cnt[idx]);
            }
        };
        b.setOnClickListener(l);
        ll.addView(b);
    }
    setContentView(ll);
    
    0 讨论(0)
  • 2021-02-10 16:48

    I suggest you can use a FrameLayout. 1.- you can use 2 images one for your button, one for the little circle and use a textview for the numbers... 2.- you can use one image for your button, and create a gradient for the little circle and a text view..

    For the frameLayout see :http://developer.android.com/reference/android/widget/FrameLayout.html For the gradient see: How to make gradient background in android.

    0 讨论(0)
提交回复
热议问题