简体   繁体   English

自定义对话框中的图标android

[英]Icons in custom dialogs android

Is there a way to set an icon on a custom dialog without using the AlertDialog methods? 有没有办法在不使用AlertDialog方法的情况下在自定义对话框上设置图标? 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? Dialog有标题,但缺少漂亮的分隔符和设置图标的能力,但肯定必须有一种方法来获得两者而不必使用AlertDialog?

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: 对于分隔符,您只需将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>

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: 只需在res/drawable/ catalog中创建一个文件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>

And then inside your LinearLayout you can put a View: 然后在你的LinearLayout中你可以放一个视图:

<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 :) 然后它描绘了一个漂亮的渐变分隔器:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM