【问题标题】:Custom Dialog that looks like a AlertDialog看起来像 AlertDialog 的自定义对话框
【发布时间】:2012-02-06 23:22:37
【问题描述】:

我目前正在创建一个自定义对话框(alertdialog 不够灵活,无法满足我的需要)。我希望它看起来像一个 AlertDialog,带有众所周知的 AlertDialog 标题(见下图)。

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

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

使用自定义对话框应该是什么样子:

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

我的布局非常基本,如下所示:

<?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>

列表视图包含使用此布局的项目

<?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 的解决方法,但现在我遇到了以下问题:

【问题讨论】:

    标签: android dialog android-alertdialog attr


    【解决方案1】:

    您可以使用典型的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,您只需按照自己认为合适的方式重新创建 View 并将其用作我的示例中的 customLayout

    【讨论】:

    • 嗯,标题应该可以工作。但我提供的列表是动态创建的。我不确定您所说的重新创建列表视图项目是什么意思。您能否更准确地解释如何为我将作为 customLayout 提供的自定义列表视图获取警报对话框列表视图的外观?
    • 据我所知,没有可以继承的默认主题或类似的东西。您只需要手动布置项目并设置它们的边距和填充以及字体大小以使其匹配您想要的。仅供参考,您可以在创建AlertDialog 之前动态更改AlertDialog 列表中的项目。为什么这对你不起作用?
    • 它按照你现在的建议工作,除了我仍然遇到一个小错误,我用问题的示例图像编辑了初始问题:我无法摆脱这些差距!
    猜你喜欢
    • 1970-01-01
    • 2012-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-30
    • 1970-01-01
    相关资源
    最近更新 更多