App下載

在Android開(kāi)發(fā)中怎么實(shí)現(xiàn)好看的進(jìn)度條?好看進(jìn)度條實(shí)現(xiàn)方法(代碼片段)!

潮起潮落 2021-08-26 14:51:37 瀏覽數(shù) (2648)
反饋

介紹

在本文中,我們將創(chuàng)建不同類(lèi)型的進(jìn)度條,例如線(xiàn)性、圓形、確定的和不確定的。

第一步是創(chuàng)建一個(gè)活動(dòng)(MainActivity.java)。首先,我們將創(chuàng)建一個(gè)活動(dòng)類(lèi),然后我們將在活動(dòng) XML 文件中編寫(xiě)以下代碼。

activity_main.xml 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical"
        android:padding="16dp">
        <TextView
            android:id="@+id/textView17"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="18sp"
            android:textStyle="bold"
            android:textAlignment="center"
            android:text="\u00A9  itinsidenews.com" />
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:orientation="vertical">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Determinate" />
            <ProgressBar
                android:id="@+id/progressDeterminate"
                style="@style/Widget.AppCompat.ProgressBar.Horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:indeterminate="false"
                android:max="100"
                android:progress="10" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:orientation="vertical">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Indeterminate" />
            <ProgressBar
                android:id="@+id/progressIndeterminate"
                style="@style/Widget.AppCompat.ProgressBar.Horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:indeterminate="true" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:orientation="vertical">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Buffer" />
            <ProgressBar
                android:id="@+id/progressBuffered"
                style="@style/Widget.AppCompat.ProgressBar.Horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:indeterminate="false"
                android:max="100"
                android:progress="10"
                android:secondaryProgress="20" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:orientation="vertical">
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Indeterminate and Determinate" />
            <ProgressBar
                android:id="@+id/progressIndeterminateDeterminate"
                style="@style/Widget.AppCompat.ProgressBar.Horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:indeterminate="false"
                android:max="100"
                android:progress="20" />
        </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:orientation="horizontal">
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:layout_weight="1"
                android:gravity="center"
                android:orientation="vertical">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_margin="15dp"
                    android:text="Indeterminate" />
                <ProgressBar
                    style="@style/Widget.AppCompat.ProgressBar"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:indeterminate="true"
                   android:progressDrawable="@drawable/circular_progress_bar" />
            </LinearLayout>
            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_margin="10dp"
                android:layout_weight="1"
                android:gravity="center"
                android:orientation="vertical">
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_margin="15dp"
                    android:text="Determine" />
                <ProgressBar
                    android:id="@+id/progressIndeterminateCircular"
                    style="@style/Widget.AppCompat.ProgressBar.Horizontal"
                    android:layout_width="?attr/actionBarSize"
                    android:layout_height="?attr/actionBarSize"
                    android:background="@drawable/circle_shape"
                    android:indeterminate="false"
                    android:max="100"
                    android:progress="0"
                   android:progressDrawable="@drawable/circular_progress_bar" />
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

MainActivity Java 文件代碼

在MainActivity.java文件中,我們的第一步是初始化所有的進(jìn)度條視圖,然后編寫(xiě)三個(gè)void類(lèi)型的函數(shù),分別為每個(gè)進(jìn)度條編寫(xiě)代碼。 

我們?cè)诿總€(gè)函數(shù)中使用了一個(gè)處理程序。在Android中,我們不能在主線(xiàn)程上運(yùn)行長(zhǎng)期任務(wù);這就是我們使用處理程序的原因。處理程序允許從其他后臺(tái)線(xiàn)程與 UI 線(xiàn)程進(jìn)行通信。 

MainActivity.java

package com.progressbar.example.mainactivity; 

import android.os.Bundle; import android.os.Handler; import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.widget.Toolbar; import android.view.Menu; import android.view.MenuItem; import android.widget.ProgressBar; import android.widget.Toast; import com.progressbar.example.R; import com.progressbar.example.utils.Tools; public class MainActivity extends AppCompatActivity {     private ProgressBar progressDeterminate;     private ProgressBar progressIndeterminateCircular;     private ProgressBar progressBuffered;     private ProgressBar progressIndeterminateDeterminate;     @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);                 initComponent();     }      private void initComponent() {         progressDeterminate = (ProgressBar) findViewById(R.id.progressDeterminate);         progressIndeterminateCircular = (ProgressBar) findViewById(R.id.progressIndeterminateCircular);         progressBuffered = (ProgressBar) findViewById(R.id.progressBuffered);         progressIndeterminateDeterminate = (ProgressBar) findViewById(R.id.progressIndeterminateDeterminate);         startDeterminateProgress();         startBufferedProgress();         startBufferedSecondaryProgress();         startIndeterminateDeterminateProgress();         startDeterminateCircularProgress();     }     private void startDeterminateProgress() {         final Handler mHandler = new Handler();         Runnable runnable = new Runnable() {             public void run() {                 int progress = progressDeterminate.getProgress() + 10;                 progressDeterminate.setProgress(progress);                 if (progress > 100) {                     progressDeterminate.setProgress(0);                 }                 mHandler.postDelayed(this, 1000);         

 之后,我們就可以運(yùn)行我們的 android 應(yīng)用程序了!


0 人點(diǎn)贊