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

首頁 > 系統 > Android > 正文

Android PopupWindow實現微信右上角的彈出菜單

2019-10-22 18:12:32
字體:
來源:轉載
供稿:網友

日常開發過程中對于PopupWindown的使用也是比較多的。這里給大家展示一下PopupWindow的使用。

修改activity_main.xml布局:

<RelativeLayout 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"  tools:context="${relativePackage}.${activityClass}" >   <RelativeLayout   android:layout_width="match_parent"   android:layout_height="50dip"   android:background="@android:color/holo_blue_dark">    <ImageView    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:layout_centerVertical="true"    android:layout_marginLeft="10dip"    android:background="@drawable/ic_launcher" />    <ImageView    android:id="@+id/rl_more"    android:layout_width="wrap_content"    android:layout_height="match_parent"    android:background="@drawable/ability_show_item_bg"    android:paddingLeft="15dp"    android:paddingRight="5dp"    android:layout_alignParentRight="true"    android:src="@drawable/actionbar_more_icon" />   </RelativeLayout>  </RelativeLayout> 

新建popup_window.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="@android:color/white"  android:gravity="center_horizontal"  android:orientation="vertical" >   <TextView   android:id="@+id/settings"   android:layout_width="match_parent"   android:layout_height="45dp"   android:gravity="center"   android:padding="12dp"   android:text="設置"   android:textSize="16sp" />   <View   android:layout_width="match_parent"   android:layout_height="1dp"   android:background="#BDBDBD" />   <TextView   android:id="@+id/about"   android:layout_width="match_parent"   android:layout_height="45dp"   android:gravity="center"   android:padding="12dp"   android:text="關于"   android:textSize="16sp" />   <View   android:layout_width="match_parent"   android:layout_height="1dp"   android:background="#BDBDBD" />   <TextView   android:id="@+id/ability_logout"   android:layout_width="match_parent"   android:layout_height="45dp"   android:gravity="center"   android:padding="12dp"   android:text="退出"   android:textSize="16sp" />  </LinearLayout> 

自定義PopupWindow類PopWindow

package com.syz.mypopupwindow;  import android.app.Activity; import android.content.Context; import android.graphics.drawable.ColorDrawable; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup.LayoutParams; import android.widget.PopupWindow;  /**  * <p>Title:PopWindow</p>  * <p>Description: 自定義PopupWindow</p>  * @author syz  * @date 2016-3-14  */ public class PopWindow extends PopupWindow{  private View conentView;  public PopWindow(final Activity context){   LayoutInflater inflater = (LayoutInflater) context     .getSystemService(Context.LAYOUT_INFLATER_SERVICE);   conentView = inflater.inflate(R.layout.popup_window, null);   int h = context.getWindowManager().getDefaultDisplay().getHeight();   int w = context.getWindowManager().getDefaultDisplay().getWidth();   // 設置SelectPicPopupWindow的View   this.setContentView(conentView);   // 設置SelectPicPopupWindow彈出窗體的寬   this.setWidth(w / 2 + 40);   // 設置SelectPicPopupWindow彈出窗體的高   this.setHeight(LayoutParams.WRAP_CONTENT);   // 設置SelectPicPopupWindow彈出窗體可點擊   this.setFocusable(true);   this.setOutsideTouchable(true);   // 刷新狀態   this.update();   // 實例化一個ColorDrawable顏色為半透明   ColorDrawable dw = new ColorDrawable(0000000000);   // 點back鍵和其他地方使其消失,設置了這個才能觸發OnDismisslistener ,設置其他控件變化等操作   this.setBackgroundDrawable(dw);   // mPopupWindow.setAnimationStyle(android.R.style.Animation_Dialog);   // 設置SelectPicPopupWindow彈出窗體動畫效果   this.setAnimationStyle(R.style.AnimationPreview);      conentView.findViewById(R.id.about).setOnClickListener(new OnClickListener() {     @Override    public void onClick(View arg0) {     //do something you need here     PopWindow.this.dismiss();    }   });   conentView.findViewById(R.id.ability_logout).setOnClickListener(new OnClickListener() {        @Override    public void onClick(View arg0) {     // do something before signing out     context.finish();     PopWindow.this.dismiss();    }   });   conentView.findViewById(R.id.settings).setOnClickListener(new OnClickListener() {        @Override    public void onClick(View arg0) {     // do something you need here          PopWindow.this.dismiss();    }   });  }    /**   * 顯示popupWindow   *   * @param parent   */  public void showPopupWindow(View parent) {   if (!this.isShowing()) {    // 以下拉方式顯示popupwindow    this.showAsDropDown(parent, parent.getLayoutParams().width / 2, 5);   } else {    this.dismiss();   }  } } 

添加自定義PopupWindow所需的style

AnimationPreview

<style name="AnimationPreview">   <item name="android:windowEnterAnimation">@anim/fade_in</item>   <item name="android:windowExitAnimation">@anim/fade_out</item>  </style> 

添加style所需的動畫

fade_in.xml

<?xml version="1.0" encoding="utf-8"?> <!-- 左上角擴大-->  <scale xmlns:android="http://schemas.android.com/apk/res/android"   android:interpolator="@android:anim/accelerate_decelerate_interpolator"   android:fromXScale="0.001"   android:toXScale="1.0"    android:fromYScale="0.001"    android:toYScale="1.0"    android:pivotX="100%"   android:pivotY="10%"   android:duration="200" /> 
    

fade_out.xml

<?xml version="1.0" encoding="utf-8"?> <!-- 左上角縮小 -->  <scale xmlns:android="http://schemas.android.com/apk/res/android"   android:interpolator="@android:anim/accelerate_decelerate_interpolator"   android:fromXScale="1.0"    android:toXScale="0.001"    android:fromYScale="1.0"    android:toYScale="0.001"    android:pivotX="100%"   android:pivotY="10%"   android:duration="200" /> 

最后在MainActivity類中使用

package com.syz.mypopupwindow;  import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener;  public class MainActivity extends Activity implements OnClickListener {   @Override  protected void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentView(R.layout.activity_main);  findViewById(R.id.rl_more).setOnClickListener(this);  }   @Override  public void onClick(View v) {  if(v.getId() == R.id.rl_more){   PopWindow popWindow = new PopWindow(this);   popWindow.showPopupWindow(findViewById(R.id.rl_more));  }  } } 

效果圖:

Android,PopupWindow,微信,彈出菜單

 Android,PopupWindow,微信,彈出菜單

Android,PopupWindow,微信,彈出菜單

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 宜兰县| 日喀则市| 兴仁县| 苍山县| 凤城市| 安多县| 南郑县| 卓尼县| 三亚市| 邯郸县| 江川县| 祥云县| 临猗县| 绩溪县| 高清| 四平市| 寿光市| 富裕县| 嘉定区| 玛纳斯县| 西畴县| 吴江市| 鄂尔多斯市| 左权县| 郧西县| 靖远县| 汤阴县| 集贤县| 莒南县| 托克托县| 铅山县| 沁水县| 灵山县| 额尔古纳市| 库尔勒市| 高清| 大方县| 富源县| 铅山县| 彭山县| 彭阳县|