how to include custom title view with in AlertDialog in android?

前端 未结 2 569
暗喜
暗喜 2021-01-04 02:21

how can i include custom titlebar in alertDialog?I know android sdk provide setCustomTitle method but it does\'not work

edit:

    AlertDialog alert =         


        
相关标签:
2条回答
  • 2021-01-04 02:40

    you should not use the .show method with the first line, use the following code it works:

    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 讨论(0)
  • 2021-01-04 02:46
        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);
            }
        }
    

    Add this snippet to your strings.xml

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

    Use dismiss(); to close the dialog

    0 讨论(0)
提交回复
热议问题