【Android开发学习27】界面控件之进度条(ProgressBar)

一、基础知识:

 

1.ProgressBar在界面文件XML中的布局:

<progressBar android:id="@+id/progressbar_updown"
        android:layout_width="200dp" 
        android:layout_height="wrap_content"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_gravity="center_vertical" 
        android:max="100"
        android:progress="50"
        android:secondaryProgress="70"    > 

 

style="?android:attr/progressBarStyleHorizontal"    设置风格为长形 
android:max="100"    最大进度值为100 
android:progress="50"   初始化的进度值 
android:secondaryProgress="70" 初始化的底层第二个进度值 
android:layout_gravity="center_vertical"    垂直居中

 


2.ProgressBar在代码文件(.java)中的控制使用:

private ProgressBar myProgressBar;
//定义ProgressBar

myProgressBar = (ProgressBar) findViewById(R.id.progressbar_updown);
//ProgressBar通过ID来从XML中获取

myProgressBar.incrementProgressBy(5);
//ProgressBar进度值增加5

myProgressBar.incrementProgressBy(-5);
//ProgressBar进度值减少5

myProgressBar.incrementSecondaryProgressBy(5);
//ProgressBar背后的第二个进度条 进度值增加5

myProgressBar.incrementSecondaryProgressBy(-5);
//ProgressBar背后的第二个进度条 进度值减少5

 

 

3.XML重要属性

android:progressBarStyle:默认进度条样式

android:progressBarStyleHorizontal:水平样式

 

 

4.重要方法

getMax():返回这个进度条的范围的上限

getProgress():返回进度

getSecondaryProgress():返回次要进度

incrementProgressBy(int diff):指定增加的进度

isIndeterminate():指示进度条是否在不确定模式下

setIndeterminate(boolean indeterminate):设置不确定模式下

setVisibility(int v):设置该进度条是否可视


 

 

 

 

二、代码展示:

1."Activity_09\src\yan\activity_09\MainActivity.java"

package yan.activity_09;

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

public class MainActivity extends Activity {
	// 声明变量
	private ProgressBar firstBar = null;
	private ProgressBar secondBar = null;
	private Button myButton = null;
	private int progress_vol = 0;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		//映射控件ID到变量
		firstBar = (ProgressBar)findViewById(R.id.firstBar);
		secondBar = (ProgressBar)findViewById(R.id.secondBar);
		myButton = (Button)findViewById(R.id.myButton);
		
		
		myButton.setOnClickListener(new ButtonListenr());
	}
	
	class ButtonListenr implements OnClickListener{

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			if(0 == progress_vol)
			{
				// 设置进度条的最大值
				firstBar.setMax(200);
				// 设置进度条为可见的状态
				firstBar.setVisibility(View.VISIBLE);
				secondBar.setVisibility(View.VISIBLE);
			}else if(progress_vol < firstBar.getMax()){
				// 设置主进度条的当前值
				firstBar.setProgress(progress_vol);
				// 设置第二进度条的当前值
				firstBar.setSecondaryProgress(progress_vol+10);
				// 默认的进度条是无法显示进行的状态的
				//secondBar.setProgress(progress_vol);
			}else{
				// 设置进度条为不可见的状态
				firstBar.setVisibility(View.GONE);
				secondBar.setVisibility(View.GONE);
			}
			progress_vol +=10;
		}
		
	}
}


 

2."Activity_09\res\layout\main.xml"

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:orientation="vertical"  
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent" 
	android:background="#00aaaa"  
    >  
   <TextView
		android:id="@+id/firstText"  
		android:text="@string/hello_world"  
		android:gravity="center_vertical"  
		android:textSize="15pt"  
		android:background="#aa0000"  
		android:layout_width="fill_parent"  
		android:layout_height="wrap_content"  
		android:singleLine="true"/>  
<ProgressBar
    android:id="@+id/firstBar"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:visibility="gone"
    />
<ProgressBar
    android:id="@+id/secondBar"
    style="?android:attr/progressBarStyle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:visibility="gone"
    />
<Button
    android:id="@+id/myButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="begin"
    />
</LinearLayout>  


3."Activity_09\res\values\strings.xml"

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">Activity_09</string>
    <string name="hello_world">Hello world!</string>
    <string name="menu_settings">Settings</string>

</resources>

 

 

 

 

 

三、效果展示:

 

 

 


 

本文完整代码下载地址: http://download.csdn.net/detail/ypist/5146876

 

拓展阅读文章:  花样Android ProgressBar 史上最强大讲解

Android学习笔记之progressBar(进度条) 

 

本文博客源地址:http://blog.csdn.net/ypist

 

 

你可能感兴趣的:(android,ProgressBar,setVisibility,setProgress)