安卓学习-界面-ui-ProcessBar
ProcessBar
属性 | 方法 | 说明 |
android:max | setMax(int max) | 最大值 |
android:progress | setProgress(int progress) | 进度 |
android:progressDrawable | setProgressDrawable(Drawable d) | 轨道图片 |
android:indeterminate | setIndeterminate(boolean indeterminate) | 进度是否确定,常用在不确定进度,但是有需要进度效果的 |
android:indeterminateDrawable | setIndeterminateDrawable(Drawable d) | 不确定进度的轨道图片 |
android:indeterminateDuration |
不确定进度的持续时间 设置了没效果 |
例子1
没1秒,增加1,最大100
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/linearLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:padding="10dp" > <ProgressBar android:id="@+id/ProgressBar1" style="?android:attr/progressBarStyleHorizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:max="100" /> </LinearLayout>
MainActivity.java
public class MainActivity extends Activity{ ProgressBar progressBar1; int proc=0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); progressBar1=(ProgressBar)findViewById(R.id.ProgressBar1); new Thread(){ @Override public void run() { while(proc<100){ proc=proc+1; try { sleep(1000); handler.sendEmptyMessage(123); } catch (InterruptedException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } } } }.start(); } Handler handler=new Handler(){ @Override public void handleMessage(Message msg) { if(msg.what==123){ progressBar1.setProgress(proc); } } }; }
例子2
显示在标题上的进度条
需要说明的是标题的进度条是从0-10000的,而且最大值是不能设置的
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/linearLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" android:padding="10dp" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="显示" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="隐藏" /> </LinearLayout>
MainActivity.java
public class MainActivity extends Activity{ Button btn1; Button btn2; int proc=0; Thread thread; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //带进度条的 requestWindowFeature(Window.FEATURE_PROGRESS); //不带 requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); setContentView(R.layout.activity_main); btn1=(Button)findViewById(R.id.button1); btn2=(Button)findViewById(R.id.button2); thread=new Thread(){ @Override public void run() { while(proc<10000){ proc=proc+1000; try { sleep(1000); handler.sendEmptyMessage(123); } catch (InterruptedException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } } } }; btn1.setOnClickListener(new OnClickListener() { public void onClick(View v) { setProgressBarIndeterminateVisibility(true); setProgressBarVisibility(true); thread.start(); } }); btn2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { setProgressBarIndeterminateVisibility(false); setProgressBarVisibility(false); } }); } Handler handler=new Handler(){ @Override public void handleMessage(Message msg) { if(msg.what==123){ MainActivity.this.setProgress(proc); } } }; }