我必须在onPreExecute()
中设置消息和标题,但无法做到。 ProgressDialog
有setMessage()
方法,但ProgressBar
没有。
protected void onPreExecute() {
progressBar.
super.onPreExecute();
}
我必须在onPreExecute()
中设置消息和标题,但无法做到。 ProgressDialog
有setMessage()
方法,但ProgressBar
没有。
protected void onPreExecute() {
progressBar.
super.onPreExecute();
}
使用 ProgressBar 显示进度,使用 TextView 显示消息:
<ProgressBar
android:id="@+id/indeterminateBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
创建自己的ProgressBar并将一个TextView放在旁边。来自https://github.com/Q115/DelayedProgressDialog的库可以解决问题。
用法:
DelayedProgressDialog progressDialog = new DelayedProgressDialog();
progressDialog.show(getSupportFragmentManager(), "tag");
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ProgressBar
android:id="@+id/indeterminateBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/progress_bar_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="loading..."
/>
</LinearLayout>
你的 DialogFragment 类
public class Dialog extends DialogFragment {
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new
AlertDialog.Builder(getActivity());
//use layoutinflater to inflate xml
LayoutInflater inflater = getActivity().getLayoutInflater();
builder.setView(inflater.inflate(R.layout.xml_containing_progressbar,null));
return builder.create();
}
}
Dialog dialog = new Dialog();
dialog.show(getSupportFragmentManager(), "tag");
虽然有点晚,但总比不做好。我曾经遇到同样的问题,所以我创建了一个自定义进度条。
首先,您需要为视图创建类:
public class CustomProgressBar extends RelativeLayout {
private ProgressBar progressBar;
private TextView textView;
public CustomProgressBar(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.custom_progress_bar, this);
progressBar = findViewById(R.id.progressBarCustom);
textView = findViewById(R.id.pbTV);
init(attrs);
private void init(@Nullable AttributeSet attrs) {
if (attrs != null) {
TypedArray ta = getContext().obtainStyledAttributes(attrs, R.styleable.CustomProgressBar);
String textAttrib = ta
.getString(R.styleable.CustomProgressBar_text);
textView.setText(textAttrib);
ta.recycle();
}
}
public void setProgressBar(ProgressBar progressBar) {
this.progressBar = progressBar;
}
public String getText() {
return textView.getText().toString();
}
public void setText(String text) {
textView.setText(text);
}
public void setText(int resid) {
textView.setText(resid);
}
}
接下来,为了能够从布局中设置这个类的属性,我们需要把它变成样式化的。因此,你需要在“values”目录下创建一个名为"attrs.xml"的 XML 文件。
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="CustomProgressBar">
<attr name="text" format="string"/>
</declare-styleable>
</resources>
现在我们为自定义视图创建布局:
<?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="wrap_content">
<ProgressBar
android:id="@+id/progressBarCustom"
style="?android:attr/progressBarStyle"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_centerInParent="true"/>
<TextView
android:id="@+id/pbTV"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"/>
</RelativeLayout>
最后,您可以在任何地方使用它:
<com.example.mypackage.CustomProgressBar
android:id="@+id/radioGroupProgressBar"
android:layout_width="match_parent"
android:layout_height="150dp"
app:text="Loading..."
android:layout_gravity="center_horizontal"
/>
就是这样 :)