gpt4 book ai didi

android - 看起来像 AlertDialog 的自定义对话框

转载 作者:搜寻专家 更新时间:2023-11-01 07:37:18 24 4
gpt4 key购买 nike

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

我的自定义对话框中还有一个 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>

有什么想法吗?

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

gapsproblem

最佳答案

您可以使用典型的 AlertDialog 标题/标题创建一个普通的 AlertDialog,然后将对话框的内容设置为使用自定义 View :

// 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,其标题看起来很正常,但内容将使用您想要的任何自定义 View 。

对于要显示为内容的 ListView,您只需重新创建您认为合适的 View ,并将其用作我示例中的 customLayout

关于android - 看起来像 AlertDialog 的自定义对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9168854/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com