TShopping

 找回密碼
 註冊
搜索
查看: 1108|回復: 0
打印 上一主題 下一主題

[教學] Android ProgressBar 進度條元件

[複製鏈接]
跳轉到指定樓層
1#
發表於 2019-12-24 22:23:20 | 只看該作者 |只看大圖 回帖獎勵 |倒序瀏覽 |閱讀模式
 
Push to Facebook
  1. ProgressBar pb1;
  2. pb1 = (ProgressBar) findViewById(R.id.pb1);
  3. pb1.setVisibility(progress<100? View.VISIBLE:View.GONE); pb1.setProgress(progress);
複製代碼

示範
XML
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"
  4.     android:id="@+id/layout"
  5.     android:layout_width="match_parent"
  6.     android:layout_height="match_parent"
  7.     android:layout_alignParentBottom="true"
  8.     android:layout_alignParentEnd="true"
  9.     android:layout_alignParentRight="true"
  10.     >

  11.     <ProgressBar
  12.         android:id="@+id/pb1"
  13.         style="?android:attr/progressBarStyle"
  14.         android:layout_width="wrap_content"
  15.         android:layout_height="wrap_content"
  16.         android:alpha="0.5"
  17.         app:layout_constraintStart_toStartOf="parent"
  18.         app:layout_constraintTop_toTopOf="parent" />

  19.     <ProgressBar
  20.         android:id="@+id/pb2"
  21.         style="?android:attr/progressBarStyleHorizontal"
  22.         android:layout_width="191dp"
  23.         android:layout_height="19dp"
  24.         android:layout_marginEnd="168dp"
  25.         android:layout_marginStart="168dp"
  26.         android:layout_marginTop="8dp"
  27.         app:layout_constraintEnd_toEndOf="parent"
  28.         app:layout_constraintStart_toStartOf="parent"
  29.         app:layout_constraintTop_toBottomOf="@+id/pb1" />

  30.     <Button
  31.         android:id="@+id/b1"
  32.         android:layout_width="wrap_content"
  33.         android:layout_height="wrap_content"
  34.         android:layout_marginBottom="8dp"
  35.         android:layout_marginEnd="8dp"
  36.         android:layout_marginLeft="8dp"
  37.         android:layout_marginRight="8dp"
  38.         android:layout_marginStart="8dp"
  39.         android:layout_marginTop="8dp"
  40.         android:onClick="add"
  41.         android:text="增加進度條"
  42.         app:layout_constraintBottom_toBottomOf="parent"
  43.         app:layout_constraintEnd_toEndOf="parent"
  44.         app:layout_constraintStart_toStartOf="parent"
  45.         app:layout_constraintTop_toTopOf="parent" />

  46. </android.support.constraint.ConstraintLayout>
複製代碼

JAVA
  1. public class MainActivity extends AppCompatActivity {
  2.     ProgressBar pb1, pb2;
  3.     int progress = 0;

  4.     @Override
  5.     protected void onCreate(Bundle savedInstanceState) {
  6.         super.onCreate(savedInstanceState);
  7.         setContentView(R.layout.activity_main);
  8.   
  9.         pb1 = (ProgressBar) findViewById(R.id.pb1);
  10.         pb2 = (ProgressBar) findViewById(R.id.pb2);

  11.     }


  12.     public void add(View v){
  13.         progress+=10;
  14.         pb2.setVisibility(progress<100? View.VISIBLE:View.GONE);
  15.         pb2.setProgress(progress);
  16.         pb1.setVisibility(progress<100? View.VISIBLE:View.GONE);
  17.         pb1.setProgress(progress);
  18.     }
  19. }
複製代碼

展示


文章出處

 

臉書網友討論
*滑块验证:
您需要登錄後才可以回帖 登錄 | 註冊 |

本版積分規則



Archiver|手機版|小黑屋|免責聲明|TShopping

GMT+8, 2024-4-23 17:37 , Processed in 0.056885 second(s), 25 queries .

本論壇言論純屬發表者個人意見,與 TShopping綜合論壇 立場無關 如有意見侵犯了您的權益 請寫信聯絡我們。

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表