[置頂] Android常用UI控制項之PopupWindow

來源:互聯網
上載者:User

        今天我們學習如何?PopupWindow泡泡視窗,效果類似於快顯功能表,但是實現方式截然不同,下面給出該情境的案例:

一、案例代碼

1.AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.android.popupwindow"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="15" />    <application        android:icon="@drawable/ic_launcher"        android:label="@string/app_name" >        <activity            android:name=".PopupWindowActivity"            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>

2.string.xml

<resources>    <string name="app_name">泡泡視窗</string>    <string name="button">開啟PopupWindow</string></resources>

3.主介面布局檔案:main.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:id="@+id/main" >    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:onClick="openPopupWindow"        android:text="@string/button" /></LinearLayout>

4.PopupWindow布局檔案:popupwindow.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:background="@drawable/background" >    <!-- <TextView         android:layout_width="match_parent"        android:layout_height="match_parent"        android:text="我是PopupWindow"/> -->                <GridView            android:id="@+id/gridView"            android:layout_width="match_parent"            android:layout_height="match_parent"             android:numColumns="4"            android:verticalSpacing="10dp"            android:horizontalSpacing="10dp"/>            </LinearLayout>

5.GridViewItem布局檔案: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:id="@+id/imageView"        android:layout_width="wrap_content"        android:layout_height="wrap_content"/>    <TextView         android:id="@+id/textView"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:gravity="center"        android:textSize="16sp"        android:textColor="#000099"/></LinearLayout>

6.PopupWindow彈進動畫檔案: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.5"         android:toAlpha="1.0"        android:duration="300" /></set>

7.PopupWindow彈齣動畫檔案: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="500" />        <alpha        android:fromAlpha="1.0"         android:toAlpha="0.5"        android:duration="300" /></set>

8.PopupWindowActivity.java

package com.android.popupwindow;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 PopupWindowActivity extends Activity {    private PopupWindow popupWindow;    private View parent;        // 設定GridView Item圖片資源    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};        // 設定GridView Item名稱資源    private String[] names = {            "搜尋",            "檔案",            "下載",            "全屏",            "網址",            "書籤",            "加入",            "分享"    };    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        // 構建PopupWindow內容視圖        View contentView = getLayoutInflater().inflate(R.layout.popupwindow, null);        GridView gridView = (GridView) contentView.findViewById(R.id.gridView);        gridView.setAdapter(getAdapter());        gridView.setOnItemClickListener(new OnItemClickListener() {            @Override            public void onItemClick(AdapterView<?> parent, View view, int position,                    long id) {                if(popupWindow.isShowing()) {                    popupWindow.dismiss();                }            }        });                // 初始化PopupWindow控制項        popupWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);        // 允許PopupWindow擷取焦點        popupWindow.setFocusable(true);        // 設定PopupWindow背景圖        popupWindow.setBackgroundDrawable(new BitmapDrawable());        // 設定PopupWindow彈進彈齣動畫效果        popupWindow.setAnimationStyle(R.style.animation);        // 找到主介面布局檔案        parent = findViewById(R.id.main);    }    /**     * 構建用於填充GridView資料條目的適配器     * @return ListAdapter     */    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 adapter = new SimpleAdapter(this, data, R.layout.grid_item,                 new String[]{"image", "name"}, new int[]{R.id.imageView, R.id.textView});        return adapter;    }    /**     * 設定在頁面底端顯示PopupWindow     * @param v     */    public void openPopupWindow(View v) {        popupWindow.showAtLocation(parent, Gravity.BOTTOM, 0, 0);    }}

 

        注意:要將、、、、、、、存放到drawable-hdpi檔案夾下。

二、案例效果

三、代碼下載:http://download.csdn.net/detail/leverage_1229/5470815

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.