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

问题描述 投票:0回答:1

我正在尝试创建一些组件的自定义视图,因为我在我的应用程序中需要这么多组合。除了我的progressBar的进展外,代码中的所有内容都有效。我认为它没有正确获得XML文件的值。

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

custom progress bar.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

(我还不允许添加图片,所以我有链接。)

android customization android-progressbar progress
1个回答
1
投票

试试这个,样式属性正在以另一种方式进行

public class CustomProgressbar extends RelativeLayout {

private TextView titleText;
private TextView valueText;
private 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);

    TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomProgressbar);

    CharSequence title = typedArray.getText(R.styleable.CustomProgressbar_title);
    CharSequence value = typedArray.getText(R.styleable.CustomProgressbar_value);
    int progress = typedArray.getInteger(R.styleable.CustomProgressbar_progress, 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);
}

}

© www.soinside.com 2019 - 2024. All rights reserved.