Android控件之ProgressBar


1,带有进度条的ProgressBar

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Request the progress bar to be shown in the title
        requestWindowFeature(Window.FEATURE_PROGRESS);
        setContentView(R.layout.progressbar_1);
        setProgressBarVisibility(true);//设置在title里的ProgressBar可见
        
        final ProgressBar progressHorizontal = (ProgressBar) findViewById(R.id.progress_horizontal);
        
        setProgress(progressHorizontal.getProgress() * 100);//为title中的ProgressBar设置进度
        setSecondaryProgress(progressHorizontal.getSecondaryProgress() * 100);//为title中的ProgressBar设置二级进度
        
        Button button = (Button) findViewById(R.id.increase);//一级进度递增
        button.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                progressHorizontal.incrementProgressBy(1);
                // Title progress is in range 0..10000
                setProgress(100 * progressHorizontal.getProgress());//为title中的ProgressBar设置进度
            }
        });

        button = (Button) findViewById(R.id.decrease);//一级进度递减
        button.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                progressHorizontal.incrementProgressBy(-1);
                // Title progress is in range 0..10000
                setProgress(100 * progressHorizontal.getProgress());//为title中的ProgressBar设置进度
            }
        });

        button = (Button) findViewById(R.id.increase_secondary);//二级进度递增
        button.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                progressHorizontal.incrementSecondaryProgressBy(1);
                // Title progress is in range 0..10000
                setSecondaryProgress(100 * progressHorizontal.getSecondaryProgress());
            }
        });

        button = (Button) findViewById(R.id.decrease_secondary);//二级进度递减
        button.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                progressHorizontal.incrementSecondaryProgressBy(-1);
                // Title progress is in range 0..10000
                setSecondaryProgress(100 * progressHorizontal.getSecondaryProgress());
            }
        });
        
    }

配置文件 :

    <ProgressBar android:id="@+id/progress_horizontal"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="200dip"
        android:layout_height="wrap_content"
        android:max="100"
        android:progress="50"
        android:secondaryProgress="75" />



效果图:

Android控件之ProgressBar_第1张图片


2, 转圈的样式的ProgressBar

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <ProgressBar
        android:id="@+android:id/progress_large"
        style="?android:attr/progressBarStyleLarge"//大样式
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <ProgressBar                                  //默认
        android:id="@+android:id/progress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <ProgressBar
        android:id="@+android:id/progress_small"   //小样式
        style="?android:attr/progressBarStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <ProgressBar                                   //小标题样式
        android:id="@+android:id/progress_small_title"
        style="?android:attr/progressBarStyleSmallTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>


Java代码:

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Request for the progress bar to be shown in the title
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        
        setContentView(R.layout.progressbar_2);
        
        // Make sure the progress bar is visible
        setProgressBarVisibility(true);
    }

效果:

Android控件之ProgressBar_第2张图片

3,Dialog样式的ProgressBar

 private static final int DIALOG1_KEY = 0;
    private static final int DIALOG2_KEY = 1;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.progressbar_3);

        Button button = (Button) findViewById(R.id.showIndeterminate);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                showDialog(DIALOG1_KEY);
            }
        });

        button = (Button) findViewById(R.id.showIndeterminateNoTitle);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                showDialog(DIALOG2_KEY);
            }
        });
    }

    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
            case DIALOG1_KEY: {
                ProgressDialog dialog = new ProgressDialog(this);
                dialog.setTitle("Indeterminate");
                dialog.setMessage("Please wait while loading...");
                dialog.setIndeterminate<span style="color:#ff0000;"></span>(true);//设置转圈的效果
                dialog.setCancelable(true);
                return dialog;
            }
            case DIALOG2_KEY: {
                ProgressDialog dialog = new ProgressDialog(this);
                dialog.setMessage("Please wait while loading...");
                dialog.setIndeterminate<span style="color:#ff0000;"></span>(true);//设置转圈的效果
                dialog.setCancelable(true);
                return dialog;
            }
        }
        return null;
    }

效果:

Android控件之ProgressBar_第3张图片Android控件之ProgressBar_第4张图片


4,在title上面的转圈的ProgressBar

    private boolean mToggleIndeterminate = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Request progress bar
        requestWindowFeature(Window.FEATURE_<span style="color:#ff0000;">INDETERMINATE</span>_PROGRESS);//转圈样的ProgressBar
        setContentView(R.layout.progressbar_4);
        setProgressBarIndeterminateVisibility(mToggleIndeterminate);//是否可见
        
        Button button = (Button) findViewById(R.id.toggle);
        button.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                mToggleIndeterminate = !mToggleIndeterminate;//如果是false,设成true,如果是true设成false.
                setProgressBarIndeterminateVisibility(mToggleIndeterminate);//是否可见
            }
        });
    }
Android控件之ProgressBar_第5张图片 Android控件之ProgressBar_第6张图片


欢迎转载:http://blog.csdn.net/johnny901114/article/details/7868808





你可能感兴趣的:(android,layout,null,dialog,button)