自定义对话框,外观类似于AlertDialog

3
我目前正在创建一个自定义对话框(AlertDialog 对我的需求不够灵活)。我希望它看起来像 AlertDialog,具有众所周知的 AlertDialog 标头(请参见下面的图像)。
同时,在我的自定义对话框中,我有一个 ListView,我希望它看起来像 AlertDialog 的 ListView。我该如何做?
到目前为止,我在 attr 类中找到了 alertDialogTheme 和 alertDialogStyle,但我不知道如何使用它们。
使用自定义对话框时应该是这个样子: typical alertdialog with listview 它的样子是这样的(黑色背景,错误的标头):custom 我的布局非常基本,看起来像这样:
<?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>

这个 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>

你有什么想法吗?

---> 使用带有自定义视图的alerdialog的解决方法,但现在我遇到了一些间隙问题:

gapsproblem

1个回答

6
您可以使用典型的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();

这将生成一个外观常规的AlertDialog,具有常规外观的标题,但内容将使用您想要的任何自定义视图。
对于作为内容显示的ListView,您只需要根据需要重新创建视图,并将其用作我的示例中的customLayout

嗯,标题应该能正常工作。但是我提供的列表是动态创建的。我不确定您所说的重新创建listview项的意思。您能更加详细地解释一下如何为我的自定义列表视图(以customLayout形式提供)获取AlertDialog列表视图的外观吗? - omni
1
据我所知,没有默认的主题可以继承或类似的东西。您只需要手动布置项目并设置它们的边距、填充和字体大小,以使其与您想要的匹配。顺便说一下,在创建“AlertDialog”之前,您可以动态地更改列表中的项目。为什么这对您不起作用呢? - Jake Wilson
现在它按照你的建议工作,除了一个小错误之外,我仍然有问题。我已经在初始问题中编辑了一个示例图像来说明问题:我无法摆脱那些间隙! - omni

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接