網站首頁 編程語言 正文
本文實例為大家分享了Android使用popupWindow仿微信彈出框的具體實現代碼,供大家參考,具體內容如下
效果如下:
一、activity_main.xml代碼
在activity_main.xml中設置"彈出框"按鈕,并將activity_main.xml最外層設置一個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的左側圖標 --> ? ? ? ? <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="側邊欄" ? ? ? ? ? ? ? ? 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>
二、創建帶箭頭的視圖類
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 {
? ? /**
? ? ?* 構造方法
? ? ?* */
? ? 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);
? ? ? ? // 創建畫筆
? ? ? ? Paint paint = new Paint();
? ? ? ? paint.setAntiAlias(true); ? //設置畫筆抗鋸齒
? ? ? ? paint.setStrokeWidth(2); ? ?//設置線寬
? ? ? ? paint.setColor(Color.BLACK); ?//設置線的顏色
?
? ? ? ? int height = getHeight(); ? //獲取View的高度
? ? ? ? int width = getWidth(); ? ? //獲取View的寬度
?
? ? ? ? /**視圖顯示的區域**/
? ? ? ? /**
? ? ? ? ?* @param left 長方形左側的x坐標
? ? ? ? ?* @param top 長方形頂的Y坐標
? ? ? ? ?* @param right 長方形右側的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);// 三角形左側頂點
? ? ? ? path.lineTo(getPaddingLeft(), height / 2 - getPaddingLeft()); ? //三角形上面的點
? ? ? ? path.lineTo(getPaddingLeft(), height / 2 + getPaddingLeft()); ? //三角形下面的點*/
?
? ? ? ?/* // 三角形在視圖的正右方
? ? ? ? Path path = new Path();
? ? ? ? //以下是繪制視圖的那個箭頭
? ? ? ? path.moveTo(width, ?height / 2);// 三角形右側頂點
? ? ? ? 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);
? ? }
}
三、創建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 ={"發起群聊","添加朋友","視屏聊天","掃一掃","拍照分享"};
?
?
? ? @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;
? ? ? ? ? ? }
? ? ? ? });
? ? }
?
}
原文鏈接:https://blog.csdn.net/qq_19688207/article/details/113682850
相關推薦
- 2023-01-08 利用Qt實現獲取計算機的硬件信息_C 語言
- 2022-09-08 詳解Dijkstra算法原理及其C++實現_C 語言
- 2022-05-23 C語言的動態內存分配及動態內存分配函數詳解_C 語言
- 2022-03-20 ajax和fetch的區別點總結_AJAX相關
- 2022-06-19 WPF項目在設計界面調用后臺代碼_實用技巧
- 2022-07-19 react組件通訊的基本使用props
- 2022-08-05 處理“URIError: URI malformed“的辦法
- 2022-12-03 FFmpeg?Principle學習new_video_stream添加視頻輸出流_Android
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支