activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" 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.example.progressbardemo.MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="小圆形进度条" /> <ProgressBar style="@android:attr/progressBarStyleSmallTitle" 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" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="大型圆形进度条" /> <ProgressBar style="@android:style/Widget.ProgressBar.Large" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="水平进度条" /> <ProgressBar style="@android:style/Widget.ProgressBar.Horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:max="100" android:progress="30" /> <ProgressBar android:id="@+id/bar" style="@android:style/Widget.ProgressBar.Horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:max="100" android:progress="30" android:secondaryProgress="60" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="horizontal" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:text="增加进度" /> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:text="减少进度" /> </LinearLayout> </LinearLayout>
MainA.java:
import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.view.Window; import android.widget.Button; import android.widget.ProgressBar; public class MainActivity extends Activity implements OnClickListener { private ProgressBar progressBar; private Button button, button2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 如何设置窗口有刻度的效果 requestWindowFeature(Window.FEATURE_PROGRESS); requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); setContentView(R.layout.activity_main); progressBar = (ProgressBar) findViewById(R.id.bar); setProgressBarVisibility(true); setProgressBarIndeterminate(true); setProgress(3500); button = (Button) findViewById(R.id.button1); button2 = (Button) findViewById(R.id.button2); button.setOnClickListener(this); button2.setOnClickListener(this); } @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.button1: progressBar.setProgress((int) (progressBar.getProgress() * 1.2)); progressBar.setSecondaryProgress((int) (progressBar .getSecondaryProgress() * 1.2)); break; case R.id.button2: progressBar.setProgress((int) (progressBar.getProgress() * 0.8)); progressBar.setSecondaryProgress((int) (progressBar .getSecondaryProgress() * 0.8)); break; default: break; } } }
时间: 2024-10-09 20:12:16