五分鐘教你學(xué)會PopupWindow

于自定義窗口橙垢,Android提供了PopupWindow焰檩,簡單實(shí)用鲁沥。
下面我們來看看今天demo實(shí)現(xiàn)的效果:

彈出PopupWindow

PopupWindow的構(gòu)造函數(shù)

public PopupWindow(View contentView, int width, int height, boolean focusable)

其中contentView為要顯示的view封恰,width和height為寬和高洲炊,
值為像素值腋逆,可以是MATCHT_PARENT和WRAP_CONTENT
來設(shè)置岳锁,如果focusable為false圆米,在一個(gè)Activity彈出一個(gè)PopupWindow,按返回鍵声畏,由于PopupWindow沒有焦點(diǎn)撞叽,會直接退出Activity姻成。如果focusable為true,PopupWindow彈出后愿棋,所有的觸屏和物理按鍵都有PopupWindows處理科展。
如果PopupWindow中有Editor的話,focusable要為true糠雨。

下面來看一個(gè)簡單的demo
主界面:就簡單放一個(gè)button,點(diǎn)擊彈出我們需要的PopupWindow

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    xmlns:tools="http://schemas.android.com/tools"  
    android:id="@+id/container"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent" >  
    <Button  
        android:id="@+id/btnOpen"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_centerInParent="true"  
        android:text="@string/app_name" />  
</RelativeLayout>  

popupwindow 界面:放了2個(gè)button 和一個(gè)文本框才睹,用來輸入值

<?xml version="1.0" encoding="utf-8"?>  
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    android:background="#b5555555" >  
    <LinearLayout  
        android:layout_width="match_parent"  
        android:layout_height="wrap_content"  
        android:layout_alignParentBottom="true"  
        android:background="#eee"  
        android:orientation="vertical" >  
        <EditText  
            android:id="@+id/leaveword"  
            android:layout_width="fill_parent"  
            android:layout_height="wrap_content"  
            android:layout_marginBottom="20dp"  
            android:layout_marginLeft="20dp"  
            android:layout_marginRight="20dp"  
            android:layout_marginTop="20dp"  
            android:gravity="top"  
            android:hint="說點(diǎn)什么吧~"  
            android:inputType="textMultiLine"  
            android:lineSpacingExtra="6.0dp"  
            android:maxHeight="150dp"  
            android:minHeight="100dp"  
            android:paddingLeft="10.0dp"  
            android:paddingRight="10.0dp"  
            android:paddingTop="10.0dp" />  
        <LinearLayout  
            android:layout_width="fill_parent"  
            android:layout_height="wrap_content"  
            android:layout_marginBottom="20dp"  
            android:gravity="center"  
            android:orientation="horizontal" >  
            <Button  
                android:id="@+id/confirmButton"  
                android:layout_width="80.0dip"  
                android:layout_height="wrap_content"  
                android:gravity="center"  
                android:text="發(fā)表"  
                android:textColor="#fff"  
                android:textSize="16.0sp" />  
            <Button  
                android:id="@+id/cancleButton"  
                android:layout_width="80.0dip"  
                android:layout_height="wrap_content"  
                android:layout_marginLeft="30dp"  
                android:gravity="center"  
                android:text="取消"  
                android:textColor="#565656"  
                android:textSize="16.0sp" />  
        </LinearLayout>  
    </LinearLayout>  
</RelativeLayout>  

這樣我們的界面布局就算完成了,下來我們來看一個(gè)Activity甘邀。寫個(gè)了OpenView方法來彈出view

public void OpenView() {  
    LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);  
    popupWindowView = inflater.inflate(R.layout.popupwindow, null);  
    popupWindow = new PopupWindow(popupWindowView,  
            LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, true);  
    // 設(shè)置PopupWindow的彈出和消失效果  
    popupWindow.setAnimationStyle(R.style.popupAnimation);  
    btnsure = (Button) popupWindowView.findViewById(R.id.confirmButton);  
    btnsure.setOnClickListener(new ButtonOnClickListener());  
    cancleButton = (Button) popupWindowView.findViewById(R.id.cancleButton);  
    cancleButton.setOnClickListener(new ButtonOnClickListener());  
    leaveword = (EditText) popupWindowView.findViewById(R.id.leaveword);  
    popupWindow.showAtLocation(btnsure, Gravity.CENTER, 0, 0);  
}  

我們看到彈出來的有點(diǎn)動(dòng)畫效果琅攘,是因?yàn)槲覀冊趶棾鰰r(shí),加上了

popupWindow.setAnimationStyle(R.style.popupAnimation); 

我們需要在在styles.xml下加上popupAnimation

<style name="popupAnimation" parent="android:Animation">  
   <item name="android:windowEnterAnimation">@anim/in</item>  
   <item name="android:windowExitAnimation">@anim/out</item>  
lt;/style>  

在工程res下新建anim文件夾松邪,在anim文件夾先新建兩個(gè)xml文件

in.xml

<?xml version="1.0" encoding="utf-8"?>  
<set xmlns:android="http://schemas.android.com/apk/res/android" >  
    <translate  
        android:duration="1500"  
        android:fromYDelta="5000"  
        android:toYDelta="0" />  
</set>  

out.xml

<?xml version="1.0" encoding="utf-8"?>  
<set xmlns:android="http://schemas.android.com/apk/res/android">  
    <translate  
        android:fromYDelta="0"  
        android:toYDelta="5000"  
        android:duration="1500"  
    />  
</set>  

如果想要關(guān)閉彈出框
調(diào)用popupWindow.dismiss();就好.
完整的Activity源碼

public class MainActivity extends Activity {  
    private View popupWindowView;  
    private PopupWindow popupWindow;  
    private Button btnsure, cancleButton, btnOpen;  
    private EditText leaveword;  
  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
  
        btnOpen = (Button) findViewById(R.id.btnOpen);  
        btnOpen.setOnClickListener(new ButtonOnClickListener());  
    }  
  
    public void OpenView() {  
        LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);  
        popupWindowView = inflater.inflate(R.layout.popupwindow, null);  
        popupWindow = new PopupWindow(popupWindowView,  
                LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, true);  
        // 設(shè)置PopupWindow的彈出和消失效果  
        popupWindow.setAnimationStyle(R.style.popupAnimation);  
        btnsure = (Button) popupWindowView.findViewById(R.id.confirmButton);  
        btnsure.setOnClickListener(new ButtonOnClickListener());  
        cancleButton = (Button) popupWindowView.findViewById(R.id.cancleButton);  
        cancleButton.setOnClickListener(new ButtonOnClickListener());  
        leaveword = (EditText) popupWindowView.findViewById(R.id.leaveword);  
        popupWindow.showAtLocation(btnsure, Gravity.CENTER, 0, 0);  
    }  
  
    private class ButtonOnClickListener implements OnClickListener {  
        @Override  
        public void onClick(View vid) {  
  
            switch (vid.getId()) {  
  
            case R.id.btnOpen:  
                OpenView();  
                break;  
            case R.id.confirmButton:  
  
                Toast.makeText(MainActivity.this, leaveword.getText().toString(), Toast.LENGTH_SHORT)  
                        .show();  
  
                break;  
            case R.id.cancleButton:  
                popupWindow.dismiss();  
                break;  
  
            default:  
                break;  
            }  
        }  
    }  
  
}  

這樣一個(gè)簡單的PopupWindow例子就算完成了.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末坞琴,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子逗抑,更是在濱河造成了極大的恐慌剧辐,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,548評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件邮府,死亡現(xiàn)場離奇詭異荧关,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)挟纱,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,497評論 3 399
  • 文/潘曉璐 我一進(jìn)店門羞酗,熙熙樓的掌柜王于貴愁眉苦臉地迎上來腐宋,“玉大人紊服,你說我怎么就攤上這事⌒鼐海” “怎么了欺嗤?”我有些...
    開封第一講書人閱讀 167,990評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長卫枝。 經(jīng)常有香客問我煎饼,道長,這世上最難降的妖魔是什么校赤? 我笑而不...
    開封第一講書人閱讀 59,618評論 1 296
  • 正文 為了忘掉前任吆玖,我火速辦了婚禮,結(jié)果婚禮上马篮,老公的妹妹穿的比我還像新娘沾乘。我一直安慰自己,他們只是感情好浑测,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,618評論 6 397
  • 文/花漫 我一把揭開白布翅阵。 她就那樣靜靜地躺著歪玲,像睡著了一般。 火紅的嫁衣襯著肌膚如雪掷匠。 梳的紋絲不亂的頭發(fā)上滥崩,一...
    開封第一講書人閱讀 52,246評論 1 308
  • 那天,我揣著相機(jī)與錄音讹语,去河邊找鬼钙皮。 笑死,一個(gè)胖子當(dāng)著我的面吹牛顽决,可吹牛的內(nèi)容都是我干的株灸。 我是一名探鬼主播,決...
    沈念sama閱讀 40,819評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼擎值,長吁一口氣:“原來是場噩夢啊……” “哼慌烧!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起鸠儿,我...
    開封第一講書人閱讀 39,725評論 0 276
  • 序言:老撾萬榮一對情侶失蹤屹蚊,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后进每,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體汹粤,經(jīng)...
    沈念sama閱讀 46,268評論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,356評論 3 340
  • 正文 我和宋清朗相戀三年田晚,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了嘱兼。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,488評論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡贤徒,死狀恐怖芹壕,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情接奈,我是刑警寧澤踢涌,帶...
    沈念sama閱讀 36,181評論 5 350
  • 正文 年R本政府宣布,位于F島的核電站序宦,受9級特大地震影響睁壁,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜互捌,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,862評論 3 333
  • 文/蒙蒙 一潘明、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧秕噪,春花似錦钳降、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,331評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽固阁。三九已至,卻和暖如春城菊,著一層夾襖步出監(jiān)牢的瞬間备燃,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,445評論 1 272
  • 我被黑心中介騙來泰國打工凌唬, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留并齐,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,897評論 3 376
  • 正文 我出身青樓客税,卻偏偏與公主長得像况褪,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子更耻,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,500評論 2 359

推薦閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,280評論 25 707
  • ¥開啟¥ 【iAPP實(shí)現(xiàn)進(jìn)入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開一個(gè)線程测垛,因...
    小菜c閱讀 6,444評論 0 17
  • PopupWindow是Android上自定義彈出窗口,使用起來很方便秧均。 PopupWindow的構(gòu)造函數(shù)為 pu...
    OzanShareing閱讀 776評論 1 2
  • 今天食侮,站在公元2016年6月1日的門檻,遙望三四十年前目胡,我童年的六一锯七,以及記憶中的那些節(jié)日。 關(guān)于六一誉己,記憶最深刻...
    花之季閱讀 190評論 0 0
  • 停在湖心的水巨双,和一張臉 留在眼底的溫柔噪猾,和過去的每一年 秋風(fēng)知意,也喚淺淺 帶來了你炉峰,和往昔的容顏 你是那...
    愛讀書的喵123閱讀 173評論 0 0