17

如何在 alertDialog 中包含自定义标题栏?我知道 android sdk 提供 setCustomTitle 方法,但它不起作用

编辑:

    AlertDialog alert = new AlertDialog.Builder(this).setTitle("Test").setMessage("hello").show();
    View view=alert.getLayoutInflater().inflate(R.layout.titlebar, null);
    alert.setCustomTitle(view);

但上面的代码不起作用

注意: 我不是在寻找自定义对话框,而只是想自定义其标题布局。如下所示像这样用关闭按钮

4

2 回答 2

57

您不应该在第一行使用 .show 方法,请使用以下代码:

AlertDialog.Builder alert = new AlertDialog.Builder(this);
LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.titlebar, null);
alert.setCustomTitle(view);
alert.setMessage("helo");
alert.show();
于 2012-02-24T12:33:51.427 回答
0
    DisplayDialog d = new DisplayDialog(this);
    d.show();

    public class DisplayDialog extends Dialog implements android.view.View.OnClickListener
    {
        public DisplayDialog(Context c) 
        {
            super(c, R.style.Theme_Dialog_Translucent);
        }

        @Override
        protected void onCreate(Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState);
            requestWindowFeature(Window.FEATURE_NO_TITLE);

            setCanceledOnTouchOutside(false);
            setContentView(R.layout.your_layout);
        }
    }

将此片段添加到您的strings.xml

 <style name="Theme_Dialog_Translucent" parent="android:Theme.Dialog">
     <item name = "android:windowBackground">@color/transparent</item>
 </style>
 <color name="transparent">#00000000</color> 

用于dismiss();关闭对话框

于 2012-02-24T12:01:45.400 回答