Existe-t-il un moyen de définir une icône dans une boîte de dialogue personnalisée sans utiliser les méthodes AlertDialog? Dialog a un titre, mais il manque ce diviseur sympa et la possibilité de définir une icône, mais il doit sûrement y avoir un moyen d'obtenir les deux sans avoir à utiliser AlertDialog?Icônes dans les boîtes de dialogue personnalisées android
4
A
Répondre
19
Vous pouvez ajouter une icône avec le code suivant:
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);
Pour un diviseur vous pouvez simplement ajouter un ImageView
à votre mise en page de dialogue:
<?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>
1
Une belle façon d'ajouter un diviseur est en utilisant une forme de gradient.
Il suffit de faire un fichier gradient.xml
ou si, dans votre res/drawable/
catalogue et de mettre quelque chose comme ça en elle:
<?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>
Et puis à l'intérieur de votre LinearLayout vous pouvez mettre une vue:
<View android:id="@+id/divider"
android:layout_width="fill_parent"
android:layout_height="1dip"
android:background="@drawable/gradient">
</View>
Ensuite, il peint un joli diviseur de gradient :)
Gubbei qui ne semble pas fonctionner non plus .... je reçois un 01-24 04: 46: 24.037: ERROR/AndroidRuntime (1693): android.util.AndroidRuntim eException: requestFeature() doit être appelé avant d'ajouter du contenu – Maxrunner
Avez-vous appelé 'requestWindowFeature()' ** avant ** 'setContentView()'? – Gubbel
Merci Gubbel, fonctionne bien pour moi. Note: Il n'est pas nécessaire de définir le Drawable après le 'show()', ce qui est sympa, car normalement le Dialog est créé dans 'onCreateDialog()' tout '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' ' – Ridcully