天天看點

popupwindow和listview

在使用popupwindow的時候,有一個不好的地方就是不太好設定彈出窗體的大小。如果指定絕對大小,那麼對于不同分辨率不同尺寸的手機來說,顯示出來效果會不同,進而導緻使用者體驗不佳。

為了達到popupwindow能夠自适配布局大小,可以在設定長寬時候指定:

popupwindow.setwidth(layoutparams.wrap_content);    

popupwindow.setheight(layoutparams.wrap_content);   

下面我就來具體講解一下在popupwindow中使用listview的方法。

首先貼出的是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">  

    <button android:id="@+id/button"   

        android:layout_width="match_parent"  

        android:layout_height="wrap_content"   

        android:text="彈出popupwindow" />  

</linearlayout>  

然後貼出的是popupwindow中顯示的listview_demo.xml

    android:orientation="vertical" android:layout_width="match_parent"  

    android:layout_height="match_parent">  

    <listview android:id="@+id/listview"   

        android:layout_height="match_parent" />  

再貼出的是listview顯示的每一項item.xml

    <textview android:id="@+id/item"  

        android:layout_width="wrap_content"  

        android:textsize="18sp" />  

最後貼出的是java代碼popupwindowdemoactivity.java

package xmu.zgy;  

import java.util.arraylist;  

import java.util.hashmap;  

import java.util.list;  

import java.util.map;  

import android.app.activity;  

import android.os.bundle;  

import android.view.layoutinflater;  

import android.view.view;  

import android.view.view.onclicklistener;  

import android.view.viewgroup.layoutparams;  

import android.widget.button;  

import android.widget.listview;  

import android.widget.popupwindow;  

import android.widget.simpleadapter;  

/** 

 *  

 * @author yulongfei 

 * @blog blog.csdn.net/zgyulongfei 

 * 

 */  

public class popupwindowdemoactivity extends activity {  

    private button button;  

    private popupwindow popupwindow;  

    private listview listview;  

    @override  

    public void oncreate(bundle savedinstancestate) {  

        super.oncreate(savedinstancestate);  

        setcontentview(r.layout.main);  

        initcontrols();  

    }  

    private void initcontrols() {  

        layoutinflater inflater = layoutinflater.from(this);  

        view view = inflater.inflate(r.layout.listview_demo, null);  

        simpleadapter adapter = new simpleadapter(this, getdata(),   

                r.layout.item,  

                new string[] { "text" },  

                new int[] { r.id.item });  

        listview = (listview) view.findviewbyid(r.id.listview);  

        listview.setadapter(adapter);  

        //自适配長、框設定  

        popupwindow = new popupwindow(view, layoutparams.wrap_content,  

                layoutparams.wrap_content);  

        popupwindow.setbackgrounddrawable(getresources().getdrawable(r.drawable.bg));  

        popupwindow.setoutsidetouchable(true);  

        popupwindow.setanimationstyle(android.r.style.animation_dialog);  

        popupwindow.update();  

        popupwindow.settouchable(true);  

        popupwindow.setfocusable(true);  

        button = (button) findviewbyid(r.id.button);  

        button.setonclicklistener(new onclicklistener() {  

            @override  

            public void onclick(view v) {  

                if (!popupwindow.isshowing()) {  

                    popupwindow.showasdropdown(button, 0, 0);  

                }  

            }  

        });  

    private list<map<string, string>> getdata() {  

        list<map<string, string>> list = new arraylist<map<string, string>>();  

        map<string, string> map = new hashmap<string, string>();  

        map.put("text", "中國");  

        list.add(map);  

        map = new hashmap<string, string>();  

        map.put("text", "加油");  

        map.put("text", "釣魚島是中國的");  

        map.put("text", "!!");  

        return list;  

}  

運作結果圖如下所示:

popupwindow和listview

咦?不是已經設定自适應長和寬了嗎?為什麼顯示出來的效果還是占滿螢幕的寬度呢?

為了讓popupwindow能夠自适應listview的内容,需要在listview_demo.xml添加一項:

    <textview android:layout_width="wrap_content"  

        android:layout_height="0dp"   

        android:textsize="18sp"   

        android:text="釣魚島是中國的" />  

先看顯示結果再做解釋:

popupwindow和listview

看到了嗎?很神奇吧,popupwindow的寬度進行了自适配。

因為我在xml中加了一個textview,然後設定了高度為0,這樣他就看不到了。

最重要的步驟是我在textview中設定了android:text="釣魚島是中國的",這一句是關鍵性的動作。

因為textview才是自适配的砝碼,要在text中寫上你的listview中最長的那個字元。上述demo中,所有顯示的文字{中國,加油,釣魚島是中國的,!!!}中”釣魚島是中國的“是最長的。

雖然方法不太好,但是實作了效果。如果你遇到這樣的問題,可以試試這種方式。

希望本文能夠幫到有需要的朋友!

<a target="_blank" href="http://download.csdn.net/detail/zgyulongfei/4566716">點選下載下傳本文demo。</a>

繼續閱讀