问题
I'm new to Android..
In my app I'm using a progress dialog. I want to set background image for progress dialog. How do I implement this?
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(FrontActivity.this);
pDialog.setMessage("Loading Grades. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
回答1:
This is my layout for custom progress dialog :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:background="@layout/custom_rounded_box"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight=".6"
android:orientation="horizontal" >
<ProgressBar
android:id="@+id/progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:indeterminateDrawable="@drawable/progress_background" />
<TextView
android:id="@+id/dialogTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".3"
android:gravity="center_horizontal|center_vertical"
android:text=""
android:textSize="22sp"
android:textStyle="bold" />
<ProgressBar
android:id="@+id/progress"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight=".05"
android:background="@android:color/black"
android:indeterminateDrawable="@drawable/progress_background"
android:visibility="invisible" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:orientation="vertical" >
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/light_gray_line" >
</ImageView>
<TextView
android:id="@+id/dialogMessage"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@+id/dialogTitle"
android:focusable="true"
android:focusableInTouchMode="true"
android:gravity="center_vertical"
android:maxLines="10"
android:padding="10dip"
android:scrollbars="vertical"
android:text=""
android:textSize="18sp" >
</TextView>
</LinearLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight=".6" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:orientation="horizontal" >
<Button
android:id="@+id/OkButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/dialogMessage"
android:layout_centerHorizontal="true"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dp"
android:background="@drawable/ok_btn" >
</Button>
<Button
android:id="@+id/cancelButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/dialogMessage"
android:layout_centerHorizontal="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dip"
android:background="@drawable/cancel_btn" />
</LinearLayout>
</RelativeLayout>
</LinearLayout>`
And this is my custom Dialog class
public class CustomizeDialog extends Dialog implements OnClickListener {
Button okButton;
Context mContext;
TextView mTitle = null;
TextView mMessage = null;
View v = null;
Button cancelButton;
ProgressBar progressBar;
public CustomizeDialog(Context context) {
super(context);
mContext = context;
/** 'Window.FEATURE_NO_TITLE' - Used to hide the mTitle */
requestWindowFeature(Window.FEATURE_NO_TITLE);
/** Design the dialog in main.xml file */
setContentView(R.layout.custom_dialog_box);
v = getWindow().getDecorView();
v.setBackgroundResource(android.R.color.transparent);
mTitle = (TextView) findViewById(R.id.dialogTitle);
mMessage = (TextView) findViewById(R.id.dialogMessage);
okButton = (Button) findViewById(R.id.OkButton);
cancelButton = (Button) findViewById(R.id.cancelButton);
cancelButton.setOnClickListener(this);
okButton.setOnClickListener(this);
progressBar = (ProgressBar) findViewById(R.id.progress);
}
@Override
public void setTitle(CharSequence title) {
super.setTitle(title);
mTitle.setText(title);
}
@Override
public void setTitle(int titleId) {
super.setTitle(titleId);
mTitle.setText(mContext.getResources().getString(titleId));
}
/**
* Set the message text for this dialog's window.
*
* @param message
* - The new message to display in the title.
*/
public void setMessage(CharSequence message) {
mMessage.setText(message);
mMessage.setMovementMethod(ScrollingMovementMethod.getInstance());
}
/**
* Set the message ID
*
* @param messageId
*/
public void setMessage(int messageId) {
mMessage.setText(mContext.getResources().getString(messageId));
mMessage.setMovementMethod(ScrollingMovementMethod.getInstance());
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}
you can use it in any Activity by passing the title and message
mCustomizeDialog = new CustomizeDialog(myActivity.this);
mCustomizeDialog.setTitle("Title");
mCustomizeDialog.setMessage("Message");
mCustomizeDialog.okButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mCustomizeDialog.dismiss();
});
mCustomizeDialog.show();
Hope this helps you :)
回答2:
Try this:
This progressDialog with backgroud i hope this will be help to you
Change background of ProgressDialog
回答3:
How can I change the background of Android alert dialogs?
I think this same way would work on progressdialog aswell.
回答4:
You can use your custom layout for progress
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/progress_backgroundimage"
android:layout_centerInParent="true"
android:padding="10dip">
<ProgressBar android:id="@+id/progress_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
/>
<TextView android:id="@+id/text_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/progress_bar"
android:text="Loading Please wait..."
android:textSize="10dip"
android:layout_centerVertical="true"
android:layout_margin="10dip"/>
</RelativeLayout>
来源:https://stackoverflow.com/questions/14874247/how-to-set-background-image-for-progress-dialog