最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

PopupWindow仿微信浮層彈出框效果

 更新時間:2018年04月25日 11:31:40   作者:隨風(fēng)漂泊_  
這篇文章主要為大家詳細介紹了PopupWindow仿微信浮層彈出框效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下

最近公司項目需要實現(xiàn)類似微信的浮層彈出框。研究發(fā)現(xiàn)是用PopupWindow實現(xiàn)的。而且可以自定義位置以及出現(xiàn)和退出時的動畫,由于太晚了就不實現(xiàn)動畫了,需要得同學(xué)請自己研究下。由于本人新手其中的不足和缺點請見諒。

代碼如下:

首先是定義頂部按鈕的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> 

接著是每一個彈出框顯示的文字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 標題 
 *@description 仿微信頂部彈出框的popuwindow 
 *@author zhengxiaolin 
 *@version 1.0 
 *@created 2014-5-23 上午12:11:11 
 *@changeRecord [修改記錄]<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 = {"我的相冊","我的收藏","我的銀行卡","設(shè)置","意見反饋"}; 
 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,默認情況是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; 
  } 
 }); 
 } 
 /** 
 * 點擊+時彈出的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,默認情況是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; 
  } 
 }); 
 } 
} 

好了,主要代碼就完成了,實現(xiàn)效果如下所示

由于本人沒有圖片,所以彈出框的背景圖沒有處理,彈出框中的每一項的前面也沒有添加圖片,有需要得同學(xué)可以自行添加。(效果已經(jīng)出來了,細節(jié)沒有調(diào)整,請大家見諒)

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android 使用<layer-list>實現(xiàn)微信聊天輸入框功能

    Android 使用<layer-list>實現(xiàn)微信聊天輸入框功能

    <layer-list> 標簽可以設(shè)置LayerDrawable,一種有層次的Drawable疊加效果,<layer-list> 可以包含多個 <item>標簽。這篇文章主要介紹了Android 使用<layer-list>實現(xiàn)微信聊天輸入框,需要的朋友可以參考下
    2017-05-05
  • Android 異步加載圖片的實例代碼

    Android 異步加載圖片的實例代碼

    異步加載圖片主要是利用多線程進行下載、圖片弱引用緩存和Handler操作UI進行實現(xiàn)的。
    2013-05-05
  • Android四大組件之Activity深入解讀生命周期

    Android四大組件之Activity深入解讀生命周期

    雖然說我們天天都在使用Activity,但是你真的對Activity的生命機制完全了解了嗎?Activity的生命周期方法只有七個,但是其實那只是默認的情況。也就是說在其他情況下,Activity的生命周期可能不會是按照我們以前所知道的流程,這就要說到Activity的啟動模式
    2022-07-07
  • Android自定義View實現(xiàn)五子棋游戲

    Android自定義View實現(xiàn)五子棋游戲

    這篇文章主要為大家詳細介紹了Android自定義View實現(xiàn)五子棋游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-11-11
  • API處理Android安全距離詳情

    API處理Android安全距離詳情

    這篇文章主要介紹了API處理Android安全距離詳情,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的朋友可以參考一下
    2022-06-06
  • Android studio 連接手機調(diào)試操作步驟

    Android studio 連接手機調(diào)試操作步驟

    這篇文章主要介紹了Android studio 連接手機調(diào)試操作步驟,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-05-05
  • Jetpack Compose布局的使用詳細介紹

    Jetpack Compose布局的使用詳細介紹

    Jetpack Compose是用于構(gòu)建原生Android界面的新工具包。它可簡化并加快Android上的界面開發(fā),使用更少的代碼、強大的工具和直觀的 Kotlin API,快速讓應(yīng)用生動而精彩
    2022-09-09
  • 詳解Android中motion_toast的使用

    詳解Android中motion_toast的使用

    我們通常會用 toast(也叫吐司)來顯示提示信息,例如網(wǎng)絡(luò)請求錯誤,校驗錯誤等等。本文為大家介紹一個非常有趣的toast組件 —— motion_toast,感興趣的可以了解一下
    2022-06-06
  • Android Studio的中文亂碼問題解決方法

    Android Studio的中文亂碼問題解決方法

    Android Studio安裝后發(fā)現(xiàn)所有的中文,不管是界面上的還是輸出的log中的中文都變成小框框,具體的解決方法如下,感興趣的朋友可以參考下哈
    2013-06-06
  • Android訪問php取回json數(shù)據(jù)實例

    Android訪問php取回json數(shù)據(jù)實例

    Android訪問php取回json數(shù)據(jù),實現(xiàn)代碼如下,遇到訪問網(wǎng)絡(luò)的權(quán)限不足在AndroidManifest.xml中,需要進行如下配置
    2013-06-06

最新評論

即墨市| 五原县| 腾冲县| 汤原县| 太仓市| 成武县| 巨野县| 台南县| 长阳| 乌鲁木齐市| 遵化市| 搜索| 宜君县| 永宁县| 柞水县| 论坛| 江都市| 建德市| 花莲县| 常熟市| 巴塘县| 商河县| 图木舒克市| 高密市| 双辽市| 泸州市| 印江| 龙陵县| 宁晋县| 平度市| 桃江县| 抚宁县| 桐乡市| 枣强县| 溆浦县| 镇远县| 万全县| 东乡县| 衡山县| 合川市| 攀枝花市|