Android 自定义Dialog

自定义Dialog.
首先,把Dialog 当作是一个Activity,你想要什么样式就写成什么样子。
创建class类 继承Dialog.重写其中的方法。
效果图:
这里写图片描述

package gdswww.com.momo.view;

import android.app.ActionBar;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import gdswww.com.momo.R;

import static android.content.DialogInterface.BUTTON_POSITIVE;

/**
 * Created by Administrator on 2017/6/15 0015.
 * 自定义 dialog样式
 */

public class ZhuBoDialog extends Dialog {
    public ZhuBoDialog(Context context) {
        super(context);
    }

    public ZhuBoDialog(Context context, int themeResId) {
        super(context, themeResId);
    }

    public static class Builder {
        private Context context;//当前页面
        private String name;//主播名字
        private String imgPath;//头像路径
        private String sex;//性别 男 1   女 2
        private String message;
        private String city;//城市
        private String fensi;//粉丝
        private String huozeng;//获赠
        private String guanzhu;//关注
        private String songchu;//送出
        private View view;//布局
        private View.OnClickListener quxiaoOnCliCKListener;//取消事件
        private View.OnClickListener guanzhuOnCliCKListener;//关注
        private View.OnClickListener aiteOnCliCKListener;//艾特
        private View.OnClickListener songliOnCliCKListener;//送礼
        private View.OnClickListener quanxianOnCliCKListener;//权限

        /**
         * 当前页面
         *
         * @param context
         */
        public Builder(Context context) {
            this.context = context;
        }

        /**
         * 设置主播莫名字
         *
         * @param name
         * @return
         */
        public Builder setName(String name) {
            this.name = name;
            return this;
        }

        /**
         * 设置性别
         *
         * @param sex
         */
        public Builder setSex(String sex) {
            this.sex = sex;
            return this;
        }

        /**
         * 关注
         *
         * @param guanzhu
         * @return
         */
        public Builder setGuanZhu(String guanzhu) {
            this.guanzhu = guanzhu;
            return this;
        }

        /**
         * @param message
         * @return
         */
        public Builder setMessage(String message) {
            this.message = message;
            return this;
        }

        /**
         * 设置城市
         *
         * @param city
         */
        public Builder setCity(String city) {
            this.city = city;
            return this;
        }

        /**
         * 粉丝数量
         *
         * @param fensi
         */
        public Builder setFensi(String fensi) {
            this.fensi = fensi;
            return this;
        }

        /**
         * 获赠量
         *
         * @param huozeng
         */
        public Builder setHuozeng(String huozeng) {
            this.huozeng = huozeng;
            return this;
        }

        /**
         * 送出
         *
         * @param songchu
         */
        public Builder setSongchu(String songchu) {
            this.songchu = songchu;
            return this;
        }

        /**
         * 设置布局
         *
         * @param view
         * @return
         */
        public Builder setContentView(View view) {
            this.view = view;
            return this;
        }

        /**
         * 取消
         *
         * @param
         * @return
         */
        public Builder setQuxiaoOnCliCKListener(View.OnClickListener quxiaoOnCliCKListener) {
            this.quxiaoOnCliCKListener = quxiaoOnCliCKListener;
            return this;
        }

        /**
         * 关注
         *
         * @param guanzhuOnCliCKListener
         * @return
         */
        public Builder setGuanzhuOnCliCKListener(View.OnClickListener guanzhuOnCliCKListener) {
            this.guanzhuOnCliCKListener = guanzhuOnCliCKListener;
            return this;
        }

        /**
         * 艾特
         *
         * @param aiteOnCliCKListener
         * @return
         */
        public Builder setAiTeOnCliCKListener(View.OnClickListener aiteOnCliCKListener) {
            this.aiteOnCliCKListener = aiteOnCliCKListener;
            return this;
        }

        /**
         * 送礼
         *
         * @param songliOnCliCKListener
         * @return
         */
        public Builder setSongLiOnCliCKListener(View.OnClickListener songliOnCliCKListener) {
            this.songliOnCliCKListener = songliOnCliCKListener;
            return this;
        }

