Android控制項TextProgressBar進度條上顯文字

來源:互聯網
上載者:User

標籤:android   c   style   class   blog   code   

Android系統的進度條控制項預設的設計的不是很周全,比如沒有包含文字的顯示,那麼如何在Android進度條控制項上顯示文字呢? 來自Google內部的代碼來瞭解下,主要使用的addView這樣的方法通過覆蓋一層Chronometer秒錶控制項來實現,整個代碼如下 :

 public class TextProgressBar extends RelativeLayout implements OnChronometerTickListener {    public static final String TAG = "TextProgressBar";         static final int CHRONOMETER_ID = android.R.id.text1;    static final int PROGRESSBAR_ID = android.R.id.progress;        Chronometer mChronometer = null;    ProgressBar mProgressBar = null;        long mDurationBase = -1;    int mDuration = -1;    boolean mChronometerFollow = false;    int mChronometerGravity = Gravity.NO_GRAVITY;        public TextProgressBar(Context context, AttributeSet attrs, int defStyle) {        super(context, attrs, defStyle);    }    public TextProgressBar(Context context, AttributeSet attrs) {        super(context, attrs);    }    public TextProgressBar(Context context) {        super(context);    }    //Android開發網提示關鍵區段在這裡    @Override    public void addView(View child, int index, ViewGroup.LayoutParams params) {        super.addView(child, index, params);                int childId = child.getId();        if (childId == CHRONOMETER_ID && child instanceof Chronometer) {            mChronometer = (Chronometer) child;            mChronometer.setOnChronometerTickListener(this);                        // Check if Chronometer should move with with ProgressBar             mChronometerFollow = (params.width == ViewGroup.LayoutParams.WRAP_CONTENT);            mChronometerGravity = (mChronometer.getGravity() & Gravity.HORIZONTAL_GRAVITY_MASK);                    } else if (childId == PROGRESSBAR_ID && child instanceof ProgressBar) {            mProgressBar = (ProgressBar) child;        }    }     @android.view.RemotableViewMethod    public void setDurationBase(long durationBase) {        mDurationBase = durationBase;                if (mProgressBar == null || mChronometer == null) {            throw new RuntimeException("Expecting child ProgressBar with id " +                    "‘android.R.id.progress‘ and Chronometer id ‘android.R.id.text1‘");        }                // Update the ProgressBar maximum relative to Chronometer base        mDuration = (int) (durationBase - mChronometer.getBase());        if (mDuration <= 0) {            mDuration = 1;        }        mProgressBar.setMax(mDuration);    }         public void onChronometerTick(Chronometer chronometer) {        if (mProgressBar == null) {            throw new RuntimeException(                "Expecting child ProgressBar with id ‘android.R.id.progress‘");        }                // Stop Chronometer if we‘re past duration        long now = SystemClock.elapsedRealtime();        if (now >= mDurationBase) {            mChronometer.stop();        }         int remaining = (int) (mDurationBase - now);        mProgressBar.setProgress(mDuration - remaining);                   if (mChronometerFollow) {            RelativeLayout.LayoutParams params;                         params = (RelativeLayout.LayoutParams) mProgressBar.getLayoutParams();            int contentWidth = mProgressBar.getWidth() - (params.leftMargin + params.rightMargin);            int leadingEdge = ((contentWidth * mProgressBar.getProgress()) /                    mProgressBar.getMax()) + params.leftMargin;                         int adjustLeft = 0;            int textWidth = mChronometer.getWidth();            if (mChronometerGravity == Gravity.RIGHT) {                adjustLeft = -textWidth;            } else if (mChronometerGravity == Gravity.CENTER_HORIZONTAL) {                adjustLeft = -(textWidth / 2);            }                         leadingEdge += adjustLeft;            int rightLimit = contentWidth - params.rightMargin - textWidth;            if (leadingEdge < params.leftMargin) {                leadingEdge = params.leftMargin;            } else if (leadingEdge > rightLimit) {                leadingEdge = rightLimit;            }                        params = (RelativeLayout.LayoutParams) mChronometer.getLayoutParams();            params.leftMargin = leadingEdge;                         mChronometer.requestLayout();                    }    }}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.