Android用PopupWindow實現彈出菜單實例

jopen 11年前發布 | 69K 次閱讀 Android Android開發 移動開發

step1:新建項目PopWindow,并導入菜單項使用的圖片到/res/drawable目錄下

Android用PopupWindow實現彈出菜單實例               (項目總覽圖)                                        Android用PopupWindow實現彈出菜單實例            (drawable目錄截圖)       

step2:設置應用的UI界面 

a.應用的總體界面,main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/main"
    >
<Button  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/button"
    android:onClick="openPopWindow"
    />
</LinearLayout>

 

b.彈出菜單的界面,popwindow.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:background="@drawable/rectangle">  <!-- 設置一個手繪的長方形背景 -->
    <GridView android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:numColumns="4"
        android:horizontalSpacing="10dp" android:verticalSpacing="10dp"
        android:id="@+id/gridView" />
</LinearLayout>

其中的android:background="@drawable/rectangle"是引用rectangle.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient android:startColor="#1DC9CD" android:endColor="#A2E0FB"
        android:angle="270" />
    <padding android:left="2dp" android:top="2dp" android:right="2dp"
        android:bottom="2dp" />
</shape>


c.每個菜單項的界面,grid_item.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" 
    android:gravity="center"
    >
    <ImageView 
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:id="@+id/imageView"
        />
    <TextView 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:textSize="16sp"
        android:textColor="#000099"
        android:id="@+id/textView"
        />
</LinearLayout>


d:為菜單設置一個style,用于指定菜單彈出時和退出時的動畫效果  styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="animation">
        <item name="android:windowEnterAnimation">@anim/enter</item> 
     <item name="android:windowExitAnimation">@anim/exit</item> 
    </style>
</resources>

其中enter.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false">
    <translate
        android:fromYDelta="100%p"
        android:toYDelta="0" 
        android:duration="500"
        />
    <alpha
        android:fromAlpha="0.7"
        android:toAlpha="1.0" 
        android:duration="300"
        />   
</set>

exit.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false">
    <translate
        android:fromYDelta="0"
        android:toYDelta="100%p" 
        android:duration="2000"
        />
    <alpha
        android:fromAlpha="1.0"
        android:toAlpha="0.5" 
        android:duration="1000"
        />   
</set>

 

step3:MainActivity.java

package cn.roco.popwindow;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ListAdapter;
import android.widget.PopupWindow;
import android.widget.SimpleAdapter;

public class MainActivity extends Activity {
    private PopupWindow popupWindow;
    private View parent;
    /**菜單彈出來時候的菜單項圖案*/
    private int[] images = { R.drawable.i1, R.drawable.i2, R.drawable.i3,
            R.drawable.i4, R.drawable.i5, R.drawable.i6, R.drawable.i7,
            R.drawable.i8 };
    /**菜單彈出來時候的菜單項文字*/
    private String[] names = { "搜索", "文件管理", "下載管理", "全屏", "網址", "書簽", "加入書簽",
            "分享頁面" };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        /**PopupWindow的界面*/
        View contentView = getLayoutInflater()
                .inflate(R.layout.popwindow, null);
        /**網格布局界面*/
        GridView gridView = (GridView) contentView.findViewById(R.id.gridView);
        /**設置網格布局的適配器*/
        gridView.setAdapter(getAdapter());
        /**設置網格布局的菜單項點擊時候的Listener*/
        gridView.setOnItemClickListener(new ItemClickListener());
        /**初始化PopupWindow*/
        popupWindow = new PopupWindow(contentView,
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setFocusable(true);// 取得焦點
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        /**設置PopupWindow彈出和退出時候的動畫效果*/
        popupWindow.setAnimationStyle(R.style.animation);

        parent = this.findViewById(R.id.main);
    }

    private final class ItemClickListener implements OnItemClickListener{
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            if (popupWindow.isShowing()) {
                popupWindow.dismiss();//關閉
            }
        }
    }

    /**返回網格布局的適配器*/
    private ListAdapter getAdapter() {
        List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>();
        for (int i = 0; i < images.length; i++) {
            HashMap<String, Object> item = new HashMap<String, Object>();
            item.put("image", images[i]);
            item.put("name", names[i]);
            data.add(item);
        }
        SimpleAdapter simpleAdapter = new SimpleAdapter(this, data,
                R.layout.grid_item, new String[] { "image", "name" },
                new int[] { R.id.imageView, R.id.textView });
        return simpleAdapter;
    }

    public void openPopWindow(View v) {
        /**設置PopupWindow彈出后的位置*/
        popupWindow.showAtLocation(parent, Gravity.BOTTOM, 0, 0);
    }
}

step4:AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="cn.roco.popwindow"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="8" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".MainActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>

step5:運行效果

Android用PopupWindow實現彈出菜單實例                         Android用PopupWindow實現彈出菜單實例

 

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!