当前位置: 代码迷 >> 综合 >> ProgressBar --MarsChen Android 开发教程学习笔记
  详细解决方案

ProgressBar --MarsChen Android 开发教程学习笔记

热度:75   发布时间:2023-12-17 02:35:45.0
ProgressBar就是进度条,很常见的组件,Android 中的进度条十分丰富,也十分方便。

了解Android 当中不同的进度条
安卓进度条可以分为四个种类:圆圈、传统进度条、可拖拽进度条和星形进度条。圆圈和传统进度条主要用于程序运行中的表示,可拖拽进度条可以用在可调节进度的应用当中,而星形进度条通常在评分界面可以看见。

各种进度条的关系
所有进度条都是ProgressBar 类,圆圈和传统进度条都是从ProgressBar 得来的,圆圈进度条是垂直风格,传统进度条是水平风格。可拖拽进度条对应SeekBar ,星形进度条对应RatingBar 。
SeekBar 和RatingBar 是ProgressBar 的子类。

ProgressBar 进度条的风格
主要分为两大类:垂直风格和水平风格,皆可通过XML中的style 命令控制。
垂直风格又分为小风格(Small)、大风格(Large)、反向风格(Inverse ,圆圈的颜色相对较深,而不是反向旋转)、小反向风格(Small.Inverse)和大反向风格(Large.Inverse)。

ProgressBar 的使用方法
在XML文件中添加<ProgressBar />组件,通过style 控制进度条的风格。
在水平风格下,还可以在XML文件中利用progress命令设置进度条的属性,利用secondaryprogress命令设置进度条的第二进度。而在JAVA 文件中可以分别通过setMax(int i) 方法设置进度条最大值、setProgress(int i) 方法设置当前进度、setSecondaryProgress(int i) 方法设置当前的第二进度。
在JAVA文件下,调用返回boolean 值的isIndeterminate() 方法判断是否为不知明确进度的垂直进度条。调用incrementProgressBy(int i) 方法增加当前进度的值,调用incrementSecondaryProgressBy(int i) 方法增加第二进度的值。
在XML 文件中的代码:

<RelativeLayout 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: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="com.shumin.lbs05.MainActivity" >

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

<ProgressBar
android:id="@+id/thesecondbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/thefirstbar"
style="@android:attr/progressBarStyleLarge"/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/thesecondbar"
android:text="欧耶!"/>
</RelativeLayout>



在JAVA 文件中的代码:

package com.shumin.lbs05;

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


public class MainActivity extends Activity {

private ProgressBar thefirstbar;
private ProgressBar thesecondbar;
private Button button;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
thefirstbar=(ProgressBar)findViewById(R.id.thefirstbar);
thesecondbar=(ProgressBar)findViewById(R.id.thesecondbar);
button=(Button)findViewById(R.id.button);
thefirstbar.setMax(100);
thefirstbar.setProgress(5);
thefirstbar.setSecondaryProgress(10);
buttonListener listener=new buttonListener();
button.setOnClickListener(listener);

}

class buttonListener implements OnClickListener{

@Override
public void onClick(View v) {
thefirstbar.incrementProgressBy(5);
thefirstbar.incrementSecondaryProgressBy(5);
}

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}




  相关解决方案