编程语言
首页 > 编程语言> > java-如何创建自定义ProgressBar

java-如何创建自定义ProgressBar

作者:互联网

编辑:
问题1)已解决.但是我仍然无法更改ProgressBar的颜色.我尝试使用自己的主题(请参见下面的代码).

我希望我的ProgressBar看起来像下面的图像.我已经在ListView上方创建了一个ProgressBar.我在RelativeLayout中使用ListView,ProgressBar和两个TextView.

我的问题:

1.) How can I align the TextViews above the ProgressBar in the way shown below?
2.) How can I set the Color of the ProgressBar itself and the Background Color of the 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>

解决方法:

1.) How can I align the TextViews above the ProgressBar in the way shown below?

尝试这个:

<?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.) How can I set the Color of the ProgressBar itself and the Background Color of the 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

标签:android-studio,android-progressbar,java,android
来源: https://codeday.me/bug/20191108/2008480.html