Android使用popupWindow仿微信彈出框使用方法
本文實(shí)例為大家分享了Android使用popupWindow仿微信彈出框的具體實(shí)現(xiàn)代碼,供大家參考,具體內(nèi)容如下
效果如下:
一、activity_main.xml代碼
在activity_main.xml中設(shè)置"彈出框"按鈕,并將activity_main.xml最外層設(shè)置一個(gè)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è)圖標(biāo) --> ? ? ? ? <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)建帶箭頭的視圖類(lèi)
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); ? ? } ? ? ? /** ? ? ?* 重寫(xiě)繪制方法 ? ? ?* */ ? ? @Override ? ? protected void onDraw(Canvas canvas) { ? ? ? ? super.onDraw(canvas); ? ? ? ? // 創(chuàng)建畫(huà)筆 ? ? ? ? Paint paint = new Paint(); ? ? ? ? paint.setAntiAlias(true); ? //設(shè)置畫(huà)筆抗鋸齒 ? ? ? ? paint.setStrokeWidth(2); ? ?//設(shè)置線(xiàn)寬 ? ? ? ? paint.setColor(Color.BLACK); ?//設(shè)置線(xiàn)的顏色 ? ? ? ? ? int height = getHeight(); ? //獲取View的高度 ? ? ? ? int width = getWidth(); ? ? //獲取View的寬度 ? ? ? ? ? /**視圖顯示的區(qū)域**/ ? ? ? ? /** ? ? ? ? ?* @param left 長(zhǎng)方形左側(cè)的x坐標(biāo) ? ? ? ? ?* @param top 長(zhǎng)方形頂?shù)腨坐標(biāo) ? ? ? ? ?* @param right 長(zhǎng)方形右側(cè)的X坐標(biāo) ? ? ? ? ?* @param bottom 長(zhǎng)方形底的Y坐標(biāo)*/ ? ? ? ? RectF rectF = new RectF(getPaddingLeft() - 20,getPaddingTop() - 20,width - getPaddingRight() + 20,height - getPaddingBottom()+20); ? ? ? ? ? /** ? ? ? ? ?* @param rectF RectF對(duì)象 ? ? ? ? ?* @param rx x方向上的圓角半徑 ? ? ? ? ?* @param ry y方向上的圓角半徑 ? ? ? ? ?* @param paint 繪制時(shí)所使用的畫(huà)筆 ? ? ? ? ?* */ ? ? ? ? canvas.drawRoundRect(rectF,30,30,paint); ? ? ? ?/* // 三角形在視圖的正上方 ? ? ? ? Path path = new Path(); ? ? ? ? //以下是繪制視圖的那個(gè)箭頭 ? ? ? ? path.moveTo(width / 2, 0);// 三角形頂點(diǎn) ? ? ? ? path.lineTo(width / 2 + getPaddingTop(), ?getPaddingTop()); ? //三角形右邊的點(diǎn) ? ? ? ? path.lineTo(width / 2 - getPaddingTop(), ?getPaddingTop()); ? //三角形左邊的點(diǎn)*/ ? ? ? ? ? // 三角形在視圖的正下方 ? ? ? ? /*Path path = new Path(); ? ? ? ? //以下是繪制視圖的那個(gè)箭頭 ? ? ? ? path.moveTo(width / 2, height);// 三角形頂點(diǎn) ? ? ? ? path.lineTo(width / 2 - getPaddingBottom(), height - getPaddingBottom()); ? //三角形左邊的點(diǎn) ? ? ? ? path.lineTo(width / 2 + getPaddingBottom(), height - getPaddingBottom()); ? //三角形右邊的點(diǎn)*/ ? ? ? ? ? /*// 三角形在視圖的正左方 ? ? ? ? Path path = new Path(); ? ? ? ? //以下是繪制視圖的那個(gè)箭頭 ? ? ? ? path.moveTo(0, ?height / 2);// 三角形左側(cè)頂點(diǎn) ? ? ? ? path.lineTo(getPaddingLeft(), height / 2 - getPaddingLeft()); ? //三角形上面的點(diǎn) ? ? ? ? path.lineTo(getPaddingLeft(), height / 2 + getPaddingLeft()); ? //三角形下面的點(diǎn)*/ ? ? ? ? ?/* // 三角形在視圖的正右方 ? ? ? ? Path path = new Path(); ? ? ? ? //以下是繪制視圖的那個(gè)箭頭 ? ? ? ? path.moveTo(width, ?height / 2);// 三角形右側(cè)頂點(diǎn) ? ? ? ? path.lineTo(width - getPaddingRight(), height / 2 - getPaddingRight()); ? //三角形上面的點(diǎn) ? ? ? ? path.lineTo(width - getPaddingRight(), height / 2 + getPaddingRight()); ? //三角形下面的點(diǎn)*/ ? ? ? ? ? // 三角形在視圖的右上方 ? ? ? ? Path path = new Path(); ? ? ? ? //以下是繪制視圖的那個(gè)箭頭 ? ? ? ? path.moveTo(width - getPaddingTop() * 3/2, 0);// 三角形頂點(diǎn) ? ? ? ? path.lineTo(width - getPaddingTop(), ?getPaddingTop()); ? //三角形右邊的點(diǎn) ? ? ? ? path.lineTo(width - getPaddingTop()*2, ?getPaddingTop()); ? //三角形左邊的點(diǎn) ? ? ? ? ? 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(); ? ? } ? ? ? ? /** ? ? ?* 彈出框按鈕點(diǎn)擊事件 ? ? ?* */ ? ? 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); ? ? ? ? ? ? } ? ? ? ? }); ? ? } ? ? ? ? /** ? ? ?* 點(diǎn)擊popupBtn按鈕時(shí)彈出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,默認(rèn)情況是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; ? ? ? ? ? ? } ? ? ? ? }); ? ? } ? }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android實(shí)戰(zhàn)教程第四十篇之Chronometer實(shí)現(xiàn)倒計(jì)時(shí)
這篇文章主要介紹了Android實(shí)戰(zhàn)教程第四十篇之Chronometer實(shí)現(xiàn)倒計(jì)時(shí),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11Fiddler實(shí)現(xiàn)手機(jī)抓包之小白入門(mén)必看
這篇文章主要介紹了Fiddler實(shí)現(xiàn)手機(jī)抓包之小白入門(mén)必看篇,需要的朋友可以參考下2018-03-03Android入門(mén)之源碼開(kāi)發(fā)基礎(chǔ)教程
這篇文章主要介紹了Android入門(mén)之源碼開(kāi)發(fā)基礎(chǔ)教程,分析了環(huán)境搭建、模擬器使用及編譯文件的相關(guān)技巧與注意事項(xiàng),需要的朋友可以參考下2016-02-02Android開(kāi)發(fā)中CheckBox的簡(jiǎn)單用法示例
這篇文章主要介紹了Android開(kāi)發(fā)中CheckBox的簡(jiǎn)單用法,結(jié)合實(shí)例形式分析了Android中CheckBox控件的基本功能設(shè)置與頁(yè)面布局技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07Android快速實(shí)現(xiàn)無(wú)預(yù)覽拍照功能
這篇文章主要為大家詳細(xì)介紹了Android快速實(shí)現(xiàn)無(wú)預(yù)覽拍照功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-06-06