Android 自定义对话框中的图标

发布于 2024-10-06 19:28:11 字数 112 浏览 0 评论 0原文

有没有一种方法可以在不使用 AlertDialog 方法的情况下在自定义对话框上设置图标? 对话框有标题,但缺少漂亮的分隔线和设置图标的功能,但肯定有一种方法可以同时获得两者而不必使用 AlertDialog?

Is there a way to set an icon on a custom dialog without using the AlertDialog methods?
Dialog has title, but is missing that nice divider and the ability to set an icon, but surely there must be a way of getting both without having to use AlertDialog?

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

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

发布评论

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

评论(2

萌无敌 2024-10-13 19:28:11

您可以使用以下代码添加图标:

Dialog dialog = new Dialog(context);

dialog.requestWindowFeature(Window.FEATURE_LEFT_ICON);
dialog.setContentView(R.layout.custom_dialog);
dialog.setTitle("Dialog Title");

dialog.show();
dialog.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.your_icon);

对于分隔线,您可以简单地将 ImageView 添加到对话框布局中:

<?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="wrap_content"
    android:orientation="vertical">

    <ImageView
        android:src="@android:drawable/divider_horizontal_dim_dark"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:text="content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

You can add an icon with the following code:

Dialog dialog = new Dialog(context);

dialog.requestWindowFeature(Window.FEATURE_LEFT_ICON);
dialog.setContentView(R.layout.custom_dialog);
dialog.setTitle("Dialog Title");

dialog.show();
dialog.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.your_icon);

For a divider you can simply add an ImageView to your dialog 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="wrap_content"
    android:orientation="vertical">

    <ImageView
        android:src="@android:drawable/divider_horizontal_dim_dark"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:text="content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>
野稚 2024-10-13 19:28:11

添加分隔线的一个好方法是使用渐变形状。

只需在您的 res/drawable/ 目录中创建一个文件 gradient.xml 左右,然后将类似的内容放入其中:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
 android:shape="rectangle">

<gradient android:startColor="#424542" 
          android:centerColor="#FFFFFF"
          android:endColor="#424542" 
          android:angle="0" />
</shape>

然后在 LinearLayout 中您可以放置​​一个视图:

<View android:id="@+id/divider" 
      android:layout_width="fill_parent"
      android:layout_height="1dip"
      android:background="@drawable/gradient">
</View>

然后它绘制了一个漂亮的渐变分隔线:)

A nice way of adding a divider is by using a gradient shape.

Simply make a file gradient.xml or so, in your res/drawable/ catalog and put something like this into it:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
 android:shape="rectangle">

<gradient android:startColor="#424542" 
          android:centerColor="#FFFFFF"
          android:endColor="#424542" 
          android:angle="0" />
</shape>

And then inside your LinearLayout you can put a View:

<View android:id="@+id/divider" 
      android:layout_width="fill_parent"
      android:layout_height="1dip"
      android:background="@drawable/gradient">
</View>

Then it paints a nice gradient divider :)

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