PopupWindow仿微信浮層彈出框效果
最近公司項(xiàng)目需要實(shí)現(xiàn)類(lèi)似微信的浮層彈出框。研究發(fā)現(xiàn)是用PopupWindow實(shí)現(xiàn)的。而且可以自定義位置以及出現(xiàn)和退出時(shí)的動(dòng)畫(huà),由于太晚了就不實(shí)現(xiàn)動(dòng)畫(huà)了,需要得同學(xué)請(qǐng)自己研究下。由于本人新手其中的不足和缺點(diǎn)請(qǐng)見(jiàn)諒。
代碼如下:
首先是定義頂部按鈕的main.xml文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/main" android:orientation="vertical" tools:context=".MainActivity" android:background="@color/white" > <RelativeLayout android:id="@+id/rl_action_bar" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:padding="10dip" android:background="@color/gold" > <Button android:id="@+id/more" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_marginRight="10dip" android:background="@drawable/more" /> <Button android:id="@+id/add" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="20dip" android:layout_toLeftOf="@+id/more" android:background="@drawable/add" /> <Button android:id="@+id/search" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="20dip" android:layout_toLeftOf="@+id/add" android:background="@drawable/search" /> </RelativeLayout> </LinearLayout>
其次是定義彈出框PopupWindow的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:background="@drawable/click" android:cacheColorHint="#00000000" android:orientation="vertical" > <ListView android:id="@+id/lv_dialog" android:layout_width="match_parent" android:layout_height="match_parent" android:cacheColorHint="#00000000" android:listSelector="@drawable/grouplist_item_bg_normal" > </ListView> </LinearLayout>
接著是每一個(gè)彈出框顯示的文字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_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="10dip" android:padding="5dp" android:textSize="20sp" /> </LinearLayout>
最后是主界面的MainActivity.java
package com.bn.weixindemo; import android.app.Activity; import android.graphics.drawable.BitmapDrawable; import android.os.Bundle; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; 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; import android.widget.AdapterView.OnItemClickListener; /** * *@title 標(biāo)題 *@description 仿微信頂部彈出框的popuwindow *@author zhengxiaolin *@version 1.0 *@created 2014-5-23 上午12:11:11 *@changeRecord [修改記錄](méi)<br /> */ public class MainActivity extends Activity implements OnClickListener{ private Button mBtnMore,mBtnAdd,mBtnSearch; private PopupWindow popupWindow; private LinearLayout layout; private ListView listView; private String[] more = {"我的相冊(cè)","我的收藏","我的銀行卡","設(shè)置","意見(jiàn)反饋"}; private String[] add ={"發(fā)起群聊","添加朋友","視屏聊天","掃一掃","拍照分享"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mBtnMore = (Button) findViewById(R.id.more); mBtnAdd = (Button) findViewById(R.id.add); mBtnSearch = (Button) findViewById(R.id.search); setOnClickListener(); } private void setOnClickListener() { mBtnMore.setOnClickListener(this); mBtnAdd.setOnClickListener(this); mBtnSearch.setOnClickListener(this); } @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.more: mBtnMore.getTop(); int y = mBtnMore.getBottom() * 3 / 2; int x = getWindowManager().getDefaultDisplay().getWidth(); showMorePopupWindow(x, y); break; case R.id.add: mBtnAdd.getTop(); int y1 = mBtnAdd.getBottom() * 3 / 2; int x1 = getWindowManager().getDefaultDisplay().getWidth(); showAddPopupWindow(x1, y1); break; case R.id.search: Toast.makeText(getBaseContext(), "搜索", 1).show(); default: break; } } public void showMorePopupWindow(int x, int y) { layout = (LinearLayout) LayoutInflater.from(MainActivity.this).inflate( R.layout.popupwindow_dialog, null); listView = (ListView) layout.findViewById(R.id.lv_dialog); listView.setAdapter(new ArrayAdapter<String>(MainActivity.this, R.layout.text, R.id.tv_text, more)); popupWindow = new PopupWindow(MainActivity.this); popupWindow.setBackgroundDrawable(new BitmapDrawable()); popupWindow .setWidth(getWindowManager().getDefaultDisplay().getWidth() / 2); popupWindow.setHeight(420); popupWindow.setOutsideTouchable(true); popupWindow.setFocusable(true); popupWindow.setContentView(layout); popupWindow.showAtLocation(findViewById(R.id.main), Gravity.LEFT | Gravity.TOP, x, y);//需要指定Gravity,默認(rèn)情況是center. listView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { Toast.makeText(getBaseContext(), "您選擇了:"+more[arg2], 1).show(); popupWindow.dismiss(); popupWindow = null; } }); } /** * 點(diǎn)擊+時(shí)彈出的popuwindow */ public void showAddPopupWindow(int x, int y) { layout = (LinearLayout) LayoutInflater.from(MainActivity.this).inflate( R.layout.popupwindow_dialog, null); listView = (ListView) layout.findViewById(R.id.lv_dialog); listView.setAdapter(new ArrayAdapter<String>(MainActivity.this, R.layout.text, R.id.tv_text, add)); popupWindow = new PopupWindow(MainActivity.this); popupWindow.setBackgroundDrawable(new BitmapDrawable()); popupWindow .setWidth(getWindowManager().getDefaultDisplay().getWidth() / 2); popupWindow.setHeight(420); popupWindow.setOutsideTouchable(true); popupWindow.setFocusable(true); popupWindow.setContentView(layout); popupWindow.showAtLocation(findViewById(R.id.main), Gravity.LEFT | Gravity.TOP, x, y);//需要指定Gravity,默認(rèn)情況是center. listView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { Toast.makeText(getBaseContext(), "您選擇了:"+add[arg2], 1).show(); popupWindow.dismiss(); popupWindow = null; } }); } }
好了,主要代碼就完成了,實(shí)現(xiàn)效果如下所示
由于本人沒(méi)有圖片,所以彈出框的背景圖沒(méi)有處理,彈出框中的每一項(xiàng)的前面也沒(méi)有添加圖片,有需要得同學(xué)可以自行添加。(效果已經(jīng)出來(lái)了,細(xì)節(jié)沒(méi)有調(diào)整,請(qǐng)大家見(jiàn)諒)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android實(shí)現(xiàn)底部半透明彈出框PopUpWindow效果
- Android中自定義PopupWindow實(shí)現(xiàn)彈出框并帶有動(dòng)畫(huà)效果
- Android實(shí)現(xiàn)可輸入數(shù)據(jù)的彈出框
- Android 多種簡(jiǎn)單的彈出框樣式設(shè)置代碼
- Android使用Dialog風(fēng)格彈出框的Activity
- react-native 封裝選擇彈出框示例(試用ios&android)
- Android 仿微信朋友圈點(diǎn)贊和評(píng)論彈出框功能
- 高仿IOS的Android彈出框
- Android仿微信進(jìn)度彈出框的實(shí)現(xiàn)方法
相關(guān)文章
Android使用ViewDragHelper實(shí)現(xiàn)仿QQ6.0側(cè)滑界面(一)
這篇文章主要介紹了Android使用ViewDragHelper實(shí)現(xiàn)仿QQ6.0側(cè)滑界面(一)的相關(guān)資料,需要的朋友可以參考下2016-02-02Android中AlertDialog各種對(duì)話(huà)框的用法實(shí)例詳解
這篇文章主要介紹了Android中AlertDialog各種對(duì)話(huà)框的用法在項(xiàng)目開(kāi)發(fā)中經(jīng)常用的到,本文給大家介紹的非常詳細(xì),具有參考借鑒價(jià)值2016-04-04如果你在Android Studio碰到gradle的各種問(wèn)題就來(lái)看這篇文章吧(強(qiáng)烈建議收藏)
這篇文章主要介紹了你可能會(huì)在Android Studio碰到gradle的各種問(wèn)題,完美解決關(guān)于gradle的全部問(wèn)題,切記收藏以防需要的時(shí)候找不到了哦2021-08-08Android Studio里如何使用lambda表達(dá)式
這篇文章主要介紹了Android Studio里如何使用lambda表達(dá)式,需要的朋友可以參考下2017-05-05Android實(shí)現(xiàn)鍵盤(pán)彈出界面上移的實(shí)現(xiàn)思路
這篇文章主要介紹了Android實(shí)現(xiàn)鍵盤(pán)彈出界面上移的實(shí)現(xiàn)思路,需要的朋友可以參考下2018-04-04Android Flutter實(shí)現(xiàn)淘寶App的搜索推薦
這篇文章主要為大家詳細(xì)介紹了Android?Flutter如何模擬實(shí)現(xiàn)淘寶App的搜索推薦,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,感興趣的可以了解一下2023-07-07Android入門(mén)之TableLayout應(yīng)用解析(二)
這篇文章主要介紹了Android入門(mén)之TableLayout應(yīng)用,需要的朋友可以參考下2014-08-08Android開(kāi)發(fā)仿IOS滑動(dòng)開(kāi)關(guān)實(shí)現(xiàn)代碼
這篇文章主要介紹了 android開(kāi)發(fā)仿IOS滑動(dòng)開(kāi)關(guān)實(shí)現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下2017-05-05