        /**
         * 权限
         *
         * @param quanxianOnCliCKListener
         * @return
         */
        public Builder setQuanXianOnCliCKListener(View.OnClickListener quanxianOnCliCKListener) {
            this.quanxianOnCliCKListener = quanxianOnCliCKListener;
            return this;
        }

        public ZhuBoDialog create() {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final ZhuBoDialog dialog = new ZhuBoDialog(context, R.style.Dialog);
            View layout = inflater.inflate(R.layout.dialog_zhubo, null);
            dialog.addContentView(layout, new ActionBar.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
//            (ImageView)layout.findViewById(R.id.zhubo_touxiang_iv).setBackgroundResource(R.mipmap.zhubo);//主播
            ((TextView) layout.findViewById(R.id.zhubo_addr_tv)).setText(city);//地址
            ((TextView) layout.findViewById(R.id.zhubo_name_tv)).setText(name);//名字加ID
            ((TextView) layout.findViewById(R.id.zhubo_guanzhu_tv)).setText(guanzhu);//关注
            ((TextView) layout.findViewById(R.id.zhubo_fs_tv)).setText(fensi);//粉丝
            ((TextView) layout.findViewById(R.id.zhubo_huozeng_tv)).setText(huozeng);//获赠
            ((TextView) layout.findViewById(R.id.zhubo_songchu_tv)).setText(songchu);//蹭出
            layout.findViewById(R.id.click_aite_tv).setOnClickListener(aiteOnCliCKListener);//艾特
            layout.findViewById(R.id.click_guanzhu_tv).setOnClickListener(guanzhuOnCliCKListener);//关注
            layout.findViewById(R.id.click_quanxian_tv).setOnClickListener(quanxianOnCliCKListener);//权限
            layout.findViewById(R.id.click_songliwu_tv).setOnClickListener(songliOnCliCKListener);//送礼
            layout.findViewById(R.id.dialog_guanbi_iv).setOnClickListener(quxiaoOnCliCKListener);//关闭 quxiao

            //aite

//            ((ImageView)layout.findViewById(R.id.zhubo_sex_iv)).(songchu);//性别
            dialog.setContentView(layout);
            return dialog;
        }

    }


}

这是Activity页面 使用

package gdswww.com.momo.activity.me;

import android.os.Build;
import android.os.Message;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.Toast;

import gdswww.com.momo.R;
import gdswww.com.momo.base.MyBaseActivity;
import gdswww.com.momo.view.ZhuBoDialog;

/**
 * Created by Administrator on 2017/5/23 0023.
 */

public class ZhiBoActivity extends MyBaseActivity {
    private ImageView imageView;//查看主播

    @Override
    public void updateUI(Message msg) {
    }

    @Override
    public void initUI() {

        toastShort("主播:ID" + getIntent().getIntExtra("zhubo_id", 0));
        loadImg((ImageView) viewId(R.id.zhubo_img),R.mipmap.zhubo);
        imageView= (ImageView) findViewById(R.id.zhibo_img);
        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showDialog();
            }
        });

    }

    @Override
    public void regUIEvent() {

    }

    @Override
    public int getLayout() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            WindowManager.LayoutParams localLayoutParams = getWindow().getAttributes();
            localLayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS | localLayoutParams.flags);
        }
        return R.layout.activity_zhibojian;
    }

    /**
     * 弹出对话框。
     */
    public void showDialog(){
        final ZhuBoDialog.Builder dialog=new ZhuBoDialog.Builder(ZhiBoActivity.this)
                .setCity("深圳")
                .setFensi("4544")
                .setGuanZhu("1235")
                .setHuozeng("4565")
                .setName("小仙女(ID:25641)")
                .setSex("女")
                .setSongchu("4145")
                .setAiTeOnCliCKListener(new View.OnClickListener() {//艾特
                    @Override
                    public void onClick(View view) {
                        Toast.makeText(ZhiBoActivity.this,"你点击了艾特。",Toast.LENGTH_SHORT).show();
                    }
                }).setGuanzhuOnCliCKListener(new View.OnClickListener() {//关注
                    @Override
                    public void onClick(View view) {
                        Toast.makeText(ZhiBoActivity.this,"你点击了关注。",Toast.LENGTH_SHORT).show();

                    }
                }).setQuanXianOnCliCKListener(new View.OnClickListener() {//权限
                    @Override
                    public void onClick(View view) {
                        Toast.makeText(ZhiBoActivity.this,"你点击权限。",Toast.LENGTH_SHORT).show();

                    }
                }).setQuxiaoOnCliCKListener(new View.OnClickListener() {//取消
                    @Override
                    public void onClick(View view) {dimissProgressDialog();


                    }
                }).setSongLiOnCliCKListener(new View.OnClickListener() {//送礼
                    @Override
                    public void onClick(View view) {
                        Toast.makeText(ZhiBoActivity.this,"你点击了送礼。",Toast.LENGTH_SHORT).show();

                    }
                });
        dialog.create().show();
    }
}

