Android之自定义Dialog弹出框公共组件类

来源:互联网 发布:上海趣骋网络 编辑:程序博客网 时间:2024/06/03 14:52

        因为刚进新公司,手上暂时没项目,于是Q哥就说让我写个自定义的弹出框公共组件类,方便以后项目中可以直接调用。于是我首先上网查了下这方面的资料,自定义dialog的信息是有不少,但没有一个是我想要的。我想实现的是可以在调用时直接传参数就能定义Dialog的背景、title背景、内容背景、button背景、字体颜色、大小等等这些,于是就只好自己想着写了,最后基本算是达到了预想的效果,但同时却感觉调用时没有预先想的那么清晰便捷,可能是因为参数多了以后,人容易晕吧。但是不管怎样,本着知识共享,共同进步的精神,就把这东西写出来,一方便是做一个知识总结,另一方面也希望给需要用到的朋友带来些帮助吧、、

一:先说下思路

1. android 系统提供了一个 Dialog类. 我们通过重写Dialog,可以把自己的工作放在"protected void onCreate(Bundle savedInstanceState)" 在里面先调用系统的"super.onCreate(savedInstanceState)" 其就会保证调用这个method. 
2. 至于 Dialog 的界面 我们就通过自己写一个xml 文件 然后 在 "void onCreate(Bundle)" 通过 "setContentView()" 来使之生效

3. 然后我们通过构造函数,把想自定义的内容构造出来,在调用的时候直接传参数就行了 

3. Dialog 使用问题: 1. 弹出:show() 2. 取消:dismiss() 


二:自定义的xml布局

<?xml version="1.0" encoding="utf-8"?><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:clickable="true"    android:orientation="vertical"    android:padding="20.0dip" >    <LinearLayout        android:id="@+id/All_layout"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:layout_gravity="center"        android:orientation="vertical" >        <!-- title -->        <LinearLayout            android:id="@+id/layoutTitleView"            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:layout_gravity="center"            android:background="@drawable/dialog_titleview_bg"            android:orientation="horizontal" >            <TextView                android:id="@+id/Dlg_Title"                style="@style/text_18_ffffff"                android:layout_width="fill_parent"                android:layout_height="40.0dip"                android:gravity="center"                android:text="标题"                 android:textColor="#fff"                android:visibility="visible" />        </LinearLayout>        <View            android:id="@+id/lineView"            android:layout_width="fill_parent"            android:layout_height="1.0px"            android:background="#ffd0d0d0" />                <!-- body -->        <LinearLayout            android:id="@+id/layoutBodyView"            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:layout_gravity="bottom"            android:gravity="center"            android:background="@drawable/dialog_bodyview_bg"            android:orientation="horizontal" >            <TextView                android:id="@+id/Dlg_content"                style="@style/text_16_666666"                android:layout_width="fill_parent"                android:layout_height="wrap_content"                android:gravity="left|center"                android:lineSpacingMultiplier="1.5"                android:minHeight="120.0dip"                android:paddingBottom="15.0dip"                android:paddingLeft="20.0dip"                android:paddingRight="20.0dip"                android:paddingTop="15.0dip"                android:textColor="#fff"                android:text="内容" />        </LinearLayout>        <View            android:id="@+id/lineView1"            android:layout_width="fill_parent"            android:layout_height="1.0px"            android:background="#ffd0d0d0" />        <!-- button -->        <LinearLayout            android:id="@+id/layoutFootView"            android:layout_width="match_parent"            android:layout_height="60.0dip"            android:layout_gravity="bottom"            android:gravity="center"            android:background="@drawable/dialog_footview_bg"            android:orientation="horizontal" >            <Button                android:id="@+id/Dlg_Yes"                android:layout_width="110dp"                android:layout_height="wrap_content"                android:background="@drawable/btn_ok_pressed"                android:gravity="center"                android:text="是"                android:textColor="#fff"                android:textSize="16sp" />            <Button                android:id="@+id/Dlg_No"                android:layout_width="110dp"                android:layout_height="wrap_content"                android:layout_marginLeft="10dp"                android:background="@drawable/btn_ok_normal"                android:gravity="center"                android:text="否"                android:textColor="#fff"                android:textSize="16sp" />        </LinearLayout>    </LinearLayout></FrameLayout>
三:自定义Dialog类

