如何以编程方式更改进度条的进度值?

时间:2019-05-01 12:28:13

标签: android customization android-progressbar progress

我试图为某些组件创建自定义视图,因为我的应用程序中非常需要这种组合。除了我的progressBar的进度外,代码中的所有内容均有效。我认为它无法正确获取XML文件的值。

在初始化变量中,我调用setProgressBar(progress)以将当前值提供给xml文件。例如,当我输入'setProgressBar(88)'时,它可以很好地工作,但不能使用它必须在xml文件中找到的值。

CustomProgressbar.java

public class CustomProgressbar extends RelativeLayout {
    @StyleableRes
    int index0 = 0;
    @StyleableRes
    int index1 = 1;
    @StyleableRes
    int index2 = 2;

    TextView titleText;
    TextView valueText;
    ProgressBar progressBar;

    public CustomProgressbar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {
        inflate(context, R.layout.custom_progressbar_layout, this);

        int[] sets = {R.attr.title, R.attr.value, R.attr.progress};

        TypedArray typedArray = context.obtainStyledAttributes(attrs, sets);

        CharSequence title = typedArray.getText(index0);
        CharSequence value = typedArray.getText(index1);
        int progress = typedArray.getInteger(index2,0);

        typedArray.recycle();

        titleText = findViewById(R.id.title_text);
        valueText = findViewById(R.id.value_text);
        progressBar = findViewById(R.id.progressbar_attr);

        setTitleText(title);
        setValueText(value);
        setProgressBar(progress);

    }

    public CharSequence getTitleText() {
        return titleText.getText();
    }
    public CharSequence getValueText() {
        return valueText.getText();
    }
    public int getProgressBar() {
        return progressBar.getProgress();
    }

    public void setTitleText(CharSequence value) {
        titleText.setText(value);
    }
    public void setValueText(CharSequence value) {
        valueText.setText(value);
    }
    public void setProgressBar(int value) {
        progressBar.setProgress(value);
    }
}

custom_progressbar_layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

        <TextView
            android:id="@+id/title_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true"
            android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
            android:textColor="@android:color/black" />

        <TextView
            android:id="@+id/value_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_alignParentEnd="true"
            android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
            android:textColor="@android:color/black" />

        <ProgressBar
            android:id="@+id/progressbar_attr"
            style="@android:style/Widget.ProgressBar.Horizontal"
            android:layout_width="match_parent"
            android:layout_height="25dp"
            android:layout_below="@id/value_text"
            android:layout_alignParentStart="true"
            android:layout_marginTop="3dp"
            android:layout_marginBottom="10dp"
            android:max="100"
            android:maxHeight="10dip"
            android:minHeight="10dip"
            android:progressTint="@android:color/holo_green_dark"
            android:progressDrawable="@drawable/progressbars" />
</RelativeLayout>

main_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:orientation="vertical"
    tools:context=".MainClass"
    android:background="@android:color/background_dark">

    <android.support.v7.widget.CardView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        app:cardCornerRadius="8dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:padding="15dp">

            <com.x.customprogressbar.CustomProgressbar
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:title="ThisIsTheTitle"
                app:value="€ 28,30"
                app:progress="77" />
        </LinearLayout>

    </android.support.v7.widget.CardView>
</LinearLayout>

还有我的属性

<resources>
    <declare-styleable name="CustomProgressbar">
        <attr name="title" format="string"/>
        <attr name="value" format="string"/>
        <attr name="progress" format="integer"/>
    </declare-styleable>
</resources>

当前结果:

  • XML文件,应该看起来像。在那里,它完美地工作。 Link1

  • 运行应用程序时。 Link2

(由于我尚未添加图片,所以我获得了链接。)

1 个答案:

答案 0 :(得分:1)

尝试一下,样式属性以另一种方式获取

select top 3 EmployeeID,SickLeaveTaken,s.SiteName,rn from 
(select e.EmployeeID,SickLeaveTaken,e.SiteID ,RANK() over(order by [SickLeaveTaken] 
desc ) as rn
from @Employee e )e
left join @Site s on e.SiteID=s.SiteID
order by rn desc

}