布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/white_shape"
    android:clipChildren="false"
    android:clipToPadding="false"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clipChildren="false"
        android:clipToPadding="false"
        android:background="#00ffffff"
        android:orientation="horizontal">

        <com.gdswww.library.view.CircleImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:clipChildren="false"
            android:src="@mipmap/zhubo"
            app:border_color="@color/white"
            app:border_width="2dp" />
            <ImageView
                android:id="@+id/dialog_guanbi_iv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_gravity="center"
                android:padding="10dp"
                android:src="@mipmap/guanbi" />

    </RelativeLayout>

    <TextView
        android:id="@+id/zhubo_name_tv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:gravity="center"
        android:text="小小美女版(ID:1142564)" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="10dp"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/zhubo_sex_iv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="40dp"
            android:gravity="center"
            android:src="@mipmap/nan" />

        <TextView
            android:id="@+id/zhubo_addr_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:drawableLeft="@mipmap/dingwei"
            android:drawablePadding="10dp"
            android:text="深圳市" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:background="#F0F0F0"
        android:gravity="center"
        android:orientation="vertical"
        android:paddingTop="20dp">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="关注:" />

            <TextView
                android:id="@+id/zhubo_guanzhu_tv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="125"
                android:textColor="@color/text_color" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="50dp"
                android:text="粉丝: " />

            <TextView
                android:id="@+id/zhubo_fs_tv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="12522"
                android:textColor="@color/text_color" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="20dp"
            android:layout_marginTop="20dp"
            android:orientation="horizontal">

            <TextView

                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="送出:" />

            <TextView
                android:id="@+id/zhubo_songchu_tv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="125"
                android:textColor="@color/text_color" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="50dp"
                android:text="获赠: " />

            <TextView
                android:id="@+id/zhubo_huozeng_tv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="12522"
                android:textColor="@color/text_color" />
        </LinearLayout>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <TextView
            android:id="@+id/click_guanzhu_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:padding="10dp"
            android:text="关注"
            android:textColor="@color/colorTheme" />

        <ImageView
            android:layout_width="2dp"
            android:layout_height="20dp"
            android:layout_gravity="center"
            android:background="#F0F0F0" />

        <TextView
            android:id="@+id/click_aite_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:padding="10dp"
            android:text=" @TA "
            android:textColor="@color/colorTheme" />

        <ImageView
            android:layout_width="2dp"
            android:layout_height="20dp"
            android:layout_gravity="center"
            android:background="#F0F0F0" />

        <TextView
            android:id="@+id/click_songliwu_tv"

            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:padding="10dp"
            android:text="送礼物"
            android:textColor="@color/colorTheme" />

        <ImageView
            android:layout_width="2dp"
            android:layout_height="20dp"
            android:layout_gravity="center"
            android:background="#F0F0F0" />

        <TextView
            android:id="@+id/click_quanxian_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:gravity="center"
            android:padding="10dp"
            android:text="权限"
            android:textColor="@color/colorTheme" />
    </LinearLayout>

</LinearLayout>

其中:CircleImageView 是圆形框架。
最后是Style 样式。

这里写代码片
  <style name="Dialog" parent="android:style/Theme.Dialog">
        <item name="android:background">#00000000</item>
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowIsFloating">true</item>
    </style>

可以根据 需求进行封装,这里没有处理。还有点击事件多的话。可以定义几个接口来实现。代码看起来简洁点。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值