@RitcheeQinG
2020-08-25T09:17:25.000000Z
字数 1927
阅读 312
Android
public class HorizontalProgressBar extends ConstraintLayout {
private View bar;
public HorizontalProgressBar(@NonNull Context context) {
super(context);
initView();
}
public HorizontalProgressBar(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
initView();
}
public HorizontalProgressBar(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView();
}
private void initView() {
LayoutInflater.from(getContext()).inflate(R.layout.layout_horizon_progress_bar, this);
bar = findViewById(R.id.view_bar);
}
public void setPercent(float percent) {
if (percent > 1) {
percent = 1;
}
ConstraintLayout.LayoutParams params = (LayoutParams) bar.getLayoutParams();
params.horizontalBias = percent;
bar.setLayoutParams(params);
}
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/cl_bg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/shape_btn_half_round">
<View
android:id="@+id/view_bar"
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="@drawable/shape_btn_half_round_grey"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_percent="0.5" />
</androidx.constraintlayout.widget.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#8A96B0" />
<!-- 圆角 -->
<corners
android:bottomLeftRadius="@dimen/dimen_19dp"
android:bottomRightRadius="@dimen/dimen_19dp"
android:topLeftRadius="@dimen/dimen_19dp"
android:topRightRadius="@dimen/dimen_19dp" />
</shape>