国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

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

2019-11-09 16:53:56
字體:
供稿:網(wǎng)友

先上圖,4張:

項目代碼:http://download.csdn.NET/download/jianfengwen/9124745 (需要2個分)

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

代碼如下:

MainActivity類:

[java] view plain copypackage 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

[html] view plain copy<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,可以自己修改

[html] view plain copy<?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

[html] view plain copy<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

[html] view plain copy<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:從左邊入

[html] view plain copy<?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:從右邊出

[html] view plain copy<?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使用
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 报价| 海阳市| 云林县| 隆林| 大理市| 建瓯市| 惠来县| 进贤县| 武城县| 孝义市| 西吉县| 来凤县| 凤凰县| 恩平市| 达拉特旗| 盐池县| 蒲江县| 津市市| 确山县| 洛川县| 长治县| 博白县| 泸溪县| 沭阳县| 麦盖提县| 阳山县| 大埔区| 夏邑县| 吐鲁番市| 龙海市| 贡嘎县| 怀来县| 遂平县| 西林县| 锡林郭勒盟| 九龙县| 普洱| 五家渠市| 乌海市| 化隆| 庆阳市|