xml布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="默认样式"
/>
<!--
ProgressBar进度条控件,默认样式为圆形进度条(进度不确定)
style:进度条样式
max:最大进度值
progress:当前进度值
secondaryProgress:次要进度值
-->
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="超大号圆形样式"
/>
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyleLarge"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="小号圆形样式"
/>
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyleSmall"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="水平方向长条形样式"
/>
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyleHorizontal"
android:max="100"
android:progress="30"
android:secondaryProgress="50"
/>
<Button
android:id="@+id/button_IncreaseProgress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="增加第一进度"
/>
<Button
android:id="@+id/button_IncreaseSecondaryProgress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="增加第二进度"
/>
</LinearLayout>
xml文件效果
Java代码
package com.example.ui_progressbar;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
public class MainActivity extends Activity
{
private ProgressBar progressBar;
private Button button_IncreaseProgress,button_IncreaseSecondaryProgress;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.linearlayout);
initView();
progressBar.setMax(100);
progressBar.setProgress(30);
progressBar.setSecondaryProgress(50);
ButtonListener buttonListener=new ButtonListener();
button_IncreaseProgress.setOnClickListener(buttonListener);
button_IncreaseSecondaryProgress.setOnClickListener(buttonListener);
}
private void initView()
{
progressBar=(ProgressBar)findViewById(R.id.progressBar);
button_IncreaseProgress=(Button)findViewById(R.id.button_IncreaseProgress);
button_IncreaseSecondaryProgress=(Button)findViewById(R.id.button_IncreaseSecondaryProgress);
}
class ButtonListener implements OnClickListener
{
@Override
public void onClick(View arg0)
{
switch(arg0.getId())
{
case R.id.button_IncreaseProgress:
{
//incrementProgressBy(int number):当前进度条增加number数值
progressBar.incrementProgressBy(20);
break;
}
case R.id.button_IncreaseSecondaryProgress:
{
//incrementSecondaryProgressBy(int number):次要进度条增加number数值
progressBar.incrementSecondaryProgressBy(40);
break;
}
}
}
}
}
本文介绍 Android 中 ProgressBar 控件的使用方法,包括不同样式展示及通过 Java 代码动态控制进度的实现方式。
1164

被折叠的 条评论
为什么被折叠?



