我是Android新手..
在我的应用中,我正在使用进度对话框。我想为进度对话框设置背景图像。我该如何实现呢?
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(FrontActivity.this);
pDialog.setMessage("Loading Grades. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
答案 0 :(得分:2)
这是我自定义进度对话框的布局:
<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>`
这是我的自定义Dialog类
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
}
}
您可以通过传递标题和消息
在任何活动中使用它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();
希望这可以帮助你:)
答案 1 :(得分:0)
答案 2 :(得分:-1)
How can I change the background of Android alert dialogs?
我认为同样的方式也适用于progressdialog。
答案 3 :(得分:-1)
您可以使用自定义布局进行
<?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>