如何在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);
但上面的代码无效
注意:
我不是在寻找自定义对话框,而是只想使其标题布局自定义。如下
答案 0 :(得分: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();
答案 1 :(得分: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();
关闭对话框