制定ProgressBar显示风格

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:context=".MainActivity"
    android:orientation="vertical">

<!--
类型:
style不写默认环形进度条
style="?android:attr/progressBarStyleLarge" 大环形进度条
style="?android:attr/progressBarStyleSmall" 大环形进度条
style="?android:attr/progressBarStyleHorizontal"  水平进度条



-->
    <ProgressBar
        android:id="@+id/progressBar1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

    <ProgressBar

        android:id="@+id/progressBar2"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1" />
    <ProgressBar

        android:id="@+id/progressBar3"
        style="?android:attr/progressBarStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

    <ProgressBar
        android:id="@+id/progressBar4"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" />
</LinearLayout>

ProgressBar的分类

带具体进度进度条和不带进度

标题上ProgressBar的设置

package com.example.progressbar;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.Window;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //启动窗口特征,启用带进度和不带进度进度条
        requestWindowFeature(Window.FEATURE_PROGRESS);
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        setContentView(R.layout.activity_main);

        //显示两种进度条true代表显示,当数据满时可以设置false
        setProgressBarVisibility(true);
        setProgressBarIndeterminateVisibility(true);
        //设置刻度,默认最大刻度Max = 10000
        setProgress(600);
        //以上设置进度条安卓app标题框上
        

    }
}

ProgressBar的关键属性

第一进度条:实际播放视频进度条

第二进度条:视频缓冲进度

最大显示进度条:视频实际总时长进度

PregressBar的关键方法

ProgressDialog的基础使用

案例:设置四个按钮,增加,减少,重置分别改变进度条,和显示进度条对话框

package com.example.progressbar;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private ProgressBar progressBar;
    private Button add;
    private Button reduce;
    private Button reset;
    private TextView textView;
    private Button show;
    private ProgressDialog prodialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //启动窗口特征,启用带进度和不带进度的进度条
        requestWindowFeature(Window.FEATURE_PROGRESS);
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        setContentView(R.layout.activity_main);

        //显示两种进度条,true代表显示,当数据满时可以设置为false
        setProgressBarVisibility(true);
        setProgressBarIndeterminateVisibility(false);  //环形
        //设置刻度,默认最大刻度Max = 10000
        setProgress(600);
        //以上设置的进度条在安卓app标题框上
        init();
        //获取第一进度条的进度
        int first = progressBar.getProgress();

        int second = progressBar.getSecondaryProgress();

        int max = progressBar.getMax();
        textView.setText("第一进度百分比: "+(int)(first/(float)max*100)+"% 第二进度百分比: " +
                ""+(int)(second/(float)max*100)+"%");
        add.setOnClickListener(this);
        reduce.setOnClickListener(this);
        reset.setOnClickListener(this);
        show.setOnClickListener(this);



    }
    private void init(){
        progressBar = findViewById(R.id.horiz);
        add = findViewById(R.id.add);
        reduce = findViewById(R.id.reduce);
        reset = findViewById(R.id.reset);
        textView = findViewById(R.id.textView);
        show = findViewById(R.id.show);


    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.add:
                //增加第一进度与第二进度10个刻度
                progressBar.incrementProgressBy(10);
                progressBar.incrementSecondaryProgressBy(10);
                break;

            case R.id.reduce:
                //减少第一进度与第二进度10个刻度
                progressBar.incrementProgressBy(-10);
                progressBar.incrementSecondaryProgressBy(-10);

                break;

            case R.id.reset:
                progressBar.setProgress(50);
                progressBar.setSecondaryProgress(80);

                break;
            case R.id.show:
                prodialog = new ProgressDialog(MainActivity.this);
                //设置显示风格(横向)
                prodialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                //设置标题
                prodialog.setTitle("标题");
                prodialog.setMessage("欢迎");
                //设置图标
                prodialog.setIcon(R.mipmap.ic_launcher);

                //设置关于ProgressBar的属性
                prodialog.setMax(100);
                //初始化已经增长到的进度
                prodialog.incrementProgressBy(50);
                //设置明确显示进度用false
                prodialog.setIndeterminate(false);

                //设定一个确定按钮
                prodialog.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(MainActivity.this,"欢迎",Toast.LENGTH_SHORT).show();
                    }
                });
                //是否通过返回按钮退出对话
                prodialog.setCancelable(true);

                //显示
                prodialog.show();

                break;

        }
        //由于不论进入哪种case最后都会刷新一次textview
        //所以直接在这里部署
        textView.setText("第一进度百分比: "
                +(int)(progressBar.getProgress()/(float)progressBar.getMax()*100)+"% 第二进度百分比: "
                +(int)(progressBar.getSecondaryProgress()/(float)progressBar.getMax()*100)+"%");
    }

}

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">

<!--
类型:
style不写默认中环形进度条
style="?android:attr/progressBarStyleLarge" 大环形进度条
style="?android:attr/progressBarStyleSmall" 大环形进度条
style="?android:attr/progressBarStyleHorizontal"  水平进度条



-->
    <ProgressBar
        android:id="@+id/progressBar1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

    <ProgressBar

        android:id="@+id/progressBar2"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1" />
    <ProgressBar

        android:id="@+id/progressBar3"
        style="?android:attr/progressBarStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

<!--    android:secondaryProgress="80"第二进度-->
<!--    android:progress="50"第一进度-->
<!--    android:max="100"最大刻度-->
    <ProgressBar
        android:secondaryProgress="80"
        android:progress="50"
        android:max="100"
        android:id="@+id/horiz"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

    <Button
        android:id="@+id/add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="增加" />

    <Button
        android:id="@+id/reduce"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="减少" />

    <Button
        android:id="@+id/reset"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="重置" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="TextView" />
    <Button
        android:id="@+id/show"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="显示进度条对话" />
</LinearLayout>

原文地址:https://blog.csdn.net/m0_66944345/article/details/130868175

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_15873.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注