Android自定义Dialog及其布局
实际项目开发中默认的Dialog无法满足需求,需要自定义Dialog及其布局,并响应布局中控件的事件。
上效果图:
自定义Dialog,LogoutDialog:
要将自定义布局传入构造函数中,才能在Activity中通过 dialog.findviewbyid 获取到控件,否则返回null。
public class LogoutDialog extends Dialog{
Context context;
public LogoutDialog(Context context) {
super(context);
this.context=context;
this.setContentView(R.layout.logout_dialog);
}
public LogoutDialog(Context context, int theme){
super(context, theme);
this.context = context;
this.setContentView(R.layout.logout_dialog);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//this.setContentView(R.layout.logout_dialog);
}
}
自定义布局文件,logout_dialog.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@android:color/transparent"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/dialog_top_bg"
android:orientation="vertical"
android:paddingTop="10dp"
>
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/pop_icon1"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="10dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="是否确认注销账号"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="20dp"
android:textColor="#FF3C25"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:gravity="center_vertical"
android:background="@drawable/dialog_buttom_bg"
>
<TextView
android:id="@+id/back_btn_dialog"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="返回"
android:layout_weight="1"
android:textColor="#FFFFFF"
android:gravity="center"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="|"
android:textColor="#C9CACC"
android:gravity="center"
/>
<TextView
android:id="@+id/submit_btn_dialog"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="确定"
android:layout_weight="1"
android:textColor="#FFFFFF"
android:gravity="center"
/>
</LinearLayout>
</LinearLayout>
两个Shape的布局,dialog_buttom_bg.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
>
<corners
android:bottomLeftRadius="@dimen/dialog_corners"
android:bottomRightRadius="@dimen/dialog_corners"
/>
<gradient
android:startColor="#FF3E25"
android:endColor="#FF3E25"
android:centerColor="#FF3E25"
android:angle="270"
/>
</shape>
dialog_top_bg.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<corners
android:topLeftRadius="@dimen/dialog_corners"
android:topRightRadius="@dimen/dialog_corners"
/>
<gradient
android:startColor="#ffffff"
android:endColor="#ffffff"
android:centerColor="#ffffff"
android:angle="270"
/>
</shape>
自定义样式Style,来改变默认的Dialog样式。在values/styles.xml下新加样式:
<style name="UpdateErrorFinishDialog" parent="@android:style/Theme.Dialog">
<item name="android:windowFrame">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">@drawable/dialog_background</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">@null</item>
</style>
在Activity中调用:
Dialog dialog==new LogoutDialog(TempActivity.this, R.style.UpdateErrorFinishDialog);
dialog.setCanceledOnTouchOutside(false);
dialog.show();
TextView submit_btn_dialog=(TextView) dialog.findViewById(R.id.submit_btn_dialog);
TextView back_btn_dialog=(TextView) dialog.findViewById(R.id.back_btn_dialog);
submit_btn_dialog.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Toaster.showLongToast("确定");
dialog.dismiss();
}
});
back_btn_dialog.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Toaster.showLongToast("返回");
dialog.dismiss();
}
});
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。