很多情况下,我们需要自定义一些Dialog效果。
首先继承系统的Dialog
/** * @author gongchaobin * * 自定义Dialog样式 */ public class SelfDialog extends Dialog implements android.view.View.OnClickListener{ private Context mContext; private Button mBtnCha; private Button mBtnUse; private ActivityManagerCommon managerCommon; public SelfDialog(Context context) { super(context); // TODO Auto-generated constructor stub mContext = context; } public SelfDialog(Context context, boolean cancelable, OnCancelListener cancelListener) { super(context, cancelable, cancelListener); // TODO Auto-generated constructor stub } public SelfDialog(Context context, int theme) { super(context, theme); // TODO Auto-generated constructor stub mContext = context; } @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.dialog); managerCommon = ActivityManagerCommon.getScreenManager(); mBtnCha = (Button) findViewById(R.id.dialog_cha_close); mBtnUse = (Button) findViewById(R.id.dialog_use); mBtnCha.setOnClickListener(this); mBtnUse.setOnClickListener(this); } public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.dialog_cha_close: dismiss(); break; case R.id.dialog_use://跳转到指引界面 dismiss(); Intent intent = new Intent(mContext,GuideActivity.class); mContext.startActivity(intent); break; default: break; } } }
dialog.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="496dp" android:layout_height="334dp" android:background="@drawable/lst_about_bg" android:orientation="vertical" > <Button android:layout_marginLeft="424dp" android:layout_marginTop="20dp" android:layout_width="50dp" android:layout_height="50dp" android:background="@drawable/cha_bg_selector" android:id="@+id/dialog_cha_close"/> <Button android:layout_width="162dp" android:layout_height="46dp" android:id="@+id/dialog_use" android:background="@drawable/use_bg_selector" android:layout_marginLeft="166dp" android:layout_marginTop="106dp" /> </LinearLayout>
同时定义Dialog的一个显示style:
<style name="MyDialog" parent="@android:Theme.Dialog"> <item name="android:windowFrame">@null</item> <item name="android:windowNoTitle">true</item> <item name="android:windowBackground">@drawable/lst_about_bg</item> <item name="android:windowIsFloating">true</item> <item name="android:windowContentOverlay">@null</item> </style>