|
- ProgressBar pb1;
- pb1 = (ProgressBar) findViewById(R.id.pb1);
- pb1.setVisibility(progress<100? View.VISIBLE:View.GONE); pb1.setProgress(progress);
複製代碼
示範
XML
- <?xml version="1.0" encoding="utf-8"?>
- <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- android:id="@+id/layout"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_alignParentBottom="true"
- android:layout_alignParentEnd="true"
- android:layout_alignParentRight="true"
- >
- <ProgressBar
- android:id="@+id/pb1"
- style="?android:attr/progressBarStyle"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:alpha="0.5"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent" />
- <ProgressBar
- android:id="@+id/pb2"
- style="?android:attr/progressBarStyleHorizontal"
- android:layout_width="191dp"
- android:layout_height="19dp"
- android:layout_marginEnd="168dp"
- android:layout_marginStart="168dp"
- android:layout_marginTop="8dp"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toBottomOf="@+id/pb1" />
- <Button
- android:id="@+id/b1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginBottom="8dp"
- android:layout_marginEnd="8dp"
- android:layout_marginLeft="8dp"
- android:layout_marginRight="8dp"
- android:layout_marginStart="8dp"
- android:layout_marginTop="8dp"
- android:onClick="add"
- android:text="增加進度條"
- app:layout_constraintBottom_toBottomOf="parent"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent" />
- </android.support.constraint.ConstraintLayout>
複製代碼
JAVA
- public class MainActivity extends AppCompatActivity {
- ProgressBar pb1, pb2;
- int progress = 0;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- pb1 = (ProgressBar) findViewById(R.id.pb1);
- pb2 = (ProgressBar) findViewById(R.id.pb2);
- }
- public void add(View v){
- progress+=10;
- pb2.setVisibility(progress<100? View.VISIBLE:View.GONE);
- pb2.setProgress(progress);
- pb1.setVisibility(progress<100? View.VISIBLE:View.GONE);
- pb1.setProgress(progress);
- }
- }
複製代碼
展示
ProgressBar 進度條
文章出處
|
|