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

Android PopupWindow實現(xiàn)右側(cè)、左側(cè)和底部彈出菜單

 更新時間:2016年11月24日 17:25:48   作者:jianfengwen  
這篇文章主要為大家詳細(xì)介紹了Android PopupWindow實現(xiàn)右側(cè)、左側(cè)和底部彈出菜單的相關(guān)代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本教程為大家分享了Android PopupWindow彈出菜單的具體代碼,供大家參考,具體內(nèi)容如下

項目代碼:http://xiazai.jb51.net/201611/yuanma/PopupLeftMenu(jb51.net).rar

項目SDK是5.1,建議將代碼拷到自己的工程中去

代碼如下:

MainActivity類:

package com.example.popupleftmenu; 
 
import android.app.Activity; 
import android.content.Context; 
import android.graphics.drawable.ColorDrawable; 
import android.os.Bundle; 
import android.view.Gravity; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.View.OnTouchListener; 
import android.view.ViewGroup.LayoutParams; 
import android.view.WindowManager; 
import android.widget.Button; 
import android.widget.PopupWindow; 
import android.widget.Toast; 
 
public class MainActivity extends Activity { 
 
 private Context context = null; 
 private PopupWindow popupWindow; 
 private int from = 0; 
  
 @Override 
 protected void onCreate(Bundle savedInstanceState) { 
  super.onCreate(savedInstanceState); 
  context = this; 
  setContentView(R.layout.activity_main); 
  Button popLeftBtn = (Button)findViewById(R.id.pop_left_btn); 
  Button popRightBtn = (Button)findViewById(R.id.pop_right_btn); 
  Button popBottomBtn = (Button)findViewById(R.id.pop_bottom_btn); 
  popLeftBtn.setOnClickListener(popClick); 
  popRightBtn.setOnClickListener(popClick); 
  popBottomBtn.setOnClickListener(popClick); 
 } 
 
  
 OnClickListener popClick = new OnClickListener() { 
   
  @Override 
  public void onClick(View v) { 
   switch(v.getId()){ 
    case R.id.pop_left_btn:{ 
     from = Location.LEFT.ordinal(); 
     break; 
    } 
    case R.id.pop_right_btn:{ 
     from = Location.RIGHT.ordinal(); 
     break; 
    } 
    case R.id.pop_bottom_btn:{ 
     from = Location.BOTTOM.ordinal(); 
     break; 
    } 
   } 
    
   //調(diào)用此方法,menu不會頂置 
   //popupWindow.showAsDropDown(v); 
   initPopupWindow(); 
    
  } 
 }; 
 /** 
  * 添加新筆記時彈出的popWin關(guān)閉的事件,主要是為了將背景透明度改回來 
  * 
  */ 
 class popupDismissListener implements PopupWindow.OnDismissListener{ 
 
