如何在android中的AlertDialog中包含自定义标题视图?

问题描述 投票:16回答:2

我如何在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);

但上面的代码不起作用

注意:我不是在寻找自定义对话框,而是只想使其标题布局自定义。如下所示

android android-alertdialog custom-titlebar
2个回答
55
投票

你不应该在第一行使用.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();

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();关闭对话框

© www.soinside.com 2019 - 2024. All rights reserved.