Android开发之自定义AlertDialog的大小

老套路先看效果图:

Android开发之自定义AlertDialog的大小_第1张图片

 

再来看下代码:

package com.tm.live.ui.dialog;

import android.app.Activity;
import android.support.annotation.ColorRes;
import android.support.annotation.DrawableRes;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.tm.live.R;
import com.tm.live.utils.AndroidUtils;

/**
 * @author xiayiye5
 */
public class SharedDialog implements View.OnClickListener {

    private AlertDialog alertDialog;

    private SharedDialog() {
    }

    private static SharedDialog sharedDialog = new SharedDialog();

    public static SharedDialog getDialog() {
        return sharedDialog;
    }

    /**
     * 标题可不传,就用默认的
     *
     * @param activity 要显示的窗体
     * @param msg      弹出的消息
     * @param btnColor 分享按钮的背景色
     * @param imgId    按钮左边的图片
     */
    public void showSharedWeChat(Activity activity, String msg, @ColorRes int btnColor, @DrawableRes int imgId) {
        showSharedDialog(activity, null, msg, "继续分享到微信", btnColor, imgId);
    }

    public void showSharedChat(Activity activity, String msg, @ColorRes int btnColor, @DrawableRes int imgId) {
        showSharedDialog(activity, null, msg, "继续分享到QQ", btnColor, imgId);
    }

    public void showSharedWeiBo(Activity activity, String msg, @ColorRes int btnColor, @DrawableRes int imgId) {
        showSharedDialog(activity, null, msg, "继续分享到微博", btnColor, imgId);
    }

    /**
     * @param activity 要显示的窗体
     * @param msg      弹出的消息
     * @param btnColor 分享按钮的背景色
     * @param imgId    按钮左边的图片
     */
    public void showSharedDialog(Activity activity, String title, String msg, String btnText, @ColorRes int btnColor, @DrawableRes int imgId) {
        AlertDialog.Builder dialog = new AlertDialog.Builder(activity);
        View dialogView = View.inflate(activity, R.layout.shared_dialog_view, null);
        ImageView ivCloseRight = dialogView.findViewById(R.id.iv_close_right);
        ImageView ivLeftSharedImg = dialogView.findViewById(R.id.iv_left_shared_img);
        TextView tvSharedButton = dialogView.findViewById(R.id.tv_shared_button);
        tvSharedButton.setText(btnText);
        LinearLayout llSharedLayout = dialogView.findViewById(R.id.ll_shared_layout);
        ivLeftSharedImg.setImageDrawable(activity.getDrawable(imgId));
        llSharedLayout.setBackgroundColor(activity.getColor(btnColor));
        ivCloseRight.setOnClickListener(this);
        llSharedLayout.setOnClickListener(this);
        dialog.setView(dialogView);
        alertDialog = dialog.create();
        TextView tvShareMessage = dialogView.findViewById(R.id.tv_share_message);
        tvShareMessage.setText(msg);
        TextView tvShareTittle = dialogView.findViewById(R.id.tv_share_tittle);
        if (!TextUtils.isEmpty(title)) {
            tvShareTittle.setText(title);
        }
        alertDialog.setCancelable(false);
        alertDialog.show();
        int dialogWidth = AndroidUtils.getInstance().getScreenWidth(activity);
        int dialogHeight = (int) (AndroidUtils.getInstance().getScreenHeight(activity) * 0.3);
        //设置dialog窗体的大小,一定要是AlertDialog
        alertDialog.getWindow().setLayout(dialogWidth, dialogHeight);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_close_right:
                //关闭dialog
                alertDialog.dismiss();
                break;
            case R.id.ll_shared_layout:
                //跳转到分享页面
                jumpSharedPage.goShared();
                break;
            default:
                break;
        }
    }

    public interface JumpSharedPage {
        /**
         * 点击分享后的方法
         */
        void goShared();
    }

    private JumpSharedPage jumpSharedPage;

    public void setJumpSharedPage(JumpSharedPage jumpSharedPage) {
        this.jumpSharedPage = jumpSharedPage;
    }
}

注意:XML布局很重要:




    

        

        

        
    

    

    

        

        

    

dialog的显示以及响应事件的方法也很简单:

//Kotlin版本
SharedDialog.getDialog().showSharedDialog(this, 
"已保存到相册",
"由于微信分享限制,请到微信上传视频来分享",
"继续分享到微信", 
R.color.blue_text, 
R.drawable.switch_img)

        SharedDialog.getDialog().setJumpSharedPage { ToastUtil.show("点击了按钮")}

 

你可能感兴趣的:(Android总结)