日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

Android使用popupWindow仿微信彈出框使用方法_Android

作者:破z曉 ? 更新時間: 2022-11-10 編程語言

本文實例為大家分享了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

欄目分類
最近更新