我在想有没有人能帮我一下。我试图创建一个自定义AlertDialog。为了做到这一点,我在styles.xml中添加了以下代码行
<resources>
<style name="CustomAlertDialog" parent="android:Theme.Dialog.Alert">
<item name="android:windowBackground">@drawable/color_panel_background</item>
</style>
</resources>
Color_panel_background.9.png位于可绘制文件夹。这也可以在Android SDK res文件夹。
以下是主要活动。
package com.customdialog;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
public class CustomDialog extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.setTheme(R.style.CustomAlertDialog);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("HELLO!");
builder .setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//MyActivity.this.finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//dialog.cancel();
}
});
AlertDialog alertdialog = builder.create();
alertdialog.show();
}
}
为了将主题应用到AlertDialog,我必须将主题设置为当前上下文。
然而,我似乎不能让应用程序显示自定义的AlertDialog。有人能帮我一下吗?
任何试图在Fragment(使用支持库,即pre API 11)中做到这一点的人都应该这样做:
public class LoadingDialogFragment extends DialogFragment {
public static final String ID = "loadingDialog";
public static LoadingDialogFragment newInstance() {
LoadingDialogFragment f = new LoadingDialogFragment();
return f;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
StyleAlertDialog adb = new StyleAlertDialog(getActivity(), R.style.Your_Style);
adb.setView(getActivity().getLayoutInflater().inflate(R.layout.fragment_dialog_layout, null));
return adb;
}
private class StyleAlertDialog extends AlertDialog {
protected StyleAlertDialog(Context context, int theme) {
super(context, theme);
}
}
}
@Rflexor鼓励我扩展AlertDialog并公开构造函数
我一直在努力解决这个问题-你可以在你的主题中使用android:alertDialogStyle="@style/AlertDialog"设置对话框的背景,但它忽略了你所拥有的任何文本设置。正如@rflexor上面所说的,在Honeycomb之前的SDK无法做到这一点(你可以使用Reflection)。
简而言之,我的解决方案是使用上述方法设置对话框的背景样式,然后设置自定义标题和内容视图(使用与SDK中相同的布局)。
我的包装:
import com.mypackage.R;
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
public class CustomAlertDialogBuilder extends AlertDialog.Builder {
private final Context mContext;
private TextView mTitle;
private ImageView mIcon;
private TextView mMessage;
public CustomAlertDialogBuilder(Context context) {
super(context);
mContext = context;
View customTitle = View.inflate(mContext, R.layout.alert_dialog_title, null);
mTitle = (TextView) customTitle.findViewById(R.id.alertTitle);
mIcon = (ImageView) customTitle.findViewById(R.id.icon);
setCustomTitle(customTitle);
View customMessage = View.inflate(mContext, R.layout.alert_dialog_message, null);
mMessage = (TextView) customMessage.findViewById(R.id.message);
setView(customMessage);
}
@Override
public CustomAlertDialogBuilder setTitle(int textResId) {
mTitle.setText(textResId);
return this;
}
@Override
public CustomAlertDialogBuilder setTitle(CharSequence text) {
mTitle.setText(text);
return this;
}
@Override
public CustomAlertDialogBuilder setMessage(int textResId) {
mMessage.setText(textResId);
return this;
}
@Override
public CustomAlertDialogBuilder setMessage(CharSequence text) {
mMessage.setText(text);
return this;
}
@Override
public CustomAlertDialogBuilder setIcon(int drawableResId) {
mIcon.setImageResource(drawableResId);
return this;
}
@Override
public CustomAlertDialogBuilder setIcon(Drawable icon) {
mIcon.setImageDrawable(icon);
return this;
}
}
alert_dialog_title.xml(取自SDK)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<LinearLayout
android:id="@+id/title_template"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical"
android:layout_marginTop="6dip"
android:layout_marginBottom="9dip"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip">
<ImageView android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:paddingTop="6dip"
android:paddingRight="10dip"
android:src="@drawable/ic_dialog_alert" />
<TextView android:id="@+id/alertTitle"
style="@style/?android:attr/textAppearanceLarge"
android:singleLine="true"
android:ellipsize="end"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<ImageView android:id="@+id/titleDivider"
android:layout_width="fill_parent"
android:layout_height="1dip"
android:scaleType="fitXY"
android:gravity="fill_horizontal"
android:src="@drawable/divider_horizontal_bright" />
</LinearLayout>
alert_dialog_message.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrollView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="2dip"
android:paddingBottom="12dip"
android:paddingLeft="14dip"
android:paddingRight="10dip">
<TextView android:id="@+id/message"
style="?android:attr/textAppearanceMedium"
android:textColor="@color/dark_grey"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dip" />
</ScrollView>
然后只需使用CustomAlertDialogBuilder而不是AlertDialog。Builder来创建对话框,并像往常一样调用setTitle和setMessage。
任何试图在Fragment(使用支持库,即pre API 11)中做到这一点的人都应该这样做:
public class LoadingDialogFragment extends DialogFragment {
public static final String ID = "loadingDialog";
public static LoadingDialogFragment newInstance() {
LoadingDialogFragment f = new LoadingDialogFragment();
return f;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
StyleAlertDialog adb = new StyleAlertDialog(getActivity(), R.style.Your_Style);
adb.setView(getActivity().getLayoutInflater().inflate(R.layout.fragment_dialog_layout, null));
return adb;
}
private class StyleAlertDialog extends AlertDialog {
protected StyleAlertDialog(Context context, int theme) {
super(context, theme);
}
}
}
@Rflexor鼓励我扩展AlertDialog并公开构造函数
更好的方法是使用自定义对话框,并根据您的需要定制自定义对话框示例.....
public class CustomDialogUI {
Dialog dialog;
Vibrator vib;
RelativeLayout rl;
@SuppressWarnings("static-access")
public void dialog(final Context context, String title, String message,
final Runnable task) {
dialog = new Dialog(context);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.custom);
dialog.setCancelable(false);
TextView m = (TextView) dialog.findViewById(R.id.message);
TextView t = (TextView) dialog.findViewById(R.id.title);
final Button n = (Button) dialog.findViewById(R.id.button2);
final Button p = (Button) dialog.findViewById(R.id.next_button);
rl = (RelativeLayout) dialog.findViewById(R.id.rlmain);
t.setText(bold(title));
m.setText(message);
dialog.show();
n.setText(bold("Close"));
p.setText(bold("Ok"));
// color(context,rl);
vib = (Vibrator) context.getSystemService(context.VIBRATOR_SERVICE);
n.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
vib.vibrate(15);
dialog.dismiss();
}
});
p.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
vib.vibrate(20);
dialog.dismiss();
task.run();
}
});
}
//customize text style bold italic....
public SpannableString bold(String s) {
SpannableString spanString = new SpannableString(s);
spanString.setSpan(new StyleSpan(Typeface.BOLD), 0,
spanString.length(), 0);
spanString.setSpan(new UnderlineSpan(), 0, spanString.length(), 0);
// spanString.setSpan(new StyleSpan(Typeface.ITALIC), 0,
// spanString.length(), 0);
return spanString;
}
}
这是xml布局
<?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="match_parent"
android:background="#00000000"
>
<RelativeLayout
android:id="@+id/rlmain"
android:layout_width="fill_parent"
android:layout_height="150dip"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:background="#569CE3" >
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginLeft="25dip"
android:layout_marginTop="10dip" >
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Are you Sure?"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#ffffff"
android:textSize="13dip" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/relativeLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/relativeLayout1"
android:layout_alignRight="@+id/relativeLayout1"
android:layout_below="@+id/relativeLayout1"
android:layout_marginTop="5dip" >
</RelativeLayout>
<ProgressBar
android:id="@+id/process"
style="?android:attr/progressBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="3dip"
android:layout_marginTop="3dip" />
<RelativeLayout
android:id="@+id/relativeLayout3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/relativeLayout2"
android:layout_below="@+id/relativeLayout2"
android:layout_toLeftOf="@+id/process" >
<TextView
android:id="@+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#ffffff"
android:textSize="13dip"/>
</RelativeLayout>
<Button
android:id="@+id/next_button"
android:layout_width="90dip"
android:layout_height="35dip"
android:layout_alignParentBottom="true"
android:textColor="@drawable/button_text_color"
android:background="@drawable/blue_button"
android:layout_marginBottom="5dp"
android:textSize="10dp"
android:layout_alignRight="@+id/relativeLayout3"
android:text="Okay" />
<Button
android:id="@+id/button2"
android:text="Cancel"
android:textColor="@drawable/button_text_color"
android:layout_width="90dip"
android:layout_height="35dip"
android:layout_marginBottom="5dp"
android:background="@drawable/blue_button"
android:layout_marginRight="7dp"
android:textSize="10dp"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="@+id/next_button"
/>
</RelativeLayout>