【问题标题】:Android small counter next to a button一个按钮旁边的安卓小计数器
【发布时间】:2013-09-25 16:02:15
【问题描述】:

我想在按钮旁边添加一个小计数器,以显示某些物品的剩余数量,例如,剩余未使用的小费数量。目标布局如下所示:

问题:

我研究了网络,发现有人说要为每个数量使用不同的图片。但是数量可以达到100个怎么解决呢?真的有必要画出来吗?

我正在考虑在RelativeLayout 中将两个按钮粘在一起,这样当用户按下底部按钮时,顶部按钮将向上和向下计数,setText 本身,但是有更好的解决方案或导入吗?

编辑:

感谢 Rupesh 的代码和建议!我已经实现如下。但是您知道如何将红色圆圈文本视图进一步向右移动吗?并且 20 也无法正确显示在红色圆圈中...

代码:

<FrameLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:layout_marginTop="10dp" >

    <Button
        android:id="@+id/button_tip"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginBottom="1dp"
        android:layout_marginLeft="2dp"
        android:layout_marginRight="2dp"
        android:layout_marginTop="5dp"
        android:background="@drawable/orange_btn"
        android:onClick="button_tip_click"
        android:text="Hello" />

    <TextView
        android:layout_width="15dp"
        android:layout_height="15dp"
        android:layout_gravity="top|right"
        android:background="@drawable/red_circle_btn"
        android:gravity="center"
        android:text="20"
        android:textColor="@color/white"
        android:textSize="8sp"
        android:textStyle="bold" />
</FrameLayout>

【问题讨论】:

  • 查看我的解决方案,使用自定义 Drawable 你可以解决你的两个问题

标签: android button counter


【解决方案1】:

将 Button 的背景 Drawable 设置为像这样的自定义 Drawable:

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);
    }
}

你可以这样使用它:

// 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);

【讨论】:

  • 非常感谢,代码看起来很复杂,我会调查并恢复!
  • 复杂?它只是装饰了一个Button的std背景drawable,并且不需要任何额外的FrameLayouts和TextViews,尽可能简单
【解决方案2】:

使用这个 - 并继续使用计数更新 textviews 文本

 <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>

【讨论】:

  • 非常感谢您的建议!我已经实现了您的代码并在问题中进行了编辑...但是您知道如何将红色按钮向右移动一点,以便可以正确看到底部按钮的文本,并且圆圈中的数字正确显示(我在使用 wrap_content 之前尝试过,但它不再是圆圈了)
【解决方案3】:

我建议你可以使用 FrameLayout。 1.- 您可以使用 2 张图片,一张作为按钮,一张用于小圆圈,并使用 textview 作为数字... 2.- 您可以为按钮使用一张图片,并为小圆圈和文本视图创建渐变..

frameLayout 见:http://developer.android.com/reference/android/widget/FrameLayout.html 渐变见:How to make gradient background in android

【讨论】:

    猜你喜欢
    • 2012-03-30
    • 1970-01-01
    • 2013-09-16
    • 1970-01-01
    • 2012-08-06
    • 2022-01-12
    • 2015-08-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多