我正在尝试在Android中生成自定义对话框.我像这样创建我的Dialog:
dialog = new Dialog(this);
dialog.setContentView(R.layout.my_dialog);
Run Code Online (Sandbox Code Playgroud)
除了Dialog的标题之外,Everythings工作正常.即使我没有设置对话框的标题,对话框弹出窗口在对话框的位置也有一个空格.
有没有办法隐藏Dialog的这一部分?
我尝试使用AlertDialog,但似乎布局设置不正确:
LayoutInflater inflater =
(LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.map_dialog, null);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(view);
// dialog = new Dialog(this);
// dialog.setContentView(R.layout.map_dialog);
dialog = builder.create();
((TextView) dialog.findViewById(R.id.nr)).setText(number);
Run Code Online (Sandbox Code Playgroud)
如果我使用这段代码,我会在最后一行得到一个空指针异常.该对话框不为null,因此我尝试检索的TextView不存在.
如果我取消注释我使用Dialog构造函数的部分,一切正常,但对于我的对话框布局上方的标题.
我想更改Alert对话框的背景.我试过以下代码片段:
<style name="Theme.AlertDialog" parent="@android:style/Theme.Holo.Light.Dialog">
<item name="android:background">@android:color/transparent</item>
<item name="android:textColor">#659F26</item>
<item name="android:windowTitleStyle">@style/Theme.AlertDialog.Title</item>
</style>
<style name="Theme.AlertDialog.Title" parent="@android:style/TextAppearance.DialogWindowTitle">
<item name="android:background">@drawable/cab_background_top_example</item>
</style>
Run Code Online (Sandbox Code Playgroud)
Java代码:
ContextThemeWrapper ctw = new ContextThemeWrapper( this, R.style.Theme_AlertDialog);
final AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(ctw);
alertDialogBuilder.setTitle(getResources().getString(R.string.confirmation));
alertDialogBuilder.setMessage(getResources().getString(R.string.msg));
alertDialogBuilder.show();
Run Code Online (Sandbox Code Playgroud)
我的应用程序显示如下对话框:

虽然我希望它看起来像:

请建议,我做错了什么.