标签:
1.简介
Progress为加载进度的进度条。
2.构建
构建如图所示的界面,当按+键按钮时进度条增加;当按-键按钮时进度条减少。
<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="example.progressbar.Activity1" >
<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="60dp"
android:progress="10" //设置进度条初始长度为10%
android:layout_alignParentStart="true"
android:layout_alignParentTop="true" />
<ProgressBar
android:id="@+id/progressBar2"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/progressBar1"
android:layout_centerHorizontal="true"
android:layout_marginTop="47dp" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/progressBar2"
android:layout_marginTop="72dp"
android:layout_toStartOf="@+id/progressBar2"
android:text="@string/bt1" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/button1"
android:layout_toEndOf="@+id/progressBar2"
android:text="@string/bt2" />
</RelativeLayout>
3.代码
public class Activity1 extends Activity implements OnClickListener{
Button b1,b2;
ProgressBar pb;
@Override
public void onClick(View v) {
//给按钮做监听,通过ID进行匹配
if(v.getId()==R.id.button1){
// getPorgress()获取初始进度条的长度,乘以2则每次增加一倍,再通过setProgress()设置进度条
pb.setProgress((int) (pb.getProgress()*2));
}
else{
pb.setProgress((int) (pb.getProgress()*0.6));
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act1);
b1=(Button) findViewById(R.id.button1);
b2=(Button) findViewById(R.id.button2);
pb=(ProgressBar) findViewById(R.id.progressBar1);
//设置监听器
b1.setOnClickListener(this);
b2.setOnClickListener(this);
}
4.效果
标签:
原文地址:http://www.cnblogs.com/jfl-xx/p/5187006.html