这篇文章将为大家详细讲解有关怎么在Android应用中自定义dialog,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。
Android 自定义dialog
要点:
设置自定义dialog的布局文件my_dialog.xml
2、设置一份自定义的样式文件styles_wx.xml,该文件用于覆盖Android的默认主题样式,如黑色边框等。
3、Java代码继承Dialog实现自定义类MyDialog,实现自定义布局,还有设置窗口的大小、位置等。
代码
Part1.styles_wx.xml
<?xml version="1.0" encoding="utf-8"?> <resources xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 微信弹窗 --> <style name="wx_dialog" parent="@android:style/Theme.Dialog"> <item name="android:windowFrame">@null</item> <!-- 边框 --> <item name="android:windowIsFloating">true</item> <!-- 是否浮现在activity之上 --> <item name="android:windowIsTranslucent">false</item> <!-- 半透明 --> <item name="android:windowNoTitle">true</item> <!-- 无 --> <item name="android:windowBackground">@drawable/transparent</item> <!-- 自己想要的背景 --> <item name="android:backgroundDimEnabled">true</item> <!-- 背景内容模糊 --> </style> </resources>
注意,此处:
<item name="android:windowBackground">@drawable/transparent</item>
这是设置对话框弹出背景,尝试设置@null,仍然是黑色背景,在使用半透明图片时会受其影响。
所以,可以在这里指定你想要的背景图片或者颜色。
我为了灵活性,我设置该属性为名为"transparent"的图片,这是一张1*1的透明图片。这样背景完全透明,真正使用的背景在my_dialog.xml里定义
Part2.my_dialog.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="wrap_content" android:background="@drawable/frame_white" android:orientation="vertical" android:padding="15dp" > <TextView android:id="@+id/tvTitle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/systitle" android:textSize="18sp" /> <!-- 分隔符用 --> <TextView android:id="@+id/tvSeparator" android:layout_marginBottom="5dp" android:layout_marginTop="5dp" /> <TextView android:id="@+id/tvText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/sysText" android:textSize="18sp" /> <!-- 该RelativeLayout作为子视图容器 --> <RelativeLayout android:id="@+id/rlContent" android:layout_width="match_parent" android:layout_height="wrap_content" > </RelativeLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="10dp" android:orientation="horizontal" > <TextView android:id="@+id/tvButton2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_marginLeft="10dp" android:background="@drawable/selector_text_button" android:clickable="true" android:paddingBottom="5dp" android:paddingLeft="15dp" android:paddingRight="15dp" android:paddingTop="5dp" android:text="@string/ok" android:textColor="@color/wx_text_link" android:textSize="14sp" android:visibility="visible" /> <TextView android:id="@+id/tvButton1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignWithParentIfMissing="true" android:layout_marginLeft="10dp" android:layout_toLeftOf="@id/tvButton2" android:background="@drawable/selector_text_button" android:clickable="true" android:paddingBottom="5dp" android:paddingLeft="15dp" android:paddingRight="15dp" android:paddingTop="5dp" android:text="@string/cancel" android:textColor="@color/wx_text_link" android:textSize="14sp" /> </RelativeLayout> </LinearLayout>
Part3.MyDialog.java
package com.kwws; import android.app.Dialog; import android.content.Context; import android.os.Bundle; import android.util.DisplayMetrics; import android.view.LayoutInflater; import android.view.View; import android.view.Window; import android.view.WindowManager; import android.widget.RelativeLayout; import android.widget.TextView; import com.navigator.R; public class MyDialog extends Dialog { // 数据 String title = "title"; String text = "text"; String cancelButtonText = "cancel"; String okButtonText = "ok"; int okColor = -1; int cancelColor = -1; // UI Context mContent; TextView tvTitle; TextView tvSeparator; TextView tvText; TextView tvBtn1; TextView tvBtn2; RelativeLayout childViewWrapper;// 子组件容器 public MyDialog(Context context, String title, String text, String cancelButtonText, String okButtonText) { super(context, R.style.wx_dialog);// 样式定义,该样式去除android默认的黑色背景边框等。 this.mContent = context; setDialogStyle(title, text, cancelButtonText, okButtonText); } @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); LayoutInflater layout = LayoutInflater.from(mContent); View view = layout.inflate(R.layout.my_dialog, null); setContentView(view); findView(); initView(); initViewEvent(); } private void setDialogStyle(String title, String text, String cancelButtonText, String okButtonText) { this.title = title; this.text = text; this.cancelButtonText = cancelButtonText; this.okButtonText = okButtonText; } private void findView() { tvTitle = (TextView) findViewById(R.id.tvTitle); tvSeparator = (TextView) findViewById(R.id.tvSeparator); tvText = (TextView) findViewById(R.id.tvText); tvBtn1 = (TextView) findViewById(R.id.tvButton1); tvBtn2 = (TextView) findViewById(R.id.tvButton2); childViewWrapper = (RelativeLayout) findViewById(R.id.rlContent); } private void initView() { if (title == null) { tvTitle.setVisibility(View.GONE); tvSeparator.setVisibility(View.GONE); } else { tvTitle.setVisibility(View.VISIBLE); tvSeparator.setVisibility(View.VISIBLE); tvTitle.setText(title); } if (text == null) { tvText.setVisibility(View.GONE); } else { tvText.setVisibility(View.VISIBLE); tvText.setText(text); } if (cancelButtonText == null) { tvBtn1.setVisibility(View.GONE); } else { tvBtn1.setVisibility(View.VISIBLE); tvBtn1.setText(cancelButtonText); if (cancelColor != -1) { tvBtn1.setTextColor(cancelColor); } } if (okButtonText == null) { tvBtn2.setVisibility(View.GONE); } else { tvBtn2.setVisibility(View.VISIBLE); tvBtn2.setText(okButtonText); if (okColor != -1) { tvBtn2.setTextColor(okColor); } } if (childViewWrapper != null && childView != null) { childViewWrapper.addView(childView); } // 设置对话框大小 Window dialogWindow = getWindow(); WindowManager.LayoutParams lp = dialogWindow.getAttributes(); DisplayMetrics d = mContent.getResources().getDisplayMetrics(); // 获取屏幕宽、高用 lp.width = (int) (d.widthPixels * 0.8); // 高度设置为屏幕的0.8 dialogWindow.setAttributes(lp); } private void initViewEvent() { View.OnClickListener listener = new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.tvButton1: if (mListener != null) { mListener.onCancelButtonClick(MyDialog.this, v); } break; case R.id.tvButton2: if (mListener != null) { mListener.onOKButtonClick(MyDialog.this, v); } break; default: break; } } }; tvBtn1.setOnClickListener(listener); tvBtn2.setOnClickListener(listener); } public interface OnDialogButtonClickListener { void onCancelButtonClick(MyDialog dialog, View view); void onOKButtonClick(MyDialog dialog, View view); } private OnDialogButtonClickListener mListener; public void setOnDialogButtonClickListener( OnDialogButtonClickListener listener) { this.mListener = listener; } public void setOKButtonTextColor(int color) { this.okColor = color; } public void setCancelButtonTextColor(int color) { this.cancelColor = color; } View childView; public void setChildView(View view) { childView = view; } }
Part4.使用
// 退出提示框 public void exitDialog() { MyDialog dialog = new MyDialog(this, "提示", "确认退出?", "取消", "退出"); dialog.setOKButtonTextColor(getResources().getColor(R.color.red)); dialog.setOnDialogButtonClickListener(new OnDialogButtonClickListener() { @Override public void onOKButtonClick(MyDialog dialog, View view) { finish(); } @Override public void onCancelButtonClick(MyDialog dialog, View view) { dialog.dismiss(); } }); dialog.show(); }
private void showConfigDialog() { // 读取参数 final SharedPreferencesHelper helper = new SharedPreferencesHelper( this, "config"); final String ip = helper.getValue("serverip"); final String port = helper.getValue("serverport"); // 配置界面 输入IP和端口的简单界面,这里就不附xml了 View view = getLayoutInflater().inflate(R.layout.dialog_config, null); final EditText etIP = (EditText) view.findViewById(R.id.etIP); final EditText etPort = (EditText) view.findViewById(R.id.etPort); etIP.setText(ip != null ? ip : Ksoap2Helper.getServerIP()); etPort.setText(port != null ? port : String.valueOf(Ksoap2Helper .getServerPort())); // 配置对话框 MyDialog dialog = new MyDialog(this, "参数配置", null, "取消", "确定"); // 添加配置界面到对话框 dialog.setChildView(view); // 按钮监听 dialog.setOnDialogButtonClickListener(new OnDialogButtonClickListener() { @Override public void onOKButtonClick(MyDialog dialog, View view) { // 保存配置 String newIP = etIP.getText().toString(); String newPort = etPort.getText().toString(); if (true) { helper.putValue("serverip", newIP); Ksoap2Helper.setServerIP(newIP); } if (true) { helper.putValue("serverport", newPort); Ksoap2Helper.setServerPort(Integer.valueOf(newPort)); } dialog.dismiss(); } @Override public void onCancelButtonClick(MyDialog dialog, View view) { dialog.dismiss(); } }); dialog.show(); }
关于怎么在Android应用中自定义dialog就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。