[英]Icons in custom dialogs android
有沒有辦法在不使用AlertDialog方法的情況下在自定義對話框上設置圖標? Dialog有標題,但缺少漂亮的分隔符和設置圖標的能力,但肯定必須有一種方法來獲得兩者而不必使用AlertDialog?
您可以使用以下代碼添加圖標:
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>
添加分隔符的一種好方法是使用漸變形狀。
只需在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>
然后在你的LinearLayout中你可以放一個視圖:
<View android:id="@+id/divider"
android:layout_width="fill_parent"
android:layout_height="1dip"
android:background="@drawable/gradient">
</View>
然后它描繪了一個漂亮的漸變分隔器:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.