如何创建自定义ProgressBar

时间:2018-10-05 19:24:16

标签: java android android-studio android-progressbar

修改:
问题1)已解决。但是我仍然无法更改为color中的ProgressBar。我尝试使用自己的theme(请参见下面的代码)。

我希望我的ProgressBar如下图所示。我已经在ProgressBar上方创建了ListView。我在ListView中使用了ProgressBarTextViews和两个RelativeLayout

我的问题:

  

1。)如何以如下所示的方式将TextViews对准ProgressBar
  2.)如何设置ProgressBar本身的颜色和ProgressBar的背景颜色?

enter image description here

MainActivity.xml

   <ProgressBar
    android:id="@+id/progressBar"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/progressBarText"
    android:progress="0"
    android:max="100"
    android:paddingTop="0dp"
    android:paddingBottom="10dp"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:theme="@style/progressBarTheme"
    style="@style/Widget.AppCompat.ProgressBar.Horizontal"/>

styles.xml

<resources>

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

<style name="DialogTheme" parent="Theme.AppCompat.Light.Dialog">
    <item name="colorAccent">@android:color/holo_green_dark</item>
</style>

<style name="progressBarTheme" parent="@style/Theme.AppCompat">
    <item name="colorAccent">@color/myRedColor</item>
</style>

</resources>

3 个答案:

答案 0 :(得分:1)

  

1。)如何以如下所示的方式在ProgressBar上方对齐TextView?

尝试一下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal">

    <ProgressBar
        android:id="@+id/progressCircle"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" />

    <TextView
        android:id="@+id/txtvStatusCircle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/progressCircle"
        android:layout_alignParentStart="true"
        android:text="Daily Progress"
        android:textSize="18sp" />

    <TextView
        android:id="@+id/txtPercent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@id/progressCircle"
        android:layout_alignParentEnd="true"
        android:text="0 %"
        android:textSize="18sp" />

</RelativeLayout>
  

2。)如何设置ProgressBar本身的颜色和ProgressBar的背景颜色?

在您的styles.xml中:

<style name="progressBarBlue" parent="@style/Theme.AppCompat">
        <item name="colorAccent">@color/myBlueColor</item>
</style>

然后在xml中设置为ProgressBar主题:

<ProgressBar
    ...
    android:theme="@style/progressBarBlue" />

关于ProgressBar本身,您为样式中的AccentColor选择的任何内容也将为ProgressBar设置。因此更改颜色:

<item name="colorAccent">@color/colorAccent/item>

可以解决问题。

输出:

enter image description here

答案 1 :(得分:1)

尝试一下:

<android.support.constraint.ConstraintLayout
            android:id="@+id/uplaodFile"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/upload"
            android:layout_marginTop="10dp"
            >

            <ImageView
                android:id="@+id/imageView2"
                android:layout_width="46dp"
                android:layout_height="65dp"
                android:src="@drawable/file_image"
                app:layout_constraintEnd_toStartOf="@+id/horizontal_progress_bar"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="@+id/notification" />

            <TextView
                android:id="@+id/notification"
                android:layout_width="282dp"
                android:layout_height="22dp"
                android:layout_marginBottom="2dp"
                android:text="File Detail"
                app:layout_constraintBottom_toTopOf="@+id/cancel"
                app:layout_constraintStart_toStartOf="@+id/horizontal_progress_bar"
                app:layout_constraintTop_toTopOf="parent"
                tools:ignore="MissingConstraints" />

            <ProgressBar
                android:id="@+id/horizontal_progress_bar"
                style="?android:attr/progressBarStyleHorizontal"
                android:layout_width="201dp"
                android:layout_height="26dp"
                android:layout_marginEnd="8dp"
                android:layout_marginTop="24dp"
                android:indeterminate="false"
                android:max="100"
                android:minHeight="100dp"
                android:progress="2"
                android:progressBackgroundTint="#aaaaaa"
                app:layout_constraintEnd_toStartOf="@+id/cancel"
                app:layout_constraintTop_toTopOf="parent"
                tools:ignore="MissingConstraints" />

            <TextView
                android:id="@+id/process_state"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="8dp"
                android:layout_marginEnd="8dp"
                android:layout_marginStart="8dp"
                android:layout_marginTop="40dp"
                android:text="uploading.. 10%"
                android:textAlignment="center"
                android:textColor="#4B74FF"
                android:textSize="10dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintHorizontal_bias="0.833"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                tools:ignore="MissingConstraints" />

            <ImageView
                android:id="@+id/cancel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="36dp"
                android:layout_marginEnd="10dp"
                android:src="@drawable/ic_close_black_24dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                tools:ignore="MissingConstraints" />


        </android.support.constraint.ConstraintLayout>

这是Java代码

ProgressBar progressBar;

process_status = findViewById(R.id.process_state);
progressBar = findViewById(R.id.horizontal_progress_bar);
progressBar.setProgressTintList(ColorStateList.valueOf(Color.parseColor("#4B74FF")));
progressBar.setProgress(currentProgress);
textView.setText("Uploading..."+currentProgress+ "%");

答案 2 :(得分:1)

这更改了进度条的颜色。

progressBar.getProgressDrawable().setColorFilter(
Color.RED, android.graphics.PorterDuff.Mode.SRC_IN);