Android使用popupWindow仿微信彈出框使用方法
更新時間:2022年09月20日 09:35:56 作者:破z曉
這篇文章主要為大家詳細介紹了Android使用popupWindow仿微信彈出框使用方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了Android使用popupWindow仿微信彈出框的具體實現(xiàn)代碼,供大家參考,具體內(nèi)容如下
效果如下:

一、activity_main.xml代碼
在activity_main.xml中設(shè)置"彈出框"按鈕,并將activity_main.xml最外層設(shè)置一個id,代碼如下
<androidx.drawerlayout.widget.DrawerLayout ? ? xmlns:android="http://schemas.android.com/apk/res/android" ? ? xmlns:app="http://schemas.android.com/apk/res-auto" ? ? xmlns:tools="http://schemas.android.com/tools" ? ? android:layout_width="match_parent" ? ? android:layout_height="match_parent" ? ? android:id="@+id/drawerLayout" ? ? tools:context=".MainActivity"> ? ? ? ? ? ? <!-- 模擬toolbar的左側(cè)圖標 --> ? ? ? ? <LinearLayout ? ? ? ? ? ? android:layout_width="match_parent" ? ? ? ? ? ? android:layout_height="56dp" ? ? ? ? ? ? android:orientation="horizontal" ? ? ? ? ? ? android:gravity="center_vertical" ? ? ? ? ? ? android:background="@color/colorPrimary"> ? ? ? ? ? ? <Button ? ? ? ? ? ? ? ? android:id="@+id/img_menuBtn" ? ? ? ? ? ? ? ? android:layout_width="match_parent" ? ? ? ? ? ? ? ? android:layout_height="match_parent" ? ? ? ? ? ? ? ? android:layout_weight="2" ? ? ? ? ? ? ? ? android:text="側(cè)邊欄" ? ? ? ? ? ? ? ? android:padding="8dp"/> ? ? ? ? ? ? ? <TextView ? ? ? ? ? ? ? ? android:layout_width="match_parent" ? ? ? ? ? ? ? ? android:layout_height="match_parent" ? ? ? ? ? ? ? ? android:layout_weight="1"/> ? ? ? ? ? ? ? <Button ? ? ? ? ? ? ? ? android:id="@+id/popupBtn" ? ? ? ? ? ? ? ? android:layout_width="match_parent" ? ? ? ? ? ? ? ? android:layout_height="match_parent" ? ? ? ? ? ? ? ? android:layout_weight="2" ? ? ? ? ? ? ? ? android:text="彈出框" ? ? ? ? ? ? ? ? android:padding="8dp"/> ? ? ? ? </LinearLayout> ? </androidx.drawerlayout.widget.DrawerLayout>
二、創(chuàng)建帶箭頭的視圖類
ArrowView.java代碼如下:
package com.chy.test;
?
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.widget.LinearLayout;
?
?
import androidx.annotation.Nullable;
?
public class ArrowView extends LinearLayout {
? ? /**
? ? ?* 構(gòu)造方法
? ? ?* */
? ? public ArrowView(Context context) {
? ? ? ? super(context);
? ? }
?
? ? public ArrowView(Context context,@Nullable AttributeSet attrs) {
? ? ? ? super(context,attrs);
? ? }
?
? ? public ArrowView(Context context,@Nullable AttributeSet attrs,int defStyleAttr) {
? ? ? ? super(context,attrs,defStyleAttr);
? ? }
?
? ? /**
? ? ?* 重寫繪制方法
? ? ?* */
? ? @Override
? ? protected void onDraw(Canvas canvas) {
? ? ? ? super.onDraw(canvas);
? ? ? ? // 創(chuàng)建畫筆
? ? ? ? Paint paint = new Paint();
? ? ? ? paint.setAntiAlias(true); ? //設(shè)置畫筆抗鋸齒
? ? ? ? paint.setStrokeWidth(2); ? ?//設(shè)置線寬
? ? ? ? paint.setColor(Color.BLACK); ?//設(shè)置線的顏色
?
? ? ? ? int height = getHeight(); ? //獲取View的高度
? ? ? ? int width = getWidth(); ? ? //獲取View的寬度
?
? ? ? ? /**視圖顯示的區(qū)域**/
? ? ? ? /**
? ? ? ? ?* @param left 長方形左側(cè)的x坐標
? ? ? ? ?* @param top 長方形頂?shù)腨坐標
? ? ? ? ?* @param right 長方形右側(cè)的X坐標
? ? ? ? ?* @param bottom 長方形底的Y坐標*/
? ? ? ? RectF rectF = new RectF(getPaddingLeft() - 20,getPaddingTop() - 20,width - getPaddingRight() + 20,height - getPaddingBottom()+20);
?
? ? ? ? /**
? ? ? ? ?* @param rectF RectF對象
? ? ? ? ?* @param rx x方向上的圓角半徑
? ? ? ? ?* @param ry y方向上的圓角半徑
? ? ? ? ?* @param paint 繪制時所使用的畫筆
? ? ? ? ?* */
? ? ? ? canvas.drawRoundRect(rectF,30,30,paint);
? ? ? ?/* // 三角形在視圖的正上方
? ? ? ? Path path = new Path();
? ? ? ? //以下是繪制視圖的那個箭頭
? ? ? ? path.moveTo(width / 2, 0);// 三角形頂點
? ? ? ? path.lineTo(width / 2 + getPaddingTop(), ?getPaddingTop()); ? //三角形右邊的點
? ? ? ? path.lineTo(width / 2 - getPaddingTop(), ?getPaddingTop()); ? //三角形左邊的點*/
?
? ? ? ? // 三角形在視圖的正下方
? ? ? ? /*Path path = new Path();
? ? ? ? //以下是繪制視圖的那個箭頭
? ? ? ? path.moveTo(width / 2, height);// 三角形頂點
? ? ? ? path.lineTo(width / 2 - getPaddingBottom(), height - getPaddingBottom()); ? //三角形左邊的點
? ? ? ? path.lineTo(width / 2 + getPaddingBottom(), height - getPaddingBottom()); ? //三角形右邊的點*/
?
? ? ? ? /*// 三角形在視圖的正左方
? ? ? ? Path path = new Path();
? ? ? ? //以下是繪制視圖的那個箭頭
? ? ? ? path.moveTo(0, ?height / 2);// 三角形左側(cè)頂點
? ? ? ? path.lineTo(getPaddingLeft(), height / 2 - getPaddingLeft()); ? //三角形上面的點
? ? ? ? path.lineTo(getPaddingLeft(), height / 2 + getPaddingLeft()); ? //三角形下面的點*/
?
? ? ? ?/* // 三角形在視圖的正右方
? ? ? ? Path path = new Path();
? ? ? ? //以下是繪制視圖的那個箭頭
? ? ? ? path.moveTo(width, ?height / 2);// 三角形右側(cè)頂點
? ? ? ? path.lineTo(width - getPaddingRight(), height / 2 - getPaddingRight()); ? //三角形上面的點
? ? ? ? path.lineTo(width - getPaddingRight(), height / 2 + getPaddingRight()); ? //三角形下面的點*/
?
? ? ? ? // 三角形在視圖的右上方
? ? ? ? Path path = new Path();
? ? ? ? //以下是繪制視圖的那個箭頭
? ? ? ? path.moveTo(width - getPaddingTop() * 3/2, 0);// 三角形頂點
? ? ? ? path.lineTo(width - getPaddingTop(), ?getPaddingTop()); ? //三角形右邊的點
? ? ? ? path.lineTo(width - getPaddingTop()*2, ?getPaddingTop()); ? //三角形左邊的點
?
? ? ? ? path.close();
? ? ? ? canvas.drawPath(path, paint);
? ? ? ? super.onDraw(canvas);
? ? }
}三、創(chuàng)建popupwindow_dialog.xml和text.xml
popupwindow_dialog.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:orientation="vertical"> ? ? <!--使用箭頭視圖--> ? ? <com.chy.test.ArrowView ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="match_parent" ? ? ? ? android:background="@color/whiteColor" ? ? ? ? android:padding="20dp"> ? ? ? ? ? <ListView ? ? ? ? ? ? android:id="@+id/lv_dialog" ? ? ? ? ? ? android:layout_width="match_parent" ? ? ? ? ? ? android:layout_height="match_parent" ? ? ? ? ? ? android:cacheColorHint="#00000000"> ? ? ? ? </ListView> ? ? ? </com.chy.test.ArrowView> ? </LinearLayout>
text.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:orientation="vertical" > ? ? ? <TextView ? ? ? ? android:id="@+id/tv_item" ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:layout_marginLeft="10dip" ? ? ? ? android:padding="5dp" ? ? ? ? android:textColor="@color/whiteColor" ? ? ? ? android:textSize="20sp" /> ? </LinearLayout>
四、使用方法
package com.chy.test;
?
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.Toast;
?
public class MainActivity extends AppCompatActivity {
?
?
? ? private Button popupBtn;// 彈出框按鈕
? ? private PopupWindow popupWindow;
? ? private LinearLayout layout;
? ? private ListView listView;
? ? private String[] add ={"發(fā)起群聊","添加朋友","視屏聊天","掃一掃","拍照分享"};
?
?
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_main);
?
? ? ? ? initViews();
? ? }
? ? /**
? ? ?* 初始化視圖
? ? ?* */
? ? private void initViews() {
? ? ? ? // 彈出框
? ? ? ? popupBtn = findViewById(R.id.popupBtn);
? ? ? ? popupClick();
? ? }
?
?
? ? /**
? ? ?* 彈出框按鈕點擊事件
? ? ?* */
? ? private void popupClick(){
? ? ? ? popupBtn.setOnClickListener(new View.OnClickListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onClick(View v) {
? ? ? ? ? ? ? ? int y1 = popupBtn.getBottom() * 3 / 2;
? ? ? ? ? ? ? ? int x1 = getWindowManager().getDefaultDisplay().getWidth();
? ? ? ? ? ? ? ? showAddPopupWindow(x1, y1);
? ? ? ? ? ? }
? ? ? ? });
? ? }
?
?
? ? /**
? ? ?* 點擊popupBtn按鈕時彈出popuwindow
? ? ?*/
? ? public void showAddPopupWindow(int x, int y) {
? ? ? ? layout = (LinearLayout) LayoutInflater.from(MainActivity.this).inflate(
? ? ? ? ? ? ? ? R.layout.popupwindow_dialog, null);
? ? ? ? listView = layout.findViewById(R.id.lv_dialog);
? ? ? ? listView.setAdapter(new ArrayAdapter(MainActivity.this,
? ? ? ? ? ? ? ? R.layout.text, R.id.tv_item, add));
?
? ? ? ? popupWindow = new PopupWindow(MainActivity.this);
? ? ? ? // 以下兩種選其一
? ? ? ? //popupWindow.setBackgroundDrawable(new BitmapDrawable());
? ? ? ? popupWindow.setBackgroundDrawable(null);
?
? ? ? ? popupWindow
? ? ? ? ? ? ? ? .setWidth(getWindowManager().getDefaultDisplay().getWidth() / 2);
? ? ? ? popupWindow.setHeight(640);
? ? ? ? popupWindow.setOutsideTouchable(true);
? ? ? ? popupWindow.setFocusable(true);
? ? ? ? popupWindow.setContentView(layout);
? ? ? ? popupWindow.showAtLocation(findViewById(R.id.drawerLayout), Gravity.LEFT
? ? ? ? ? ? ? ? | Gravity.TOP, x, y);//需要指定Gravity,默認情況是center.
? ? ? ? listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? long arg3) {
? ? ? ? ? ? ? ? Toast.makeText(getBaseContext(), "您選擇了:"+add[arg2],Toast.LENGTH_SHORT).show();
? ? ? ? ? ? ? ? popupWindow.dismiss();
? ? ? ? ? ? ? ? popupWindow = null;
? ? ? ? ? ? }
? ? ? ? });
? ? }
?
}以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android實戰(zhàn)教程第四十篇之Chronometer實現(xiàn)倒計時
這篇文章主要介紹了Android實戰(zhàn)教程第四十篇之Chronometer實現(xiàn)倒計時,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-11-11

