1 MyDialog.java
import android.app.Dialog; import android.content.Context; import android.os.Bundle; import android.view.View; import android.widget.Button; public abstract class MyDialog extends Dialog { Context context; public Button bt_ok = null; public Button bt_cancel = null; public MyDownloadDialog(Context context) { super(context); this.context = context; } public MyDownloadDialog(Context context, int theme){ super(context, theme); this.context = context; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setContentView(R.layout.dialog_download); bt_ok = (Button)findViewById(R.id.dialog_button_ok); bt_cancel = (Button)findViewById(R.id.dialog_button_cancel); bt_ok.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { click_ok(); dismiss(); } }); bt_cancel.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dismiss(); } }); } public abstract void click_ok(); }
2 Activity.java中對MyDialog的引用
//初始化一个自定义的Dialog MyDownloadDialog dialog = new MyDownloadDialog(context, R.style.MyDialog) { @Override public void click_ok() { videoList.remove(position); } }; dialog.show();3 /values/MyDialog22.xml中代碼如下
<?xml version="1.0" encoding="utf-8"?> <resources> <style name="MyDialog"> <item name="android:windowFrame">@null</item> <item name="android:windowNoTitle">true</item> <item name="android:windowIsFloating">true</item> <item name="android:windowContentOverlay">@null</item>
<!-- 去掉對話框邊框的棱角,即顯示的是弧度角,一定要加這句話--> <item name="android:windowBackground">@android:color/transparent</item> </style> </resources>
//4 dialog_download.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_vertical|center_horizontal" android:background="@drawable/border_corner_my_download2" > <RelativeLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingLeft="30dip" android:paddingTop="10dip"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="提示" android:textColor="#C5C5C5" android:textSize="20sp"/> </RelativeLayout> <TextView android:layout_width="fill_parent" android:layout_height="80dp" android:text="是否刪除該視頻" android:layout_marginLeft="72dp" android:layout_marginRight="72dp" android:gravity="center" android:textColor="#000000" /> <LinearLayout android:layout_width="match_parent" android:orientation="vertical" android:layout_height="wrap_content"> <TextView android:layout_width="match_parent" android:layout_height="1dip" /> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="bottom|center_horizontal" android:orientation="horizontal" > <Button android:id="@+id/dialog_button_ok" android:layout_width="0dip" android:layout_weight="1" android:textColor="#E30A46" android:layout_height="wrap_content" android:background="@drawable/border_right2" android:text="确定刪除" /> <Button android:id="@+id/dialog_button_cancel" android:layout_width="0dip" android:layout_weight="1" android:layout_height="wrap_content" android:background="@drawable/border_left2" android:text="取消"/> </LinearLayout> </LinearLayout> </LinearLayout>
//5 border_corner_my_download2.xml //把dialog的邊角變成弧形,同時加上上面的紅色句子
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <corners android:bottomLeftRadius="7dp" android:bottomRightRadius="7dp" android:radius="0.1dp" android:topLeftRadius="7dp" android:topRightRadius="7dp" /> <solid android:color="#FFFFFF" /> <stroke android:width="1dp" android:color="#E8E6E7" /> </shape>
// 6 border_right2.xml 設置刪除按鈕的弧度以及上邊框和右邊框的顏色為灰色【即顯示上邊框和右邊框】
<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item> <shape> <solid android:color="#eeeeee"/> <corners android:bottomLeftRadius="7dp" android:bottomRightRadius="7dp" /> </shape> </item> <!-- 主体背景颜色值 在item中的top与bottom各指定了1dp的边框线--> <item android:top="1dp" android:bottom="0dp" android:left="0dp" android:right="0.5dp"> <shape> <solid android:color="@color/white"/> <corners android:bottomLeftRadius="7dp" android:bottomRightRadius="7dp" /> </shape> //可以加padding属性,是该组件内容距该组件边框的填充区域 <!--<padding android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp" />--> </item> </layer-list>
//7 border_left2.xml 設置取消按鈕的弧度以及上邊框和左邊框的顏色為灰色【即顯示上邊框和左邊框】
<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item> <shape> <solid android:color="#eeeeee"/> <corners android:bottomLeftRadius="7dp" android:bottomRightRadius="7dp" /> </shape> </item> <!-- 主体背景颜色值 在item中的top与bottom各指定了1dp的边框线--> <item android:top="1dp" android:bottom="0dp" android:left="0.5dp" android:right="0dp"> <shape> <solid android:color="@color/white"/> <corners android:bottomLeftRadius="7dp" android:bottomRightRadius="7dp" /> </shape> //可以加padding属性,是该组件内容距该组件边框的填充区域 <!--<padding android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp" />--> </item> </layer-list>