Android初级知识--自定义任何样式的对话框

来源:互联网 发布:手机淘宝销量排名 编辑:程序博客网 时间:2024/06/09 15:03

概述

  • 对话框也是Android开发中必不可少的控件,系统提供的对话框样式丑,也很单一,无法满足开发中实际的需求,这里提供一种思路,可以自定义出任何样式的对话框;

正文

效果图

  • 首先看一下效果图:
    这里写图片描述
  • 当点击按钮时,我们弹出的对话框如下:
    这里写图片描述

正文

  • 进入主题,我们先完成对话框的布局文件:
    custom_dialog.xml文件如下:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="#fff">    <TextView        android:id="@+id/tv_title"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:background="#0099CC"        android:gravity="center"        android:padding="12dp"        android:text="标题"        android:textColor="#000"        android:textSize="17sp" />    <TextView        android:id="@+id/tv_message"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_below="@+id/tv_title"        android:background="#ffffff"        android:gravity="center"        android:padding="14dp"        android:text="提示信息"        android:textColor="#222222"        android:textSize="15sp" />    <View        android:layout_width="match_parent"        android:layout_height="0.1dp"        android:layout_above="@+id/llButtons"        android:layout_marginTop="10dp"        android:background="#FF9900" />    <LinearLayout        android:id="@+id/llButtons"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_alignParentBottom="true"        android:background="#006633"        android:orientation="horizontal">        <TextView            android:id="@+id/tv_confirm"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1"            android:gravity="center"            android:padding="12dp"            android:text="确定"            android:textColor="#CCCC99"            android:textSize="15sp" />        <View            android:layout_width="2dp"            android:layout_height="match_parent"            android:background="#f00" />        <TextView            android:id="@+id/tv_cancel"            android:layout_width="0dp"            android:layout_height="wrap_content"            android:layout_weight="1"            android:gravity="center"            android:padding="14dp"            android:text="取消"            android:textColor="#CCCC99"            android:textSize="15sp" />    </LinearLayout></RelativeLayout>
  • 接着继承AlertDialog类自定义对话框;
    CustomAlertDialog.java类如下:
public abstract class CustomAlertDialog extends AlertDialog implements View.OnClickListener {    private TextView tv_title;    private TextView tv_message;    private TextView tv_confirm;    private TextView tv_cancel;    //接受具体信息    private String title;    private String message;    private String confirm;    private String cancel;    private Context context;    protected CustomAlertDialog(Context context) {        super(context);        this.context=context;    }    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        //加载自定义布局        setContentView(R.layout.custom_dialog);        //初始化控件        tv_title = (TextView) findViewById(R.id.tv_title);        tv_message = (TextView) findViewById(R.id.tv_message);        tv_confirm = (TextView) findViewById(R.id.tv_confirm);        tv_cancel = (TextView) findViewById(R.id.tv_cancel);        //填入数据        tv_title.setText(title);        tv_message.setText(message);        tv_confirm.setText(confirm);        tv_cancel.setText(cancel);        //两个按钮的点击事件        tv_confirm.setOnClickListener(this);        tv_cancel.setOnClickListener(this);    }    @Override    public void show() {        super.show();        //在这里也可以设置对话框体大小        /*int width = getDeviceWidth((Activity) context);        setDialogSize(width * 4 / 5, width * 2 / 4);*/    }    /**     * 修改框体大小     */    public void setDialogSize(int width, int height) {        WindowManager.LayoutParams params=getWindow().getAttributes();        params.width=width;        params.height=height;        this.getWindow().setAttributes(params);    }    /**     * 设置标题     */    public void setTitle(String title){        this.title=title;    }    /**     * 设置信息     */    public void setMessage(String message){        this.message=message;    }    /**     * 设置左边的按钮     */    public void setConfirm(String confirm){        this.confirm=confirm;    }    /**     * 设置右边的按钮     */    public void setCancel(String cancel){        this.cancel=cancel;    }    /**     * 点击事件的两个抽象方法 ,子类必须实现     */    public abstract void onDialogConfirmClick();    public abstract void onDialogCancelClick();    @Override    public void onClick(View v) {        switch (v.getId()){            case R.id.tv_confirm:                onDialogConfirmClick();                break;            case R.id.tv_cancel:                onDialogCancelClick();                break;        }    }    /**     * 获取屏幕宽度     */    public static int getDeviceWidth(Activity activity) {        DisplayMetrics dm = new DisplayMetrics();        activity.getWindowManager().getDefaultDisplay().getMetrics(dm);        return dm.widthPixels;    }}
  • 最后就是使用这个对话框;
    MainActivity.java文件如下:
public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);    }    /**     * 弹出对话框按钮点击事件     */    public void showDialog(View view) {        CustomAlertDialog dialog = new CustomAlertDialog(this) {            @Override            public void onDialogConfirmClick() {    //左边的按钮                Toast.makeText(MainActivity.this, "我被点了", Toast.LENGTH_SHORT).show();            }            @Override            public void onDialogCancelClick() {     //右边的按钮                dismiss();            }        };        dialog.setTitle("提示");        dialog.setMessage("你确定要关闭当前页吗?");        dialog.setConfirm("取消");        dialog.setCancel("确定");        dialog.show();        //如果要动态设置对话框大小,那需要在show后,传入的是px        dialog.setDialogSize(800, 1000);    }}
  • activity_main.xml文件如下:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.pxj.customdialog.MainActivity">    <Button        android:onClick="showDialog"        android:layout_centerInParent="true"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="弹出对话框" /></RelativeLayout>

总结

  • 从这个列子中是可以看出来:任何形式样式的对话框都可以自定义的;
  • 有人可能觉得上述的自定义对话框过于麻烦过于繁琐,确实,其实并不需要这么麻烦,有很多方式去实现,这里也仅仅是提供一种思路而已;
0 0
原创粉丝点击