Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android PopupWindow 彈出窗口的使用方法

編輯:關於 Android 開發

PopupWindow 是 Android 中常用的彈出式容器,可以在當前介面之上浮現一段自定義內容。本文透過完整範例,示範如何建立 PopupWindow、設定背景與動畫,並處理其中的按鈕點擊事件。

先來看幾張效果示意圖:

MainActivity 程式碼

package com.example.popupwindown; import android.os.Bundle; import android.app.Activity; import android.view.Gravity; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup.LayoutParams; import android.view.WindowManager; import android.widget.Button; import android.widget.PopupWindow; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void showWindow(View v){ View view=getLayoutInflater().inflate(R.layout.pop, null); Button button = (Button) view.findViewById(R.id.operate); final TextView textview=(TextView) view.findViewById(R.id.textView1); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { /*Toast.makeText(MainActivity.this, "button is pressed", Toast.LENGTH_SHORT).show();*/ textview.setText("變了"); } }); PopupWindow pop=new PopupWindow(view, LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT); pop.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.btn_default)); pop.setAnimationStyle(android.R.style.Animation_Translucent); pop.getBackground().setAlpha(100); pop.setTouchable(true);//如果需要按鈕單擊事件,必須有 pop.setOutsideTouchable(true); pop.setFocusable(true); pop.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE); pop.showAtLocation(v,Gravity.CENTER, 0,0); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }

activity_main.xml 布局

主介面只需要一個觸發用的按鈕,透過 android:onClick 綁定 showWindow 方法。

<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="43dp" android:layout_marginTop="23dp" android:text="彈出窗口" android:onClick="showWindow" /> </RelativeLayout>

pop.xml 彈出內容布局

這個布局檔用來定義彈出窗口內實際顯示的內容。

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" /> <Button android:id="@+id/operate" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="操作" /> </LinearLayout>

執行結果如下:

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved