欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2022年09月20日 09:35:56   作者:破z曉  
這篇文章主要為大家詳細介紹了Android使用popupWindow仿微信彈出框使用方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Android使用popupWindow仿微信彈出框的具體實現(xiàn)代碼,供大家參考,具體內(nèi)容如下

效果如下:

一、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的左側(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); ? //設置畫筆抗鋸齒
? ? ? ? paint.setStrokeWidth(2); ? ?//設置線寬
? ? ? ? paint.setColor(Color.BLACK); ?//設置線的顏色
?
? ? ? ? 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)倒計時

    這篇文章主要介紹了Android實戰(zhàn)教程第四十篇之Chronometer實現(xiàn)倒計時,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • Flutter路由框架Fluro使用教程詳細講解

    Flutter路由框架Fluro使用教程詳細講解

    在Flutter應用開發(fā)過程中,除了使用Flutter官方提供的路由外,還可以使用一些第三方路由框架來實現(xiàn)頁面管理和導航,F(xiàn)luro作為一款優(yōu)秀的Flutter企業(yè)級路由框架,F(xiàn)luro的使用比官方提供的路由框架要復雜一些,但是卻非常適合中大型項目
    2022-10-10
  • Fiddler實現(xiàn)手機抓包之小白入門必看

    Fiddler實現(xiàn)手機抓包之小白入門必看

    這篇文章主要介紹了Fiddler實現(xiàn)手機抓包之小白入門必看篇,需要的朋友可以參考下
    2018-03-03
  • arcgis android之地圖圖層切換

    arcgis android之地圖圖層切換

    本篇文章主要介紹了arcgis android之地圖圖層切換,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-04-04
  • Android入門之源碼開發(fā)基礎(chǔ)教程

    Android入門之源碼開發(fā)基礎(chǔ)教程

    這篇文章主要介紹了Android入門之源碼開發(fā)基礎(chǔ)教程,分析了環(huán)境搭建、模擬器使用及編譯文件的相關(guān)技巧與注意事項,需要的朋友可以參考下
    2016-02-02
  • Android面試筆記之常問的Context

    Android面試筆記之常問的Context

    Android技術(shù)面試確實常常被問到Context,大概問題就是說說你對Context的理解吧,當時腦袋里浮現(xiàn)了是原來看到的文章片段亂說一通,這樣還是不行的。平時還是多積累知識,深刻理解Context,在項目開發(fā)過程中也能避免一些陷入坑中。下面就來看看個人的一些總結(jié)吧。
    2016-12-12
  • Android開發(fā)中CheckBox的簡單用法示例

    Android開發(fā)中CheckBox的簡單用法示例

    這篇文章主要介紹了Android開發(fā)中CheckBox的簡單用法,結(jié)合實例形式分析了Android中CheckBox控件的基本功能設置與頁面布局技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2016-07-07
  • Android組件之間交互核心Intent用法分析

    Android組件之間交互核心Intent用法分析

    這篇文章主要介紹了Android組件之間交互核心Intent用法,結(jié)合實例形式分析了Intent實現(xiàn)組件之間交互的步驟與相關(guān)技巧,需要的朋友可以參考下
    2016-06-06
  • Kotlin?泛型邊界型變及星投影使用詳解

    Kotlin?泛型邊界型變及星投影使用詳解

    這篇文章主要為大家介紹了Kotlin?泛型邊界型變及星投影使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-12-12
  • Android快速實現(xiàn)無預覽拍照功能

    Android快速實現(xiàn)無預覽拍照功能

    這篇文章主要為大家詳細介紹了Android快速實現(xiàn)無預覽拍照功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-06-06

最新評論