【Android】使用Popwindow實現左側快顯功能表層

來源:互聯網
上載者:User

PopupWindow可以實現浮層效果。

主要方法有:可以自訂view,通過LayoutInflator方法;可以出現和退出時顯示動畫;可以指定顯示位置等。 


為了將PopupWindow的多個功能展現并力求用簡單的代碼實現,編寫了一個點擊按鈕左側快顯功能表的功能,實現出現和退出時顯示動畫效果並點擊其他地區時彈出層自動消失。


Activity檔案
package com.app.test02;import android.app.Activity;import android.os.Bundle;import android.view.MotionEvent;import android.view.View;import android.view.View.OnClickListener;import android.view.View.OnTouchListener;import android.widget.Button;import android.widget.PopupWindow;import android.widget.Toast;public class PopwindowLeft extends Activity {// 聲明PopupWindow對象的引用private PopupWindow popupWindow;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_popupwindow_main);// 點擊按鈕快顯功能表Button pop = (Button) findViewById(R.id.popBtn);pop.setOnClickListener(popClick);}// 點擊彈出左側菜單的顯示方式OnClickListener popClick = new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubgetPopupWindow();// 這裡是位置顯示方式,在按鈕的左下角popupWindow.showAsDropDown(v);// 這裡可以嘗試其它效果方式,如popupWindow.showAsDropDown(v,// (screenWidth-dialgoWidth)/2, 0);// popupWindow.showAtLocation(findViewById(R.id.layout),// Gravity.CENTER, 0, 0);}};/** * 建立PopupWindow */protected void initPopuptWindow() {// TODO Auto-generated method stub// 擷取自訂布局檔案activity_popupwindow_left.xml的視圖View popupWindow_view = getLayoutInflater().inflate(R.layout.activity_popupwindow_left, null,false);// 建立PopupWindow執行個體,200,150分別是寬度和高度popupWindow = new PopupWindow(popupWindow_view, 200, 150, true);// 設定動畫效果popupWindow.setAnimationStyle(R.style.AnimationFade);// 點擊其他地方消失popupWindow_view.setOnTouchListener(new OnTouchListener() {@Overridepublic boolean onTouch(View v, MotionEvent event) {// TODO Auto-generated method stubif (popupWindow != null && popupWindow.isShowing()) {popupWindow.dismiss();popupWindow = null;}return false;}});// activity_popupwindow_left.xml視圖裡面的控制項Button open = (Button) popupWindow_view.findViewById(R.id.open);Button save = (Button) popupWindow_view.findViewById(R.id.save);Button close = (Button) popupWindow_view.findViewById(R.id.close);// activity_popupwindow_left.xml視圖裡面的控制項觸發的事件// 開啟open.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stub// 這裡可以執行相關操作Toast.makeText(PopwindowLeft.this, "開啟操作", 1000);// 對話方塊消失popupWindow.dismiss();}});// 儲存save.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stub// 這裡可以執行相關操作Toast.makeText(PopwindowLeft.this, "儲存操作", 1000);popupWindow.dismiss();}});// 關閉close.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stub// 這裡可以執行相關操作Toast.makeText(PopwindowLeft.this, "關閉操作", 1000);popupWindow.dismiss();}});}/*** * 擷取PopupWindow執行個體 */private void getPopupWindow() {if (null != popupWindow) {popupWindow.dismiss();return;} else {initPopuptWindow();}}}

PopupWindow主設定檔
<?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" >    <Button android:id="@+id/popBtn" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="彈出左側菜單" /> </LinearLayout>

PopupWindow快顯功能表
<?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:background="@android:color/darker_gray"    android:orientation="vertical" >    <Button        android:id="@+id/open"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:background="@android:color/darker_gray"        android:text="開啟" />    <Button        android:id="@+id/save"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:background="@android:color/darker_gray"        android:text="儲存" />    <Button        android:id="@+id/close"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:background="@android:color/darker_gray"        android:text="關閉" /></LinearLayout>

彈齣動畫XML在res檔案夾下,建立anim檔案夾。寫入如下兩個檔案。彈齣動畫in_lefttoright.xml
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" >    <!-- 定義從左向右進入的動畫 -->    <translate        android:duration="500"        android:fromXDelta="-100%"        android:toXDelta="0" /></set>

彈回動畫out_righttoleft.xml
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" >    <!-- 定義從右向左動畫退齣動畫 -->    <translate        android:duration="500"        android:fromXDelta="0"        android:toXDelta="-100%" /></set>

動畫管理在styles.xml中,添加如下管理代碼。
    <style name="AnimationFade">        <!-- PopupWindow左右彈出的效果 -->        <item name="android:windowEnterAnimation">@anim/in_lefttoright</item>        <item name="android:windowExitAnimation">@anim/out_righttoleft</item>    </style>



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.