简体   繁体   中英

Icons in custom dialogs android

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?

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>

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 :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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