Android UI(ProgressBar)详解

本文详细探讨了Android中的ProgressBar组件,从其基本用法到高级定制,帮助开发者更好地理解和使用这一UI元素。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录:
    1.应用场景
    2.常用属性
    3.默认的进度条分类与设置style方法
    4.简单使用(进度的更新与进度值获取) 
    5.ProgressDialog中的进度条
    6.AlertDialog.Builder中的进度条
    7.页面标题下的进度条
    8.自定义ProgressBar(可参考我的前一篇关于SeekBar的自定义)
    
1.应用场景
    ProgressBar主要的应用场景在于对网络请求,数据加载显示时由于需要用户等待,如果没有提示有可能造成用户退出,或者误认为程序错误,增加ProgressBar的目的就是显式的
告诉用户正在加载数据,显示数据加载的进度是多少,以此来优化用户体验


2.常用属性
    //最大进度值,一般为100 
    android:max=""
    //进度条风格
    style=""
    //第一进度
    android:progress=""
    //第二进度,多用于视频缓存进度显示
    android:secondaryProgress=""
    //是否为明确进度
    android:indeterminate=""
    //强制indeterminate模式
    android:indeterminateOnly="true|false"
    //明确进度条显示的Drawable
    android:progressDrawable=""
    //不明确进度显示的Drawable
    android:indeterminateDrawable=""
    //不明确进度条模式的动画间隔时间
    android:indeterminateDuration=""
    //不明确进度模式下,达到最大时动画处理效果
    //repeat:从头开始 |cycle:反过来从后往前
    android:indeterminateBehavior="repeat|cycle"

3.默认的进度条分类与设置style方法 
    1)分类:
条形进度条:style="@android:style/Widget.ProgressBar.Horizontal"
常规圆形进度条:style="@android:style/Widget.ProgressBar"
反向旋转常规圆形进度条:style="@android:style/Widget.ProgressBar.Inverse"
大号圆形进度条:style="@android:style/Widget.ProgressBar.Large"
反向旋转大号圆形进度条:style="@android:style/Widget.ProgressBar.Large.Inverse"
小号圆形进度条:style="@android:style/Widget.ProgressBar.Small"
反向旋转小号圆形进度条:style="@android:style/Widget.ProgressBar.Small.Inverse"


注:其实还有其他风格的进度条,比如HOLO风格和Material风格(API要求21以上),感兴趣的伙伴可以搜搜


    2)设置style方法
(1)通过内置的attr属性设置:style="?android:attr/progressBarStyleHorizontal"  
(2)通过Widget.ProgressBar直接设置:Widget.ProgressBarstyle="@android:style/Widget.ProgressBar.Horizontal"
 
4.简单使用(进度的更新与进度值获取) 
    1)主布局文件
<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="com.andy.androiduiprogressbar.MainActivity" >
<Button 
    android:id="@+id/start"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="开始"
    />
    


<ProgressBar
    android:id="@+id/progress"
    style="@android:style/Widget.ProgressBar.Horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:max="100"
    android:progress="20"
    android:secondaryProgress="30" />
<TextView 
    android:id="@+id/value"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="value"/>




</LinearLayout>

   
    2)主布局Java类
package com.andy.androiduiprogressbar;


import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;


public class MainActivity extends Activity {
	private Button start;
	private ProgressBar progressBar;
	private TextView value;
	private AsyncTask<Integer, Integer, Integer> myAsyncTask;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		//初始化控件
		start =(Button) findViewById(R.id.start);
		progressBar =(ProgressBar) findViewById(R.id.progress);
		value=(TextView) findViewById(R.id.value);
		//内部类初始化AsyncTask并实现方法
		 myAsyncTask= new AsyncTask<Integer, Integer, Integer>() {
				


		   /*
		    * result:doInBackground的回传值
		   * 执行完后台线程后做的操作
		   */
			@Override
			protected void onPostExecute(Integer result) {
				Log.d("info", "myAsyncTask执行完成");
			}
			
			/*
			 * 更新进度条,和实时更新Text的进度值
			 */
			@Override
			protected void onProgressUpdate(Integer... values) {
				progressBar.setProgress(values[0]);
				value.setText("当前进度:"+values[0].toString());
			}


			/*
			 * 执行后台操作
			 */
			@Override
			protected Integer doInBackground(Integer... params) {
				for(int i=1;i<=100;i++){
					try {
						Thread.sleep(500);
						//执行该操作系统会调用onProgressUpdate方法
						publishProgress(i);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
				
				return 100;
			}
		};		
		
		//设置按钮监听
		start.setOnClickListener(new OnClickListener() {


			@Override
			public void onClick(View v) {
				//启动myAsyncTask线程
				myAsyncTask.execute();
			}});
		
	




}
}


    
    4)效果截图


    
5.ProgressDialog中的进度条
    1)主布局文件
<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="com.andy.androiduiprogressbar.MainActivity" >


<Button 
    android:id="@+id/dlgProgressBtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="弹出Progress"/>


</LinearLayout>
    
    2)主布局Java类
package com.andy.androiduiprogressbar;


import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;


public class SecondActivity extends Activity {
	private Button dlgProgressBtn;


	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_second);
		//初始化控件
		dlgProgressBtn =(Button) findViewById(R.id.dlgProgressBtn);
		//设置监听
		dlgProgressBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				ProgressDialog progressDialog =new ProgressDialog(SecondActivity.this);
				//设置图标
				progressDialog.setIcon(R.drawable.ic_launcher);
				//设置标题
				progressDialog.setTitle("弹框进度条标题");
				//设置提示信息
				progressDialog.setMessage("加载中...");
				//设置进度条类型ProgressDialog.STYLE_HORIZONTAL(长进度条)|ProgressDialog.STYLE_SPINNER(圆进度条)
				progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
				//设置是否可以按back键取消
				progressDialog.setCancelable(true);
				//设置是否可以触摸弹框外部取消
				progressDialog.setCanceledOnTouchOutside(true);
				//设置按钮DialogInterface.BUTTON_NEGATIVE(取消)|DialogInterface.BUTTON_POSITIVE(确认)|DialogInterface.BUTTON_NEUTRAL(普通)
				progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						Log.d("cancel", "进度条弹框取消");
					}
				});
				//设置按钮DialogInterface.BUTTON_POSITIVE(确认)
/*				progressDialog.setButton(DialogInterface.BUTTON_POSITIVE, "确认", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						Log.d("cancel", "点击确认进度条");
					}
				});*/
				//显示progressDialog
				progressDialog.show();
				
			}
		});


		
}
}

    3)效果截图    




6.AlertDialog.Builder中的进度条   

    1)弹框布局custom_dialog_layout.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="com.andy.androiduiprogressbar.ThreeActivity" >


<ProgressBar
    android:id="@+id/progress"
    style="@android:style/Widget.ProgressBar.Horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:max="100"
    android:progress="20"
    android:secondaryProgress="30" />


</LinearLayout>
    

    2)主布局activity_three.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="com.andy.androiduiprogressbar.ThreeActivity" >


<Button 
    android:id="@+id/alertDialogbtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="alertDialogbtn"/>


</LinearLayout>
   
    3)主布局Java类
package com.andy.androiduiprogressbar;


import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.Toast;


public class ThreeActivity extends Activity {
	private Button alertDialogbtn;
	private ProgressBar progress;
	//可理解为AlertDialog构建者
	private AlertDialog.Builder builder;
	//AlertDialog实例化
	private AlertDialog progressDialog;
	//布局加载器
	private LayoutInflater minflater;
	//布局
	private LinearLayout mLayout;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_three);
		//初始化
		//minflater=getLayoutInflater();
		minflater=(LayoutInflater) getSystemService(this.LAYOUT_INFLATER_SERVICE); 
		mLayout = (LinearLayout) minflater.inflate(R.layout.custom_dialog_layout, null);		
		progress = (ProgressBar)mLayout.findViewById(R.id.progress);
		alertDialogbtn =(Button) findViewById(R.id.alertDialogbtn);
		//alertDialogbtn设置监听
		alertDialogbtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				//显示progressDialog
				progressDialog.show();
			}
		});
		//初始化构建者
		builder = new AlertDialog.Builder(ThreeActivity.this);
		//用AlertDialog构建者,构建一个progressDialog
		progressDialog = builder.create();
		//设置progressDialog图标
		Log.d("dialog", progressDialog.toString());
		progressDialog.setIcon(R.drawable.ic_launcher);
		//设置标题
		progressDialog.setTitle("progressDialog标题");
		//设置提示信息
		progressDialog.setMessage("正在加载中...");
		//设置自定义的layout布局
		progressDialog.setView(mLayout);
		//设置back键取消
		progressDialog.setCancelable(true);
		//设置点击弹框外部取消
		progressDialog.setCanceledOnTouchOutside(true);
		//设置确认按钮,除可以设置DialogInterface.BUTTON_POSITIVE(确认)以外还可以设置RESULT_OK
		progressDialog.setButton(RESULT_OK, "确认", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
						Toast.makeText(ThreeActivity.this, "点击了确认", Toast.LENGTH_SHORT).show();
			}
		});
		//设置按钮DialogInterface.BUTTON_NEGATIVE(取消)|DialogInterface.BUTTON_POSITIVE(确认)|DialogInterface.BUTTON_NEUTRAL(普通)
		progressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				Toast.makeText(ThreeActivity.this, "进度条弹框取消", Toast.LENGTH_SHORT).show();
			}
		});		
	
					
}
}

        4)效果截图
        
        
7.页面标题下的进度条
    package com.andy.androiduiprogressbar;


    import android.app.Activity;
    import android.os.Bundle;
    import android.view.Window;


    public class FourActivity extends Activity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            //请求窗口属性中的FEATURE_PROGRESS属性
            requestWindowFeature(Window.FEATURE_PROGRESS);
            //设置进度条可见
            setProgressBarVisibility(true);
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_four);
            //设置第一进度
            setProgress(30);
            //设置是否为不明确进度条样式
            setProgressBarIndeterminate(false);
            //设置第二进度
            setSecondaryProgress(40);
        
        }
    }


 

推荐: http://blog.youkuaiyun.com/mad1989/article/details/38042875

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值