Android自定義消息彈窗(帶效果圖)-創(chuàng)新互聯(lián)

前言 自定義消息彈窗,dialog那種彈窗,先發(fā)效果圖,

請?zhí)砑訄D片描述
請?zhí)砑訄D片描述

網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)建站!專注于網(wǎng)頁設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、微信平臺小程序開發(fā)、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項(xiàng)目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了金湖免費(fèi)建站歡迎大家使用!

彈窗就是使用的popuwindow,使用自由度較高,也可以按照界面位置來展示

一、popuwindow是什么?

PopupWindow是一個彈出式窗口,它可以展示任意View,里面可以自己定義任何看見,而且窗口對象也可以全局使用,它是浮在當(dāng)前窗口的上方展示。

二、使用步驟 首頁布局代碼 如下:
自定義布局代碼先別著急后面會貼,可以先注釋一下 2.首頁所有代碼,在點(diǎn)擊事件中執(zhí)行popuwindow代碼,BaseActivity22 是我繼承的代碼,你們可以只復(fù)制點(diǎn)擊事件中的代碼
public class Pop_Activity extends BaseActivity22 {@BindView(R.id.btn)
    Button btn;
    private PopupWindow popupWindow;

    @Override
    protected int Layout() {return R.layout.activity_pop;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {btn.setOnClickListener(v ->{initPopWindow(v);
        });
    }

    private void initPopWindow(View v) {ScreenUtils.backgroundAlpha(0.5f, (Activity) this);
        View vPopupWindow = LayoutInflater.from(this).inflate(R.layout.item_pop_test, null, false);
        popupWindow = new PopupWindow(vPopupWindow, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);

        Button btn_xixi = (Button) vPopupWindow.findViewById(R.id.btn_xixi);
        Button btn_hehe = (Button) vPopupWindow.findViewById(R.id.btn_hehe);

       btn_xixi.setOnClickListener(v1 ->{Toast.makeText(Pop_Activity.this, "你點(diǎn)擊了111~", Toast.LENGTH_SHORT).show();
            popupWindow.dismiss();
        });
        
        btn_hehe.setOnClickListener(v12 ->{Toast.makeText(Pop_Activity.this, "你點(diǎn)擊了222~", Toast.LENGTH_SHORT).show();
            popupWindow.dismiss();

        });

        //引入依附的布局
        popupWindow.setTouchable(true);
        popupWindow.showAsDropDown(v, 0, 33);
    }

    @Override
    protected void startCoding() {}

    @Override
    public void onSuccess(Object o) {}

    @Override
    public void onError(String error) {}
}
子布局item_pop_test代碼 如下:
自定義canvas,繪制的消息框
package com.ami.myzonghe_demo.utils;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;

public class Message_Back_Style extends View {private Paint mPaint;
    private RectF rect;
    private Path path3;

    public Message_Back_Style(Context context) {super(context);
    }

    public Message_Back_Style(Context context, @Nullable AttributeSet attrs) {super(context, attrs);

        //new一個畫筆對象
        mPaint = new Paint();
        //設(shè)置顏色
        mPaint.setColor(Color.WHITE);


        //new 路徑對象,用來繪制三角形的
        path3 = new Path();
        //路徑繪制是設(shè)置具體的幾個點(diǎn)的位置,然后連在一起,三個點(diǎn)就是三角形,四個點(diǎn)就是四邊形或者其他形狀
        //因?yàn)槲覀兊膱A角矩形是從50的高度開始繪制的,所以我這邊的三角形也設(shè)置的是50的等腰三角形
        path3.moveTo(100, 0);
        path3.lineTo(75, 50);
        path3.lineTo(125, 50);

        //在此執(zhí)行繪制圓角矩形的操作,
        //1,2個參數(shù)是左上角的位置
        //3,4個參數(shù)是右下角的位置
        rect = new RectF(0, 50, new Dp_Px_Util().Dp2Px(context,200),  new Dp_Px_Util().Dp2Px(context,100));
    }

    @Override
    protected void onDraw(Canvas canvas) {super.onDraw(canvas); 

        //繪制圓角矩形   50,50是繪制的弧度
        canvas.drawRoundRect(rect, 50, 50, mPaint);

        //繪制三角形
        //放入設(shè)置的路徑和畫筆對象
        canvas.drawPath(path3,mPaint);
        path3.close();
    }

}
代碼中用到的dp轉(zhuǎn)px的工具類Dp_Px_Util
package com.ami.myzonghe_demo.utils;

import android.content.Context;

public class Dp_Px_Util {public int Dp2Px(Context context, float dp) {final float scale = context.getResources().getDisplayMetrics().density; //當(dāng)前屏幕密度因子
        return (int) (dp * scale + 0.5f);
    }

    public int Px2Dp(Context context, float px) {final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (px / scale + 0.5f);
    }
}
總結(jié) 大家按照上述代碼復(fù)制粘貼就可以實(shí)現(xiàn)本文開始的效果了,如果遇到了點(diǎn)擊按鈕在左邊右邊下邊的情況,可以在Message_Back_Style中自行設(shè)置背景 ∠角 的位置

你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級服務(wù)器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧

新聞標(biāo)題:Android自定義消息彈窗(帶效果圖)-創(chuàng)新互聯(lián)
網(wǎng)頁鏈接:http://bm7419.com/article8/dehhop.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供云服務(wù)器、網(wǎng)站營銷、網(wǎng)站策劃網(wǎng)站維護(hù)虛擬主機(jī)、網(wǎng)站改版

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)