  @Override 
  public void onDismiss() { 
   backgroundAlpha(1f); 
  } 
   
 } 
  
  
 protected void initPopupWindow(){ 
  View popupWindowView = getLayoutInflater().inflate(R.layout.pop, null); 
  //內(nèi)容,高度,寬度 
  if(Location.BOTTOM.ordinal() == from){ 
   popupWindow = new PopupWindow(popupWindowView, LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT, true); 
  }else{ 
   popupWindow = new PopupWindow(popupWindowView, LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT, true); 
  } 
  //動畫效果 
  if(Location.LEFT.ordinal() == from){ 
   popupWindow.setAnimationStyle(R.style.AnimationLeftFade); 
  }else if(Location.RIGHT.ordinal() == from){ 
   popupWindow.setAnimationStyle(R.style.AnimationRightFade); 
  }else if(Location.BOTTOM.ordinal() == from){ 
   popupWindow.setAnimationStyle(R.style.AnimationBottomFade); 
  } 
  //菜單背景色 
  ColorDrawable dw = new ColorDrawable(0xffffffff); 
  popupWindow.setBackgroundDrawable(dw); 
  //寬度 
  //popupWindow.setWidth(LayoutParams.WRAP_CONTENT); 
  //高度 
  //popupWindow.setHeight(LayoutParams.FILL_PARENT); 
  //顯示位置 
  if(Location.LEFT.ordinal() == from){ 
   popupWindow.showAtLocation(getLayoutInflater().inflate(R.layout.activity_main, null), Gravity.LEFT, 0, 500); 
  }else if(Location.RIGHT.ordinal() == from){ 
   popupWindow.showAtLocation(getLayoutInflater().inflate(R.layout.activity_main, null), Gravity.RIGHT, 0, 500); 
  }else if(Location.BOTTOM.ordinal() == from){ 
   popupWindow.showAtLocation(getLayoutInflater().inflate(R.layout.activity_main, null), Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0); 
  } 
  //設(shè)置背景半透明 
  backgroundAlpha(0.5f); 
  //關(guān)閉事件 
  popupWindow.setOnDismissListener(new popupDismissListener()); 
   
  popupWindowView.setOnTouchListener(new OnTouchListener() { 
    
   @Override 
   public boolean onTouch(View v, MotionEvent event) { 
    /*if( popupWindow!=null && popupWindow.isShowing()){ 
     popupWindow.dismiss(); 
     popupWindow=null; 
    }*/ 
    // 這里如果返回true的話,touch事件將被攔截 
    // 攔截后 PopupWindow的onTouchEvent不被調(diào)用,這樣點擊外部區(qū)域無法dismiss 
    return false; 
   } 
  }); 
   
  Button open = (Button)popupWindowView.findViewById(R.id.open); 
  Button save = (Button)popupWindowView.findViewById(R.id.save); 
  Button close = (Button)popupWindowView.findViewById(R.id.close); 
   
   
  open.setOnClickListener(new OnClickListener() { 
    
   @Override 
   public void onClick(View v) { 
    Toast.makeText(context, "Open", Toast.LENGTH_LONG).show(); 
    popupWindow.dismiss(); 
   } 
  }); 
   
  save.setOnClickListener(new OnClickListener() { 
    
   @Override 
   public void onClick(View v) { 
    Toast.makeText(context, "Open", Toast.LENGTH_LONG).show(); 
    popupWindow.dismiss(); 
   } 
  }); 
   
  close.setOnClickListener(new OnClickListener() { 
    
   @Override 
   public void onClick(View v) { 
    Toast.makeText(context, "Open", Toast.LENGTH_LONG).show(); 
    popupWindow.dismiss(); 
   } 
  }); 
 } 
  
 /** 
  * 設(shè)置添加屏幕的背景透明度 
  * @param bgAlpha 
  */ 
 public void backgroundAlpha(float bgAlpha) 
 { 
  WindowManager.LayoutParams lp = getWindow().getAttributes(); 
  lp.alpha = bgAlpha; //0.0-1.0 
  getWindow().setAttributes(lp); 
 } 
 /** 
  * 菜單彈出方向 
  * 
  */ 
 public enum Location { 
 
  LEFT, 
  RIGHT, 
  TOP, 
  BOTTOM; 
   
 } 
} 

兩個布局文件:

1.activity_main.xml,就三個Button

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 xmlns:tools="http://schemas.android.com/tools" 
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent" 
 android:orientation="vertical"> 
 
 <Button 
  android:id="@+id/pop_left_btn" 
  android:layout_width="fill_parent" 
  android:layout_height="wrap_content" 
  android:text="@string/pop_left"/> 
  
 <Button 
  android:id="@+id/pop_right_btn" 
  android:layout_width="fill_parent" 
  android:layout_height="wrap_content" 
  android:text="@string/pop_right"/> 
  
 <Button 
  android:id="@+id/pop_bottom_btn" 
  android:layout_width="fill_parent" 
  android:layout_height="wrap_content" 
  android:text="@string/pop_bottom"/> 
  
 
</LinearLayout>

2. pop.xml,也是三個Button,可以自己修改

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent" 
 android:orientation="vertical" > 
  
 <!-- <LinearLayout 
  android:layout_width="wrap_content" 
  android:layout_height="fill_parent" 
  android:orientation="vertical" 
  android:background="#ffffff"> --> 
   
  <Button android:id="@+id/open" 
   android:layout_width="fill_parent" 
   android:layout_height="wrap_content" 
   android:text="@string/open"/> 
   
  <Button android:id="@+id/save" 
   android:layout_width="fill_parent" 
   android:layout_height="wrap_content" 
   android:text="@string/save"/> 
   
  <Button android:id="@+id/close" 
   android:layout_width="fill_parent" 
   android:layout_height="wrap_content" 
   android:text="@string/close"/> 
   
 <!-- </LinearLayout> --> 
  
</LinearLayout> 

strings.xml

<string name="pop_left">彈出左側(cè)菜單</string> 
 <string name="pop_right">彈出右側(cè)菜單</string> 
 <string name="pop_bottom">彈出底部菜單</string> 
 <string name="open">打開</string> 
 <string name="save">保存</string> 
 <string name="close">關(guān)閉</string> 

styles.xml