import android.graphics.Color;import com.dyr.view.R;import android.R.integer;import android.app.Dialog;import android.content.Context;import android.content.res.ColorStateList;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.LinearLayout;import android.widget.TextView;public class DialogDemo extends Dialog implements android.view.View.OnClickListener {    LinearLayout layout, layoutTitleView, layoutBodyView, layoutFootView;    TextView Dlg_title, Dlg_Content;    View lineView, lineView1;    Button Dlg_Yes, Dlg_No;    CustomDialogListener cdListener;    String title, content;    int titleColor;    String titleColorStr;// 标题颜色    int layoutTitleViewBg;// 标题背景色    int layoutBodyViewBg;//内容背景色    int layoutFootViewBg;//底部背景色    int Dlg_Yes_Bg,Dlg_No_Bg; //两个button的背景色    int lineViewColorStr, lineView1ColorStr;    int contentColor;//内容字体颜色    Float titleSize;    int allLayoutBg, layoutTitleColor; //背景    public Context ctx;    public DialogDemo(Context context,                      int theme,                      int layoutTitleViewBg,                      int layoutBodyViewBg,                      int layoutFootViewBg,                      String title,                      int titleColor,                      Float titleSize,                      int lineViewColorStr,                      int lineView1ColorStr,                      String content,                      int contentColor,                      int Dlg_Yes_Bg,                      int Dlg_No_Bg,                      CustomDialogListener cdListener) {        super(context, theme);        this.cdListener = cdListener;        //       this.allLayoutBg = allLayoutBg;        this.layoutTitleColor = layoutTitleColor;        this.layoutTitleViewBg=layoutTitleViewBg;        this.layoutBodyViewBg=layoutBodyViewBg;        this.layoutFootViewBg=layoutFootViewBg;        this.title = title;        this.titleColor = titleColor;        this.titleSize = titleSize;        this.lineViewColorStr = lineViewColorStr;        this.lineView1ColorStr = lineView1ColorStr;        this.content = content;        this.contentColor = contentColor;        this.Dlg_Yes_Bg=Dlg_Yes_Bg;        this.Dlg_No_Bg=Dlg_No_Bg;        this.ctx = context;    }    public DialogDemo(Context context,                      int theme,                      int allLayoutBg,//int layoutTitleColor ,                      String title,                      String titleColor,                      Float titleSize,                      String content,                      int contentColor,                      CustomDialogListener cdListener) {        super(context, theme);        this.cdListener = cdListener;        this.allLayoutBg = allLayoutBg;        this.layoutTitleColor = layoutTitleColor;        this.title = title;        this.titleColorStr = titleColor;        this.titleSize = titleSize;        this.content = content;        this.contentColor = contentColor;//      this.context=context;    }    @Override    protected void onCreate(Bundle savedInstanceState) {        // TODO Auto-generated method stub        super.onCreate(savedInstanceState);        this.setContentView(R.layout.exit_dialog);        this.setCanceledOnTouchOutside(false); //点击外部不会消失          InitViews();    }    private void InitViews() {        layoutTitleView=(LinearLayout)findViewById(R.id.layoutTitleView);        layoutBodyView=(LinearLayout)findViewById(R.id.layoutBodyView);        layoutFootView=(LinearLayout)findViewById(R.id.layoutFootView);        Dlg_title = (TextView) findViewById(R.id.Dlg_Title);        lineView = (View) findViewById(R.id.lineView);        lineView1 = (View) findViewById(R.id.lineView1);        Dlg_Content = (TextView) findViewById(R.id.Dlg_content);//      layout.setBackgroundResource(this.allLayoutBg);        layoutTitleView.setBackgroundResource(this.layoutTitleViewBg);        layoutBodyView.setBackgroundResource(this.layoutBodyViewBg);        layoutFootView.setBackgroundResource(this.layoutFootViewBg);        Dlg_title.setText(this.title);        Dlg_title.setTextColor(this.titleColor);//      Dlg_title.setTextColor(Color.parseColor(titleColorStr));        lineView.setBackgroundColor(this.lineViewColorStr);        lineView1.setBackgroundColor(this.lineView1ColorStr);        Dlg_title.setTextSize(this.titleSize);        Dlg_Content.setText(this.content);        Dlg_Content.setTextColor(contentColor);        Dlg_Yes = (Button) findViewById(R.id.Dlg_Yes);        Dlg_No = (Button) findViewById(R.id.Dlg_No);        Dlg_Yes.setBackgroundResource(this.Dlg_Yes_Bg);        Dlg_No.setBackgroundResource(this.Dlg_No_Bg);        Dlg_Yes.setOnClickListener(this);        Dlg_No.setOnClickListener(this);    }    public void setTitleColor(int color) {        Dlg_title.setTextColor(color);    }    @Override    public void onClick(View v) {        cdListener.OnClick(v);        dismiss();    }    @Override    public void dismiss() {        System.out.println("dialog dismiss...");        super.dismiss();    }}


四:接口类CustomDialogListener

import android.view.View;public interface CustomDialogListener { public void OnClick(View view); }


五:调用方法

DialogDemo dialog=new DialogDemo(MainActivity.this,R.style.Dialog,                        R.drawable.dialog_titleview_bg,    //设置title框的背景                        R.drawable.dialog_bodyview_bg,    //设置body框的背景                        R.drawable.dialog_footview_bg,   //设置foot框的背景"自定义Dialog标题",              //设置标题                        getResources().getColor(R.color.red),//设置标题颜色(float) 21,                         //设置标题字体                        getResources().getColor(R.color.red), //设置上分割线                        getResources().getColor(R.color.red) ,//设置下分割线"此处显示内容~~~~~~~~\n",          //内容YELLOW,                              //内容字体颜色                        R.drawable.btn_ok_pressed,          //设置okbutton背景色                        R.drawable.btn_cancel_pressed,          //设置nobutton背景色new CustomDialogListener() {         //设置按钮监              public void OnClick(View view) {                 // TODO Auto-generated method stub                    switch(view.getId()){                         case R.id.Dlg_Yes:                     //    finish();                          break;                         case R.id.Dlg_No:                         break;                           }                } });     dialog.show();


六:运行效果图



至此,我们就可以自定义弹出框的样式、颜色、字体、等所有内容了。可能有些地方做的不好,希望大牛们多多交流~

如有转载,请注明出处!http://blog.csdn.net/u010932039



0 0
原创粉丝点击