一、ProgressBar
1. 常用類(lèi)型
1.1 不確定式圓形進(jìn)度條
style="@android:style/Widget.Holo.Light.ProgressBar"style="@android:style/Widget.DeviceDefault.Light.ProgressBar.Large"...
沒(méi)有顯示進(jìn)度,可作為過(guò)場(chǎng)動(dòng)畫(huà)。有大、中、小三種大小,默認(rèn)為中。
1.2 條形進(jìn)度條
style="@android:style/Widget.ProgressBar.Horizontal"style="@android:style/Widget.DeviceDefault.Light.ProgressBar.Horizontal"...
帶有顯示進(jìn)度。
1.3 標(biāo)題欄不確定式進(jìn)度條
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);setProgressBarIndeterminateVisibility(true);
在標(biāo)題欄右側(cè)顯示的無(wú)顯示進(jìn)度的圓形進(jìn)度條。
1.4 標(biāo)題欄條形進(jìn)度條
requestWindowFeature(Window.FEATURE_PROGRESS);setProgressBarVisibility(true);
在標(biāo)題欄頂部顯示的條形進(jìn)度條,可通過(guò)setProgess(int)設(shè)置當(dāng)前進(jìn)度,最大值為10000。
2. 常用控件屬性
<!--最大顯示進(jìn)度-->android:max<!--第一顯示進(jìn)度-->android:progress<!--第二顯示進(jìn)度-->android:secondaryProgress<!--置是否精確顯示;true為不精確,false為精確-->android:indeterminate<!--加載自定義樣式-->android:progressDrawable
3. 自定義樣式
通過(guò)控件的android:progressDrawable屬性引用自定義的drawable文件實(shí)現(xiàn)。一般需定義三個(gè)內(nèi)容:背景、第一進(jìn)度、第二進(jìn)度。
范例:
<?xml version="1.0" encoding="utf-8"?><layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <!--背景樣式--> <item android:id="@android:id/background"> <shape> <!--圓角--> <corners android:radius="10dip" /> <!--填充色--> <solid android:color="#dddddd" /> </shape> </item> <!--第二進(jìn)度樣式--> <item android:id="@android:id/secondaryProgress"> <clip> <shape> <corners android:radius="10dip" /> <solid android:color="#78bb78" /> </shape> </clip> </item> <!--第一進(jìn)度樣式--> <item android:id="@android:id/progress"> <clip> <shape> <corners android:radius="10dip" /> <solid android:color="#55bb55" /> </shape> </clip> </item></layer-list>
貼張效果圖:

4. 關(guān)鍵方法
//設(shè)置第一進(jìn)度setProgress(int)//設(shè)置第二進(jìn)度setSecondaryProgress(int)//獲取第一進(jìn)度getProgress()//獲取第二進(jìn)度getSecondaryProgress()//增加或減少第一進(jìn)度incrementProgressBy(int)//增加或減少第二進(jìn)度incrementSecondaryProgressBy(int)//獲取進(jìn)度最大值getMax()
5. 范例
布局比較簡(jiǎn)單,線性布局,豎直排列,這里就不貼代碼了,直接貼張圖:

Java:
public class ProgessBarActivity extends Activity implements View.OnClickListener{ private ProgressBar progressBar; private TextView text; private Button addFirst; private Button addSecond; private Button subFirst; private Button subSecond; private Button reset; private int first; private int second; private int max; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_progess_bar); init(); } private void init() { progressBar = (ProgressBar) findViewById(R.id.progress_bar); text = (TextView) findViewById(R.id.text); addFirst = (Button) findViewById(R.id.add_first); subFirst = (Button) findViewById(R.id.sub_first); addSecond = (Button) findViewById(R.id.add_second); subSecond = (Button) findViewById(R.id.sub_second); reset = (Button) findViewById(R.id.reset); //獲取第一、第二、最大進(jìn)度 first = progressBar.getProgress(); second = progressBar.getSecondaryProgress(); max = progressBar.getMax(); addFirst.setOnClickListener(this); addSecond.setOnClickListener(this); subFirst.setOnClickListener(this); subSecond.setOnClickListener(this); reset.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.add_first: //第一進(jìn)度加10 progressBar.incrementProgressBy(10); break; case R.id.add_second: //第二進(jìn)度加10 progressBar.incrementSecondaryProgressBy(10); break; case R.id.sub_first: progressBar.incrementProgressBy(-10); break; case R.id.sub_second: progressBar.incrementSecondaryProgressBy(-10); break; case R.id.reset: //重置為初始數(shù)值 progressBar.setProgress(30); progressBar.setSecondaryProgress(60); break; } //更新文本內(nèi)容 text.setText("第一進(jìn)度為" + (int) (1.0*first/max*100) + "%,第二進(jìn)度為" + (int) (1.0*second/max*100) + "%"); }}二、ProgressDialog
1. 構(gòu)造函數(shù)
ProgressDialog(Context context)ProgressDialog(Context context, int theme)//theme為對(duì)話框樣式
2. 關(guān)鍵方法
//設(shè)置進(jìn)度條樣式setProgressStyle(int style)//設(shè)置對(duì)話框標(biāo)題setTitle(String title)//設(shè)置對(duì)話框本文信息setMessage(CharSequence message)//設(shè)置對(duì)話框圖標(biāo)setIcon(Drawable d)//設(shè)置按鈕,whichButton為按鈕類(lèi)型,text為按鈕名稱(chēng),listener為監(jiān)聽(tīng)器setButton(int whichButton, CharSequence text, OnClickListener listener)//顯示對(duì)話框show()
此外,除了這幾個(gè)方法,ProgressDialog也可使用上面ProgressBar中介紹的方法。
3. 范例
public class ProgressDialogActivity extends Activity { private ProgressDialog proDialog; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_progress_dialog); findViewById(R.id.show).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //新建對(duì)話框 proDialog = new ProgressDialog(ProgressDialogActivity.this); //設(shè)置進(jìn)度條樣式 proDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); //設(shè)置對(duì)話框標(biāo)題 proDialog.setTitle("初識(shí)ProgressDialog"); //設(shè)置提示對(duì)話框文本 proDialog.setMessage("好好學(xué)習(xí),天天向上!"); //設(shè)置對(duì)話框顯示圖標(biāo) proDialog.setIcon(R.drawable.ic_launcher); //設(shè)置進(jìn)度條最大進(jìn)度,默認(rèn)為10000 proDialog.setMax(100); //設(shè)置初始第一進(jìn)度 proDialog.incrementProgressBy(30); //設(shè)定取消按鈕 proDialog.setButton(DialogInterface.BUTTON_POSITIVE, "取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } }); //顯示對(duì)話框 proDialog.show(); } }); }}以上這篇ProgressBar、ProgessDialog-用法(詳解)就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持武林網(wǎng)。
新聞熱點(diǎn)
疑難解答
圖片精選