Android開發之自定義AlertDialog的大小

老套路先看效果圖:

 

再來看下代碼:

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佈局很重要:

<?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="match_parent"
    android:background="@color/white"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="@dimen/dp_5"
        android:gravity="center"
        android:orientation="horizontal">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_gravity="right"
            android:layout_margin="@dimen/dp_5"
            android:padding="@dimen/dp_8"
            android:src="@drawable/close"
            android:tint="@color/black"
            android:visibility="invisible" />

        <TextView
            android:id="@+id/tv_share_tittle"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/dp_5"
            android:layout_weight="1"
            android:gravity="center"
            android:text="@string/save_picter"
            android:textColor="@color/black"
            android:textSize="18sp"
            android:textStyle="bold" />

        <ImageView
            android:id="@+id/iv_close_right"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_gravity="right"
            android:layout_margin="@dimen/dp_5"
            android:padding="@dimen/dp_8"
            android:src="@drawable/close"
            android:tint="@color/black" />
    </LinearLayout>

    <TextView
        android:id="@+id/tv_share_message"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_centerInParent="true"
        android:layout_marginBottom="@dimen/dp_30"
        android:layout_weight="1"
        android:gravity="center"
        android:paddingLeft="@dimen/dp_20"
        android:paddingRight="@dimen/dp_20"
        android:textColor="@color/black"
        android:textSize="16sp" />

    <LinearLayout
        android:id="@+id/ll_shared_layout"
        android:layout_width="match_parent"
        android:layout_height="38dp"
        android:layout_alignParentBottom="true"
        android:layout_centerInParent="true"
        android:layout_marginLeft="@dimen/dp_20"
        android:layout_marginRight="@dimen/dp_20"
        android:layout_marginBottom="2dp"
        android:background="@android:color/holo_green_light"
        android:gravity="center">

        <ImageView
            android:id="@+id/iv_left_shared_img"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="@dimen/dp_5"
            android:background="@drawable/close"
            android:tint="@color/white" />

        <TextView
            android:id="@+id/tv_shared_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="繼續分享到微信"
            android:textColor="@color/white"
            android:textSize="18sp" />

    </LinearLayout>
</LinearLayout>

dialog的顯示以及響應事件的方法也很簡單:

//Kotlin版本
SharedDialog.getDialog().showSharedDialog(this, 
"已保存到相冊",
"由於微信分享限制,請到微信上傳視頻來分享",
"繼續分享到微信", 
R.color.blue_text, 
R.drawable.switch_img)

        SharedDialog.getDialog().setJumpSharedPage { ToastUtil.show("點擊了按鈕")}

 

發佈了193 篇原創文章 · 獲贊 107 · 訪問量 38萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章