如何自定义对话框的标题部分

时间:2014-06-12 07:15:47

标签: android dialog alertdialog android-alertdialog

我创建了一个自定义的dialog并将layout xml应用于它。但layout始终应用于dialog的正文,而不会应用于标题部分。我所能做的就是使用dilaog以编程方式设置setTitle的标题,并使用setFeatureDrawableResource添加图标。请告诉我如何自定义自定义dialog的标题部分?

方问:今天当我访问stackoverflow帐户时,我发现,有超过200个点被扣除了?任何想法为什么?

Java_Code:

reportDialog = new Dialog(MeetingPointFix.this);
            reportDialog.requestWindowFeature(Window.FEATURE_LEFT_ICON);
            reportDialog.setCancelable(false);
            LayoutInflater reportAlertDialogLayoutInflater = LayoutInflater.from(getApplicationContext());
            View reportAlertDialogInflatedView = reportAlertDialogLayoutInflater.inflate(R.layout.meetingpointfix_report_dialog, null);
            reportDialog.setContentView(reportAlertDialogInflatedView);

            int [] viewsRefsIds = {R.id.reportLocNameValue, R.id.reportLocLatValue, R.id.reportLocLngValue, R.id.reportTimeValue,
                    R.id.reportDateValue, R.id.reportImgTitleValue, R.id.reportImgPathValue
            };              
            reportDialog.setTitle(REPORT_ALERT_DIALOG_TITLE);
            reportDialog.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,R.drawable.reporticon01);

            TextView reportDialogMSG = (TextView) reportDialog.findViewById(R.id.reportDialogMessageValue);
            Button reportOkBtn = (Button) reportDialog.findViewById(R.id.reportOkBtnID);
            Button reportNavigateBtn = (Button) reportDialog.findViewById(R.id.reportNavigateBtnID);

6 个答案:

答案 0 :(得分:3)

是的,我同意默认对话框标题有时与您应用的主题风格不匹配。

幸运的是,android为您提供了一种更新标题布局的方法,您只需在创建对话框时处理这三行代码。

dialog.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
dialog.setContentView(R.layout.test);
dialog.getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.dialog_title_test);

确保在requestWindowFeature()之后和setFetureInt()之前调用setContentView()

所以,假设对话片段你可以这样做

public class DialogTest extends DialogFragment{

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = super.onCreateDialog(savedInstanceState);
        dialog.requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        dialog.setContentView(R.layout.test);
        dialog.getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.dialog_title_test);
        return dialog;
    }


}

快乐的编码! :)

答案 1 :(得分:1)

将以下内容添加到样式xml文件中:

<style name="FullHeightDialog" parent="android:style/Theme.Dialog">
    <item name="android:windowNoTitle">true</item>
</style>

使用它来创建对话框(根据需要修改并设置按钮的ID)

private void showDialog() {

    final Dialog dialog = new Dialog(this, R.style.FullHeightDialog);
    dialog.setContentView(R.layout.alert_dialog); //replace with your layout xml
    dialog.setCancelable(false);
    Button ignoreButton = (Button) dialog.findViewById(R.id.ignore_button);
    ignoreButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {
            dialog.dismiss();
        }
    });
    Button answerButton = (Button) dialog.findViewById(R.id.answer_button);
    answerButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View view) {

            dialog.dismiss();
        }
    });
    dialog.show();

答案 2 :(得分:1)

最简单的方法是使用此library。我在这篇文章中读到了其他答案,并希望添加类似于他们的代码。您可以在布局文件本身中提供自定义标题栏。看到这段代码:

public void createDialogLanguage() {
    ListView listViewLanguage;
    final Dialog dialog = new Dialog(getActivity());
    dialog.getWindow().addFlags(Window.FEATURE_NO_TITLE);
    dialog.getWindow().setBackgroundDrawableResource(
            android.R.color.transparent);
    dialog.setContentView(R.layout.dialog_language);
    dialog.show();

}

请注意这一行:

dialog.getWindow().setBackgroundDrawableResource(
            android.R.color.transparent);

这会使对话框边框和标题栏透明。如果您要设置带边框(mycase:圆角)的自定义背景以进行适当显示,则非常有用。没有边缘可见。

答案 3 :(得分:0)

要完全自定义Dialog,最好的方法是扩展它并创建自定义对话框。

这将为您提供所需的所有自由,但当然您还需要做更多的工作。

public class MyDialog extends Dialog {

    public MyDialog(Context context) {
        super(context, android.R.style.Theme_Black_NoTitleBar);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setCanceledOnTouchOutside(false);
        setContentView(R.layout.my_dialog_layout);

        TextView title = (TextView) findViewById(R.id.my_dialog_title);
        title.setText(context.getString(R.string.my_dialog_title_text));

        findViewById(R.id.error_dialog_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // do something
                MyDialog.this.dismiss();
            }
        });

        findViewById(R.id.success_dialog_btn).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // do something
                MyDialog.this.dismiss();
            }
        });
    }
}

答案 4 :(得分:0)

试试这个,这肯定会解决你的问题

      AlertDialog.Builder builder = new AlertDialog.Builder(MeetingPointFix.this);


    builder.setCancelable(false);
    LayoutInflater reportAlertDialogLayoutInflater = LayoutInflater.from(getApplicationContext());
    View reportAlertDialogInflatedView = reportAlertDialogLayoutInflater.inflate(R.layout.meetingpointfix_report_dialog, null);
    builder.setView(reportAlertDialogInflatedView);

    int [] viewsRefsIds = {R.id.reportLocNameValue, R.id.reportLocLatValue,   R.id.reportLocLngValue, R.id.reportTimeValue,
            R.id.reportDateValue, R.id.reportImgTitleValue, R.id.reportImgPathValue
    };              

    builder.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,R.drawable.reporticon01);

    TextView reportDialogMSG = (TextView)     reportDialog.findViewById(R.id.reportDialogMessageValue);
    Button reportOkBtn = (Button) reportDialog.findViewById(R.id.reportOkBtnID);
    Button reportNavigateBtn = (Button) reportDialog.findViewById(R.id.reportNavigateBtnID);

reportDialog=builder.create();

答案 5 :(得分:0)

您需要为此创建自定义AlertDialog:

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.dialog_title, null);
        mTitle = (TextView) customTitle.findViewById(R.id.alertTitle);
        mIcon = (ImageView) customTitle.findViewById(R.id.icon);
        setCustomTitle(customTitle);

    }

    @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 setIcon(int drawableResId) {
        mIcon.setImageResource(drawableResId);
        return this;
    }

    @Override
    public CustomAlertDialogBuilder setIcon(Drawable icon) {
        mIcon.setImageDrawable(icon);
        return this;
    }

}

您还需要自定义布局:

RES /布局/ dialog_title.xml

<?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>

希望这有帮助。

您可以在此处找到更多信息: How to change theme for AlertDialog

FYI您的StackOverflow帐户创建时间不到24小时,所以看起来您创建了一个新帐户而不是访问原始帐户。