我的自定义进度栏不显示。我在我的活动中为其设置动画,并为其使用可绘制资源文件。这是我的代码。
Activity.java
ProgressBar progressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
progressBar = findViewById(R.id.progressBar);
ObjectAnimator animation = ObjectAnimator.ofInt(progressBar, "progress",
0, 500);
animation.setDuration(5000); // in milliseconds
animation.setInterpolator(new DecelerateInterpolator());
animation.start();
activity.xml
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:max="500"
android:progress="0"
android:progressDrawable="@drawable/circular"
app:layout_constraintBottom_toBottomOf="@+id/relativeLayout"
app:layout_constraintEnd_toEndOf="@+id/relativeLayout"
app:layout_constraintHorizontal_bias="0.475"
app:layout_constraintStart_toStartOf="@+id/relativeLayout"
app:layout_constraintTop_toTopOf="@+id/relativeLayout"
app:layout_constraintVertical_bias="0.153" />
drawableResource.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:useLevel="true"
android:indeterminate="true"
android:innerRadiusRatio="2.3"
android:shape="ring"
android:thickness="3.8sp" >
<solid android:color="@color/colorPrimary" />
</shape>
答案 0 :(得分:0)
不确定您的自定义代码为什么不起作用,但是该代码对您的activity.java起作用,因此您的XML肯定是问题所在。这是一个有效的进度条。如果您想添加百分比,建议您在这里查看:https://github.com/ylyc/circular_progress_bar。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ProgressBar android:indeterminate="true"
android:useLevel="true"
android:layout_centerInParent="true"
android:indeterminateDrawable="@drawable/circular"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:id="@+id/progressBar1"></ProgressBar>
</RelativeLayout>