[关闭]
@RitcheeQinG 2020-08-25T09:17:25.000000Z 字数 1927 阅读 312

利用ConstraintLayout轻松实现横向进度条

Android


  1. public class HorizontalProgressBar extends ConstraintLayout {
  2. private View bar;
  3. public HorizontalProgressBar(@NonNull Context context) {
  4. super(context);
  5. initView();
  6. }
  7. public HorizontalProgressBar(@NonNull Context context, @Nullable AttributeSet attrs) {
  8. super(context, attrs);
  9. initView();
  10. }
  11. public HorizontalProgressBar(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
  12. super(context, attrs, defStyleAttr);
  13. initView();
  14. }
  15. private void initView() {
  16. LayoutInflater.from(getContext()).inflate(R.layout.layout_horizon_progress_bar, this);
  17. bar = findViewById(R.id.view_bar);
  18. }
  19. public void setPercent(float percent) {
  20. if (percent > 1) {
  21. percent = 1;
  22. }
  23. ConstraintLayout.LayoutParams params = (LayoutParams) bar.getLayoutParams();
  24. params.horizontalBias = percent;
  25. bar.setLayoutParams(params);
  26. }
  27. }
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. android:id="@+id/cl_bg"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:background="@drawable/shape_btn_half_round">
  8. <View
  9. android:id="@+id/view_bar"
  10. android:layout_width="0dp"
  11. android:layout_height="match_parent"
  12. android:background="@drawable/shape_btn_half_round_grey"
  13. app:layout_constraintBottom_toBottomOf="parent"
  14. app:layout_constraintHorizontal_bias="0.0"
  15. app:layout_constraintLeft_toLeftOf="parent"
  16. app:layout_constraintRight_toRightOf="parent"
  17. app:layout_constraintTop_toTopOf="parent"
  18. app:layout_constraintWidth_percent="0.5" />
  19. </androidx.constraintlayout.widget.ConstraintLayout>
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:shape="rectangle">
  4. <solid android:color="#8A96B0" />
  5. <!-- 圆角 -->
  6. <corners
  7. android:bottomLeftRadius="@dimen/dimen_19dp"
  8. android:bottomRightRadius="@dimen/dimen_19dp"
  9. android:topLeftRadius="@dimen/dimen_19dp"
  10. android:topRightRadius="@dimen/dimen_19dp" />
  11. </shape>
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注