自定义Dialog 和Shape
自定义的Dialog需要先建立一个drawable放Shape来设置dialog的颜色、边框、和圆角,以下是几个属性
corners圆角率
<corners
android:bottomLeftRadius="10dp"左下角为半径为10dp的圆角
android:topLeftRadius="10dp"左上角为半径为10dp的圆角
android:topRightRadius="10dp"右上角为半径为10dp的圆角
android:bottomRightRadius="10dp"右下角为半径为10dp的圆角
android:radius="10dp" 四角都为半径为10dp的圆角
/>
solid Dialog背景颜色
<solid android:color="#BFEFFF"/>
stroke android:color边框颜色 android:width边框大小
<stroke android:color="#9AFF9A" android:width="1dp"/>
若要设置dialog背景也为圆角,需要在styles中设置dialog的theme,注意的是在dialog中添加theme
<style parent="@android:Theme.Dialog" name="NoDialogTitle">
<item name="android:windowFrame">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">@null</item>
</style>
自定义的activity_dialog_define.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@mipmap/image_girlsmall"
>
<TextView
android:id="@+id/text_dialog_define_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="这是自定义标题"
android:textColor="#50ff0000"
android:gravity="center"
android:padding="10dp"
android:background="@drawable/bac_dialog_define"/>
<TextView
android:id="@+id/text_dialog_define_massage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="这是自定义的内容"
android:gravity="center"
android:padding="20dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btn_dialog_cannel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="cannel"
android:background="@drawable/bac_dialog_btn_anncel"/>
<Button
android:id="@+id/btn_dialog_sure"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="OK"
android:background="@drawable/bac_dialog_sure"/>
</LinearLayout>
</LinearLayout>
修改的主题styles
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
<style parent="@android:Theme.Dialog" name="NoDialogTitle">
<item name="android:windowFrame">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">@null</item>
</style>
</resources>
设置背景圆角以及按钮形状的drawable
bac_dialog_defien.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:topRightRadius="10dp" android:topLeftRadius="10dp"/>
<stroke android:color="#FFFACD" android:width="1dp"/>
<solid android:color="#FFA54F" />
</shape>
bac_dialog_btn_annel.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<corners android:bottomLeftRadius="10dp"
/>
<solid android:color="#BFEFFF"/>
<stroke android:color="#9AFF9A" android:width="1dp"/>
&