如何以编程方式更改进度条的进度值?
How do I change the progress value of a progress bar programmatically?
我正在尝试创建一些组件的自定义视图,因为我的应用程序中非常需要这种组合。除了我的 progressBar
的进度外,代码中的所有内容都有效。我认为它没有正确获取 XML
文件的值。
在我的 init void 中,我调用 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
(我还不能添加图片所以我得到了链接。)
试试这个,样式属性以另一种方式出现
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);
}
}
我正在尝试创建一些组件的自定义视图,因为我的应用程序中非常需要这种组合。除了我的 progressBar
的进度外,代码中的所有内容都有效。我认为它没有正确获取 XML
文件的值。
在我的 init void 中,我调用 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
(我还不能添加图片所以我得到了链接。)
试试这个,样式属性以另一种方式出现
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);
}
}