Android开发之自定义AlertDialog的大小
生活随笔
收集整理的這篇文章主要介紹了
Android开发之自定义AlertDialog的大小
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
老套路先看效果圖:
?
再來看下代碼:
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, "繼續(xù)分享到微信", btnColor, imgId);}public void showSharedChat(Activity activity, String msg, @ColorRes int btnColor, @DrawableRes int imgId) {showSharedDialog(activity, null, msg, "繼續(xù)分享到QQ", btnColor, imgId);}public void showSharedWeiBo(Activity activity, String msg, @ColorRes int btnColor, @DrawableRes int imgId) {showSharedDialog(activity, null, msg, "繼續(xù)分享到微博", 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);//設(shè)置dialog窗體的大小,一定要是AlertDialogalertDialog.getWindow().setLayout(dialogWidth, dialogHeight);}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.iv_close_right://關(guān)閉dialogalertDialog.dismiss();break;case R.id.ll_shared_layout://跳轉(zhuǎn)到分享頁面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"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginBottom="@dimen/dp_5"android:gravity="center"android:orientation="horizontal"><ImageViewandroid: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" /><TextViewandroid: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" /><ImageViewandroid: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><TextViewandroid: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" /><LinearLayoutandroid: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"><ImageViewandroid: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" /><TextViewandroid:id="@+id/tv_shared_button"android:layout_width="wrap_content"android:layout_height="wrap_content"android:gravity="center"android:text="繼續(xù)分享到微信"android:textColor="@color/white"android:textSize="18sp" /></LinearLayout> </LinearLayout>dialog的顯示以及響應(yīng)事件的方法也很簡單:
//Kotlin版本 SharedDialog.getDialog().showSharedDialog(this, "已保存到相冊", "由于微信分享限制,請到微信上傳視頻來分享", "繼續(xù)分享到微信", R.color.blue_text, R.drawable.switch_img)SharedDialog.getDialog().setJumpSharedPage { ToastUtil.show("點擊了按鈕")}?
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)
以上是生活随笔為你收集整理的Android开发之自定义AlertDialog的大小的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 全民k歌最近删除的歌曲怎么恢复
- 下一篇: Android开发之Gradle多渠道打