<style name="AnimationLeftFade"> 
  <item name="android:windowEnterAnimation">@anim/in_lefttoright</item> 
  <item name="android:windowExitAnimation">@anim/out_righttoleft</item> 
 </style> 
  
 <style name="AnimationRightFade"> 
  <item name="android:windowEnterAnimation">@anim/in_righttoleft</item> 
  <item name="android:windowExitAnimation">@anim/out_lefttoright</item> 
 </style> 
  
 <style name="AnimationBottomFade"> 
  <item name="android:windowEnterAnimation">@anim/in_bottomtotop</item> 
  <item name="android:windowExitAnimation">@anim/out_toptobottom</item> 
 </style> 

左邊彈出菜單動畫文件:

in_lefttoright.xml:從左邊入

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android"> 
  
 <translate 
  android:fromXDelta="-100%" 
  android:toXDelta="0" 
  android:duration="500"/> 
  
</set> 

out_righttoleft.xml:從右邊出

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android"> 
  
 <translate android:fromXDelta="0" 
  android:toXDelta="-100%" 
  android:duration="500"/> 
 
</set> 

其他動畫文件自己參考寫,就是fromXDelta, fromYDelta, toXDelta和toYDelta使用。

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

相關(guān)文章

  • Android開發(fā)之圖片壓縮工具類完整實例

    Android開發(fā)之圖片壓縮工具類完整實例

    這篇文章主要介紹了Android開發(fā)之圖片壓縮工具類,結(jié)合完整實例形式分析了Android針對圖片壓縮的相關(guān)屬性設(shè)置與轉(zhuǎn)換操作實現(xiàn)技巧,需要的朋友可以參考下
    2017-11-11
  • Android實現(xiàn)輪播圖片效果

    Android實現(xiàn)輪播圖片效果

    這篇文章主要為大家詳細(xì)介紹了Android實現(xiàn)輪播圖片效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-12-12
  • Flutter Navigator路由傳參的實現(xiàn)

    Flutter Navigator路由傳參的實現(xiàn)

    本文主要介紹了Flutter Navigator路由傳參的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-04-04
  • AsyncTask的三個屬性值和四個步驟

    AsyncTask的三個屬性值和四個步驟

    本文主要介紹了AsyncTask的三個屬性值和四個步驟,具有一定的參考價值,下面跟著小編一起來看下吧
    2017-01-01
  • Flutter網(wǎng)絡(luò)請求的3種簡單實現(xiàn)方法

    Flutter網(wǎng)絡(luò)請求的3種簡單實現(xiàn)方法

    這篇文章主要給大家介紹了給大家Flutter網(wǎng)絡(luò)請求的3種簡單實現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Flutter具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • Jetpack Compose實現(xiàn)列表和動畫效果詳解

    Jetpack Compose實現(xiàn)列表和動畫效果詳解

    這篇文章主要為大家詳細(xì)講講Jetpack Compose實現(xiàn)列表和動畫效果的方法步驟,文中的代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2022-06-06
  • 可伸縮的textview詳解(推薦)

    可伸縮的textview詳解(推薦)

    下面小編就為大家?guī)硪黄缮炜s的textview詳解(推薦)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-03-03
  • Android快速分析apk工具aapt的使用教程

    Android快速分析apk工具aapt的使用教程

    這篇文章主要介紹了Android快速分析apk工具aapt的使用教程,本文講解了什么是aapt、主要用法、使用aapt、查看apk的基本信息、查看基本信息、查看應(yīng)用權(quán)限等內(nèi)容,需要的朋友可以參考下
    2015-04-04
  • android實現(xiàn)上下左右滑動界面布局

    android實現(xiàn)上下左右滑動界面布局

    這篇文章主要為大家詳細(xì)介紹了android實現(xiàn)上下左右滑動的界面布局,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-12-12
  • Android 使用Vibrator服務(wù)實現(xiàn)點擊按鈕帶有震動效果

    Android 使用Vibrator服務(wù)實現(xiàn)點擊按鈕帶有震動效果

    這篇文章主要介紹了Android 使用Vibrator服務(wù)實現(xiàn)點擊按鈕帶有震動效果,,本文通過實例圖文相結(jié)合給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)火鍋工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-06-06

最新評論

静海县| 儋州市| 苍溪县| 县级市| 合肥市| 栾川县| 富平县| 新沂市| 洪湖市| 荃湾区| 长寿区| 丹凤县| 新宁县| 鄂托克前旗| 仪征市| 星座| 曲沃县| 马尔康县| 邵阳县| 刚察县| 禹城市| 宣汉县| 当阳市| 比如县| 沁水县| 河池市| 井陉县| 柳林县| 鄂托克前旗| 习水县| 普洱| 肇州县| 彭山县| 辛集市| 山西省| 温州市| 荆州市| 日照市| 宝山区| 周宁县| 博兴县|