android ProgressBar简单介绍及实例

public class MainActivity extends Activity {

	private int[] mData = new int[100];
	private int mHasData;
	private int mStatus;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		final ProgressBar bar1 = (ProgressBar) findViewById(R.id.bar1);
		final ProgressBar bar2 = (ProgressBar) findViewById(R.id.bar2);

		final Handler handler = new Handler() {

			@Override
			public void handleMessage(Message msg) {
				super.handleMessage(msg);

				if (msg.what == 0x1222) {
					bar1.setProgress(mStatus);
					bar2.setProgress(mStatus);
				}
			}

		};

		new Thread() {
			@Override
			public void run() {
				while (mStatus < 100) {
					mStatus = doWork();
					Message msg = new Message();
					msg.what = 0x1222;
					handler.sendMessage(msg);
				}
			}

		}.start();
	}

	private int doWork() {
		mData[mHasData++] = (int) (Math.random() * 100);
		try {
			Thread.sleep(100);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

		return mHasData;
	}
}

activity_main.xml文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="${relativePackage}.${activityClass}" >

    <ProgressBar
        android:id="@+id/bar1"
        style="@android:style/Widget.ProgressBar.Horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:max="100" />

    <ProgressBar
        android:id="@+id/bar2"
        style="@android:style/Widget.ProgressBar.Horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:max="100"
        android:progressDrawable="@drawable/my_bar" />

</LinearLayout>

mybar.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"
        android:drawable="@drawable/no"/>
    
    <item android:id="@android:id/progress"
        android:drawable="@drawable/ok"/>

</layer-list>

再来说几种常见的ProgressBar的样式:

总体分两大类:

一、可以精确显示进度的

style="?android:attr/progressBarStyleHorizontal" // 显示的进度条是细的
<pre name="code" class="html">style="@android:style/Widget.ProgressBar.Horizontal" // 显示的进度条是粗的

二、不可以精确显示进度的

style="?android:attr/progressBarStyleLarge"
style="?android:attr/progressBarStyleSmall" 
不设置style属性时 ,progressBar是环形的进度条即”不可以精确显示进度的“

ProgressBar的关键属性


android:max = "100" -----最大显示进度
android:progress = "50" -----第一显示进度
android:secondaryProgress = "80" -----第二显示进度
android:indeterminate = "true" -----设置是否精确显示(true:不精确,false:精确)

发布了97 篇原创文章 · 获赞 4 · 访问量 8万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览