singleLine属性

自定义了一个带进度的button,需要单行显示,但是遇到一个很奇怪的问题:若这个button设置了android:singleLine="true"时,便不能正常画出绿色的进度背景。查了些资料说singleLine属性已经过时,建议使用lines属性,后来改为android:lines="1",绘制便正常了。原因还不清楚。先记下来。

            android:id="@+id/button"
        android:layout_width="70dp"
        android:layout_height="30dp"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:focusable="false"
        android:text="@string/hot_app_btn_download"
        android:lines="1"
        android:textSize="12dp" />

            android:id="@+id/button"
        android:layout_width="70dp"
        android:layout_height="30dp"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:focusable="false"
        android:text="@string/hot_app_btn_download"
        android:singleLine="true"

        android:textSize="12dp" />


自定义的带进度的button代码如下:

public class ProgressButton extends AmigoButton {
    private boolean mProgressEnable = true;
    private long mMax = 100;
    private int mProgress = 0;
    private Paint mPaint;
    private int corner;
    private int border;

    public void setProgressEnable(boolean progressEnable) {
        mProgressEnable = progressEnable;
    }

    public void setMax(long max) {
        mMax = max;
    }

    public void setProgress(int progress) {
        mProgress = progress;
        invalidate();
    }

    public ProgressButton(Context context) {
        super(context);
        init();
    }

    public ProgressButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        mPaint = new Paint();
        mPaint.setColor(getResources().getColor(R.color.hot_app_progress_btn_border_color, null));
        corner = getResources().getDimensionPixelSize(R.dimen.corner);
        border = getResources().getDimensionPixelSize(R.dimen.progressbtn_border);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if(mProgressEnable && mProgress > 0){
            RectF oval;
            if (mProgress < corner) {
                oval = new RectF(0, corner - mProgress, (getWidth() * mProgress) / mMax, getHeight()-(corner-mProgress));
                canvas.drawRoundRect(oval, mProgress, mProgress, mPaint);
            } else {
                oval = new RectF(0, 0, (getWidth() * mProgress) / mMax, getHeight());
                canvas.drawRoundRect(oval, corner, corner, mPaint);
            }
        }
        super.onDraw(canvas);
    }
}

你可能感兴趣的:(android)