Android popupwindow彈出對話方塊

來源:互聯網
上載者:User

今天項目中要用到對話方塊浮層顯示,PopupWindow可以實現該功能。可以自訂view,通過LayoutInflator方法載入自訂的布局。而且載入和退出時都可以自訂動畫效果。還能指定顯示的位置。如下所示:

MainActivity代碼:

package com.example.popwindow;import android.os.Bundle;import android.app.Activity;import android.view.Menu;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;/** *  * @author WangJintao *  * @date 2013-5-6 */public class MainActivity extends Activity implements OnClickListener {Button button, button1, button2, button3;PopupWindow popupWindow;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);button = (Button) findViewById(R.id.button);button.setOnClickListener(this);}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.button:getPopWin();popupWindow.showAsDropDown(v);break;case R.id.button1:Toast.makeText(MainActivity.this, "button1", Toast.LENGTH_LONG).show();popupWindow.dismiss();break;case R.id.button2:Toast.makeText(MainActivity.this, "button2", Toast.LENGTH_LONG).show();popupWindow.dismiss();break;case R.id.button3:Toast.makeText(MainActivity.this, "button3", Toast.LENGTH_LONG).show();popupWindow.dismiss();break;default:break;}}private void getPopWin() {if (popupWindow != null) {popupWindow.dismiss();} else {initPopWin();}}private void initPopWin() {View popupWindow_view = getLayoutInflater().inflate(R.layout.pop_win,null, false);popupWindow = new PopupWindow(popupWindow_view, 200, 300, true);popupWindow.setAnimationStyle(R.style.AnimationFade);popupWindow_view.setOnTouchListener(new OnTouchListener() {@Overridepublic boolean onTouch(View v, MotionEvent event) {if (popupWindow != null && popupWindow.isShowing()) {popupWindow.dismiss();popupWindow = null;}return false;}});button1 = (Button) popupWindow_view.findViewById(R.id.button1);button2 = (Button) popupWindow_view.findViewById(R.id.button2);button3 = (Button) popupWindow_view.findViewById(R.id.button3);button1.setOnClickListener(this);button2.setOnClickListener(this);button3.setOnClickListener(this);}}

activity_main.xml代碼:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".MainActivity" >    <Button        android:id="@+id/button"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="PopWindow" /></RelativeLayout>

彈出的布局:

pop_win.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:background="@android:color/darker_gray"    android:orientation="vertical" >    <Button        android:id="@+id/button1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="button1" />    <Button        android:id="@+id/button2"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="button2" />    <Button        android:id="@+id/button3"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="button3" /></LinearLayout>

style代碼:

 <!-- Application theme. -->    <style name="AppTheme" parent="AppBaseTheme">        <!-- All customizations that are NOT specific to a particular API-level can go here. -->    </style>    <style name="AnimationFade"> <!-- PopupWindow左右彈出的效果 -->        <item name="android:windowEnterAnimation">@anim/in_lefttoright</item>        <item name="android:windowExitAnimation">@anim/out_righttoleft</item>    </style>

動畫代碼:

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>

聯繫我們

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