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

发布于 2025-01-03 10:48:31 字数 2012 浏览 3 评论 0原文

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

我的自定义对话框中还有一个列表视图,我希望它看起来像 AlertDialog 的列表视图。我怎么能那样做呢?

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

使用自定义对话框应该是什么样子: 典型的带有列表视图的alertdialog

它的外观(黑色背景,错误的标题): 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>

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

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

i'm currently creating a custom dialog (alertdialog is not flexible enough for my needs). and i want it to look like a AlertDialog, with that well kown AlertDialog header (see image below).

also i got a listview within my custom dialog and i want it to look like a listview of the AlertDialog. how could i do that?

so far i found alertDialogTheme and alertDialogStyle in the attr. class, but i don't know how to use it.

what it should look like using a custom dialog:
typical alertdialog with listview

what it looks like (black background, wrong header): custom

my layouts are pretty basic and look like this:

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

the listview contains items that use this layout

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

any ideas?

---> workaround using alerdialog with custom view, but now i got the following problem with some gaps:

gapsproblem

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

扮仙女 2025-01-10 10:48:32

您可以使用典型的 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 即可。

You can create a normal AlertDialog using the typical AlertDialog title/header but then set the content of the dialog to use a custom 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();

This will result in a normal looking AlertDialog with the normal looking title, but the content will use whatever custom view you want.

For the ListView you want to display as the content, you are simply going to need to recreate the View as you see fit and use it as the customLayout in my example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文