/* max -- 最大進度 progress -- 第一進度 secondaryProgress -- 第二進度 style -- 樣式,大中小水平 */ <ProgressBar android:max="100" android:progress="50" android:secondaryProgress="80" style="?android:attr/progressBarStyleHorizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/progressBar" android:layout_gravity="center_horizontal" /> add_btn = (Button)findViewById(R.id.add_button); reducer_btn = (Button)findViewById(R.id.reduce_button); reset_btn = (Button)findViewById(R.id.reset_button); protext = (TextView)findViewById(R.id.protext); progressBar = (ProgressBar)findViewById(R.id.progressBar); show_btn = (Button)findViewById(R.id.dialog_button); show_btn.setOnClickListener(this); add_btn.setOnClickListener(this); reducer_btn.setOnClickListener(this); reset_btn.setOnClickListener(this); myProtext(); private void myProtext(){ int first = progressBar.getProgress(); int second = progressBar.getSecondaryProgress(); int max = progressBar.getMax(); protext.setText((int)(first/(float)max*100)+"%"+"=="+(int)(second/(float)max*100)+"%"); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.add_button:{//增長 progressBar.incrementProgressBy(10); progressBar.incrementSecondaryProgressBy(10); break; } case R.id.reduce_button:{//減小 progressBar.incrementProgressBy(-10); progressBar.incrementSecondaryProgressBy(-10); break; } case R.id.reset_button:{//充值 progressBar.setProgress(50); progressBar.setSecondaryProgress(80); break; } case R.id.dialog_button:{//彈出進度彈出框 //初始化 progressDiaog = new ProgressDialog(FourActivity.this); //顯示風格 progressDiaog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); //標題 progressDiaog.setTitle("測試"); //對話框內的文字信息 progressDiaog.setMessage("歡迎下載"); //圖標 progressDiaog.setIcon(R.drawable.icon); //最大進度 progressDiaog.setMax(100); //開始進度 progressDiaog.incrementProgressBy(50); //明確顯示進度精度 progressDiaog.setIndeterminate(false); //設定按鈕 progressDiaog.setButton(DialogInterface.BUTTON_POSITIVE,"肯定", new DialogInterface.OnClickListener(){ @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(FourActivity.this,"下載結束",Toast.LENGTH_SHORT).show(); } }); //是否能夠經過返回按鈕退出對話框 progressDiaog.setCancelable(true); //顯示 progressDiaog.show(); break; } } myProtext(); }
自定義樣式android
/* max -- 最大進度 progress -- 第一進度 secondaryProgress -- 第二進度 style -- 樣式,大中小水平 默認樣式 -- style="?android:attr/progressBarStyleHorizontal" 自定義樣式 1.先將style修改成Widget.ProgressBar.Horizontal 2.覆蓋其progressDrawable屬性,路徑爲自定義xml */ <ProgressBar android:max="100" android:progress="50" android:secondaryProgress="80" style="@android:style/Widget.ProgressBar.Horizontal" android:progressDrawable="@drawable/progress_bar" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/progressBar" android:layout_gravity="center_horizontal" /> progress_bar.xml <?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="5dip" /> <solid android:color="#88000000"/> </shape> </item> <item android:id="@android:id/secondaryProgress"> <clip> <shape> <corners android:radius="5dip" /> <gradient android:angle="270" android:centerColor="#C6B7FF" android:centerY="0.75" android:endColor="#C3B2FF" android:startColor="#B9A4FF" /> </shape> </clip> </item> <item android:id="@android:id/progress"> <clip> <shape> <corners android:radius="5dip" /> <gradient android:angle="270" android:centerColor="#74EBFF" android:centerY="0.75" android:endColor="#8EEFFF" android:startColor="#57E8FF" /> </shape> </clip> </item> </layer-list>