【问题标题】:how to include custom title view with in AlertDialog in android?如何在 android 的 AlertDialog 中包含自定义标题视图?
【发布时间】:2012-03-14 20:56:36
【问题描述】:

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

但上面的代码不起作用

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

【问题讨论】:

  • 这将显示您如何创建自定义对话框developer.android.com/guide/topics/ui/dialogs.html#CustomDialog
  • 我知道如何创建自定义对话框,但我不想要任何自定义对话框,只需要自定义标题,这样我就可以在其标题中添加按钮,无论如何谢谢
  • 不工作是什么意思?它给出了一些错误吗?或者根本不显示自定义标题?我认为代码是正确的,它应该可以工作。
  • @Vikram 显示默认标题栏。

标签: android android-alertdialog custom-titlebar


【解决方案1】:

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

【讨论】:

  • getLayoutInflater() 在 onCreateDialog 中使我的片段崩溃。 LayoutInflater.from(getActivity()) 有效。
【解决方案2】:
    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);
        }
    }

将此 sn-p 添加到您的 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();关闭对话框

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-17
    • 2013-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-24
    相关资源
    最近更新 更多