UI控件----ProgressBar进度条 实例总结

2023-11-11

ProgressBar提供了可以向用户展示当前任务的进度。
完成效果如下,单击增加/减少进度可以调节进度。
完成步骤一:layout的xml文件中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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="小圆形进度条" />

    <ProgressBar
        style="?android:attr/progressBarStyleSmallTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    </ProgressBar>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="中圆形进度条" >
    </TextView>

    <ProgressBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="大圆形进度条" >
    </TextView>

    <ProgressBar
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    </ProgressBar>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="水平进度条" >
    </TextView>

    <ProgressBar
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:max="100"
        android:progress="30" >
    </ProgressBar>

    <ProgressBar
        android:id="@+id/progressbar"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:max="100"
        android:progress="30"
        android:secondaryProgress="60" >
    </ProgressBar>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:text="增加进度" >
        </Button>

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:text="减小进度" >
        </Button>
    </LinearLayout>

</LinearLayout>

完成步骤二:mainactivity.java中完成代码:
package com.example.android_programbar;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ProgressBar;
import android.view.View.OnClickListener;


public class MainActivity extends Activity implements OnClickListener{

	private ProgressBar progressBar;
	private Button button1, button2;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//setContentView(R.layout.activity_main);
		
		//设置窗口有刻度
		requestWindowFeature(Window.FEATURE_PROGRESS);
		requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
		setContentView(R.layout.activity_main);
		progressBar = (ProgressBar) this.findViewById(R.id.progressbar);
		setProgressBarVisibility(true);
		setProgressBarIndeterminate(true);
		setProgress(3500);
		button1 = (Button) this.findViewById(R.id.button1);
		button2 = (Button) this.findViewById(R.id.button2);
		button1.setOnClickListener( this);
		button2.setOnClickListener( this);
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.button1:
			progressBar.setProgress((int) (progressBar.getProgress() * 1.2));
			progressBar.setSecondaryProgress((int) (progressBar
					.getSecondaryProgress() * 1.2));
			break;
		case R.id.button2:
			progressBar.setProgress((int) (progressBar.getProgress() * 0.6));
			progressBar.setSecondaryProgress((int) (progressBar
					.getSecondaryProgress() * 0.6));
			break;
		}
	}

}


本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

UI控件----ProgressBar进度条 实例总结 的相关文章

随机推荐