安卓学习 之 ProgressBar(进度条)控件
首先先新建一个Activity,命名为ProgressBarActivity:
然后在布局文件中增加一个进度条控件,进度条控件默认是圆圈形状的不停地转圈圈的:
下面做了一个水平的进度条,并用代码控制进度条慢慢的涨,这里面需要有一点特别注意啊!
************在android中,4.0以后是不能直接在线程中操作控件的 进度条是个特例*************
下面来看Activity的代码吧:
package com.example.android1;import android.os.Bundle;
import android.widget.ProgressBar;import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;public class ProgressBarActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);EdgeToEdge.enable(this);setContentView(R.layout.activity_progress_bar);ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);return insets;});////在android中,4.o以后是不能直接在线程中操作控件的////进度条十个特例ProgressBar jindutiao = findViewById(R.id.jindutiao); //定义一个ProgressBar类型的进度条变量 接收ID为jindutiao的控件jindutiao.setProgress(80); //进度条的进度设置为80new Thread(){ //新建一个线程public void run(){ //定义一个方法为runfor(int i = 0; i < 200; i++) //for循环从开始到200{jindutiao.setProgress(i); //进度条的进度值设置为 变量itry { //试着休眠500msThread.sleep(500);} catch (InterruptedException e) {throw new RuntimeException(e);}}}}.start(); //线程开始}
}
下面是布局文件的代码:
其实进度条的属性也就学了这么几条?
style //设置风格的属性
style="?android:attr/progressBarStyleHorizontal" //把进度条设置成水平形状的
android:progress="40" //设置进度的 百分比就是40/200
android:max="200" //整条进度条到头的最大值 默认值100
android:indeterminate="true" //设置是否永恒滚动
<?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:id="@+id/main"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".ProgressBarActivity"android:background="#00FFFF"android:orientation="vertical"><ProgressBarandroid:layout_width="20dp"android:layout_height="20dp"/>
<!--style //设置风格的属性style="?android:attr/progressBarStyleHorizontal" //把进度条设置成水平形状的android:progress="40" //设置进度的 百分比就是40/200android:max="200" //整条进度条到头的最大值 默认值100
--><ProgressBarandroid:layout_width="match_parent"android:layout_height="wrap_content"style="?android:attr/progressBarStyleHorizontal"android:progress="40"android:layout_marginTop="50dp"android:max="200"/>
<!--android:indeterminate="true" //设置是否永恒滚动
--><ProgressBarandroid:layout_width="match_parent"android:layout_height="wrap_content"style="?android:attr/progressBarStyleHorizontal"android:indeterminate="true"android:layout_marginTop="50dp"android:progress="40"android:max="200"/><ProgressBarandroid:id="@+id/jindutiao"android:layout_width="match_parent"android:layout_height="wrap_content"style="?android:attr/progressBarStyleHorizontal"android:layout_marginTop="50dp"android:progress="40"android:max="200"/></LinearLayout>
还是很简单的,可以下楼走圈圈了。放松一下自己的紧张头脑吧!学习是要慢慢走的,欲速则不达!明天再学!