美文网首页
Android ProgressBar进度条

Android ProgressBar进度条

作者: 百里漫步 | 来源:发表于2017-10-19 11:10 被阅读0次

1、ProgressBar的显示风格
大的环形progressBar: style="?android:attr/progressBarStyleLarge"
中的环形progressBar: style默认
小的环形progressBar: style="?android:attr/progressBarStyleSmall"
水平progressBar: style="?android:attr/progressBarStyleHorizontal"
这里用 style="@android:attr/progressBarStyleHorizontal"结果显示的还是圈,所以改用了 style="@android:style/Widget.DeviceDefault.ProgressBar.Horizontal",小白不懂原理~~

 <ProgressBar
        style="@android:attr/progressBarStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:indeterminate="false"
        android:progress="50"
        android:secondaryProgress="80" />

    <ProgressBar
        style="@android:style/Widget.DeviceDefault.ProgressBar.Horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:indeterminate="false"
        android:max="100"
        android:progress="50"
        android:secondaryProgress="80" />
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //启用窗口特征
        requestWindowFeature(Window.FEATURE_PROGRESS);
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        setContentView(R.layout.main);
        //显示进度条
        setProgressBarVisibility(true);
        setProgressBarIndeterminateVisibility(true);
        //设置进度,总值10000
        setProgress(9999);
        

    }

效果:

progress.gif

2、自定义进度条样式:
progress_bar.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item android:id="@android:id/background">
        <shape>
            <corners android:radius="5dip" />

        <solid android:color="#88000000"/>
        </shape>
    </item>
    <item android:id="@android:id/secondaryProgress">
        <clip>
            <shape>
                <corners android:radius="5dip" />

                <gradient
                    android:angle="270"
                    android:centerColor="#C6B7FF"
                    android:centerY="0.75"
                    android:endColor="#C3B2FF"
                    android:startColor="#B9A4FF" />
            </shape>
        </clip>
    </item>
    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <corners android:radius="5dip" />

            <gradient
                android:angle="270"
                android:centerColor="#74EBFF"
                android:centerY="0.75"
                android:endColor="#8EEFFF"
                android:startColor="#57E8FF" />
            </shape>
        </clip>
    </item>

</layer-list>

main.xml

 <ProgressBar
        style="@android:style/Widget.DeviceDefault.ProgressBar.Horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:indeterminate="false"
        android:max="100"
        android:progress="50"
        android:progressDrawable="@drawable/progress_bar"
        android:secondaryProgress="80" />

效果:

Paste_Image.png

对话框形式的进度条:
MainActivity.java

package com.example.day101902;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {

    private ProgressDialog progressDialog;
    private Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //启用窗口特征
        requestWindowFeature(Window.FEATURE_PROGRESS);
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        setContentView(R.layout.main);
        //显示进度条
        setProgressBarVisibility(true);
        setProgressBarIndeterminateVisibility(true);
        //设置进度,总值10000
        setProgress(9999);
        

        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new OnClickListener() {
            
            @Override
            public void onClick(View arg0) {
                //初始化
                progressDialog = new ProgressDialog(MainActivity.this);
                //描述
                progressDialog.setMessage("显示进度");
                //标题
                progressDialog.setTitle("进度条");
                //样式
                progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                //最大进度
                progressDialog.setMax(100);
                //已经达到的进度
                progressDialog.incrementProgressBy(50);;
                //图标
                progressDialog.setIcon(R.drawable.ic_launcher);
                //明确显示进度
                progressDialog.setIndeterminate(false);
                //设置按钮
                progressDialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
                    
                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {
                        Toast.makeText(MainActivity.this, "返回页面", Toast.LENGTH_SHORT).show();
                        
                    }
                });
                //设置取消
                progressDialog.setCancelable(true);
                //显示progressDialog
                progressDialog.show();
            }
        });
        
    }
}

效果:


pb.gif

相关文章

网友评论

      本文标题:Android ProgressBar进度条

      本文链接:https://www.haomeiwen.com/subject/mtquuxtx.html