自定义对话框,看起来像AlertDialog

mas*_*asi 2 android dialog attr android-alertdialog

我正在创建一个自定义对话框(alertdialog不够灵活,不能满足我的需求).我希望它看起来像一个AlertDialog,具有良好的已知的AlertDialog标题(见下图).

我在自定义对话框中也有一个列表视图,我希望它看起来像AlertDialog的列表视图.我怎么能这样做?

到目前为止,我在attr中找到了alertDialogTheme和alertDialogStyle.上课,但我不知道如何使用它.

使用自定义对话框应该是什么样子: listview的典型alertdialog

看起来像什么(黑色背景,错误的标题): 习惯

我的布局很基本,看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout_root"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="10dp" >

    <ListView
        android:id="@+id/list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

listview包含使用此布局的项目

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:baselineAligned="false"
    android:gravity="center_vertical"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:src="@drawable/icon_noimage" />

    <TextView
        android:id="@+id/text"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:layout_weight="1"
        android:ellipsize="end"
        android:singleLine="true"
        android:textColor="#ffffff"
        android:textSize="18dp" />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

--->使用自定义视图的alerdialog解决方法,但现在我遇到了以下问题:

gapsproblem

Jak*_*son 5

您可以AlertDialog使用典型的AlertDialog标题/标题创建法线,但是然后将对话框的内容设置为使用自定义视图:

// Your custom layout can be whatever you want
View customLayout = getLayoutInflater().inflate(R.layout.customLayout, null);

// Create the dialog box
AlertDialog myDialog = new AlertDialog.Builder(this)
  .setTitle("My Title")
  .setView(customLayout)
  .create();

// Show the dialog box
myDialog.show();
Run Code Online (Sandbox Code Playgroud)

这将导致看起来AlertDialog正常的标题正常,但内容将使用您想要的任何自定义视图.

对于要显示为内容的ListView,您只需要根据需要重新创建视图,并customLayout在我的示例中使用它.