通過Activity的ViewGroup添加ProgressBar,viewgroup

來源:互聯網
上載者:User

通過Activity的ViewGroup添加ProgressBar,viewgroup

ProgressBar是一個常見的組件,我們可以通過多種方式來實現一個progressBar,有一種方式是通過一個Activity的的window來放置一個bar,因為android中大家都知道的關聯關係是:Activity->Window->View[ViewGroup]  ,那我們通過當前Activity的window來載入一個progressBar,當我們把這個Activity作為一個基類的時候,那麼它的子類和我們附加在上面的Fragment就可以輕鬆的展示一個progressbar了[我們把這個自訂的progressbar做一個單例即可]

具體的實現流程如下:

1:需要展示的progressbar布局

      一個linearLayout裡麵包含一個要提示的TextView和一個進度條

<?xml version="1.0" encoding="UTF-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:background="#00000000"    android:gravity="center" >    <LinearLayout        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:background="@color/sdk_semitransparent"        android:gravity="center"        android:padding="15dp" >        <ProgressBar            style="@style/sdk_load_progress_style"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:indeterminateDrawable="@drawable/sdk_load_progressbar" />        <TextView            android:id="@+id/progress_txt"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_marginLeft="5dp"            android:text="@string/sdk_loading" >        </TextView>    </LinearLayout></LinearLayout>
2:實現自訂progressbar

     裡面的注釋還是比較清楚的,就是通過Activity擷取到一個Window,然後添加進來一個我們需要展示的布局

public class ProgressView {    //要展示的progressbar布局    private View mDialogView;    //從當前Activity擷取的ViewGroup 【mFLayout】    private ViewGroup mFragmentView;    //建立一個layout,放置我們的progressbar,主要作用是設定一些現實屬性    private LinearLayout mFLayout;       //單例對象    private static ProgressView mInstance;    //需要提示的文字View    private TextView mProgressText;    //設定是否可取消    private boolean mCancelable = true;    private ProgressView() {    }    public static synchronized ProgressView getInstance() {        if (mInstance == null) {            mInstance = new ProgressView();        }        return mInstance;    }    public void showProgressView(Activity act, int loadingTextId,            boolean cancelable) {        showProgressView(act, ResUtil.getString(loadingTextId), cancelable);    }    public void showProgressView(Activity act, int loadingTextId) {        showProgressView(act, ResUtil.getString(loadingTextId), true);    }    public void showProgressView(Activity act, String tipStr) {        showProgressView(act, tipStr, true);    }    public void showDilaogProgressView(Activity act, String tipStr) {        dialogProgressView(act, tipStr, true);    }    /**     *      * 〈一句話功能簡述〉<br>     * 〈功能詳細描述〉     *      * @param act content     * @param loading_text progress顯示的String     */    public void showProgressView(Activity act, String loading_text,            boolean cancelable) {        mDialogView = act.getLayoutInflater().inflate(                R.layout.sdk_dialog_progress, null);        mProgressText = (TextView) mDialogView.findViewById(R.id.progress_txt);
        //這個layout_base就是我們Activity中內容要顯示的一個跟布局        mFragmentView = (ViewGroup) act.getWindow().findViewById(                R.id.layout_base);        mCancelable = cancelable;        mFLayout = new LinearLayout(act);        if (!TextUtils.isEmpty(loading_text)) {            mProgressText.setText(loading_text);        }        init();    }    /**     *      * 〈一句話功能簡述〉<br>     * 〈功能詳細描述〉     *      * @param act content     * @param loading_text progress顯示的String     */    public void dialogProgressView(Activity act, String loading_text,            boolean cancelable) {        mDialogView = act.getLayoutInflater().inflate(                R.layout.sdk_dialog_progress, null);        mProgressText = (TextView) mDialogView.findViewById(R.id.progress_txt);        mFragmentView = (ViewGroup) act.getWindow().findViewById(                R.id.skd2_base_content);        mCancelable = cancelable;        mFLayout = new LinearLayout(act);        if (!TextUtils.isEmpty(loading_text)) {            mProgressText.setText(loading_text);        }        init();    }    private void init() {        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(                LinearLayout.LayoutParams.MATCH_PARENT,                LinearLayout.LayoutParams.MATCH_PARENT);        params.addRule(RelativeLayout.CENTER_IN_PARENT);        mFLayout.setLayoutParams(params);        mFLayout.setBackgroundColor(ResUtil.getColor(R.color.sdk_transparent));        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(                LinearLayout.LayoutParams.MATCH_PARENT,                LinearLayout.LayoutParams.WRAP_CONTENT);        layoutParams.gravity = Gravity.CENTER;        layoutParams.setMargins(20, 0, 20, 0);        mFLayout.addView(mDialogView, layoutParams);        addDialogContentView();    }    private void addDialogContentView() {        if (mFragmentView != null) {            mFragmentView.addView(mFLayout, new LinearLayout.LayoutParams(                    LinearLayout.LayoutParams.MATCH_PARENT,                    LinearLayout.LayoutParams.MATCH_PARENT));        }        mFLayout.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                // 當progress彈出時攔截頁面點擊事件            }        });    }    public void dismissProgress() {        if (mFragmentView != null && mFLayout != null) {            mFragmentView.removeView(mFLayout);        }        reset();    }    public boolean onKeyDown(int keyCode, KeyEvent event) {        if (!mCancelable) {            reset();            return true;        }        if (keyCode == KeyEvent.KEYCODE_BACK) { // 監控/攔截/屏蔽返回鍵            if (mFragmentView == null || mDialogView == null                    || mFLayout == null) {                return true;            }            dismissProgress();            return false;        }        return true;    }    private void reset() {        mDialogView = null;        mFragmentView = null;        mFLayout = null;    }}

3:用法

  

//展示ProgressView.getInstance().showProgressView(avtivityA,"資訊加中");//取消ProgressView.getInstance().dismissProgress();


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.