效果如圖所示,點擊開始按鈕,popWindow從下往上出來,再點擊popWindow外面,popWindow又從上往下消失
可以看出來,上面的popupWindow是半透明的,后面我會細說。
最基本的是activity_main了,很簡單,就只是一個button,這里我就不貼代碼了。
接下來的是,popWindow的界面了
代碼如下: 這里注意我里面的那個注釋
<?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="wrap_content"
android:layout_margin="5dp"
android:orientation="vertical" >
<!-- 這里的linearLayout加android:background=""這個屬性要謹慎,如果加了后,popwindow是不能半透明了的 -->
<Button
android:id="@+id/first"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:background="@android:color/holo_red_light"
android:text="第一個按鈕" />
<Button
android:id="@+id/second"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@android:color/holo_red_light"
android:text="第二個按鈕" />
<Button
android:id="@+id/third"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@android:color/holo_red_light"
android:text="第三個按鈕" />
</LinearLayout>
然后在res/下新建一個文件夾anim,進而anim下新建兩個xml文件,如圖所示:
其中,pophidden_anim的代碼如下
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="1000"
android:fromYDelta="0"
android:toYDelta="50%p" />
<alpha
android:duration="1000"
android:fromAlpha="1.0"
android:toAlpha="0.0" />
</set>
popshow_anim的代碼如下
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="1000"
android:fromYDelta="100%p"
android:toYDelta="0" />
<alpha
android:duration="1000"
android:fromAlpha="0.0"
android:toAlpha="1.0" />
</set>
然后在values/styles.xml加入以下代碼,變成這個樣子,上面的那些是自帶的
<resources>
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</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="mypopwindow_anim_style">
<item name="android:windowEnterAnimation">@anim/popshow_anim</item>
<!-- 指定顯示的動畫xml -->
<item name="android:windowExitAnimation">@anim/pophidden_anim</item>
<!-- 指定消失的動畫xml -->
</style>
</resources>
之后就是Activity里面的代碼了,我里面都寫好了所有的注釋,應該可以看得很清楚
package com.example.popwindow;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button start = (Button) findViewById(R.id.start);
start.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
showPopwindow();
}
});
}
/**
* 顯示popupWindow
*/
private void showPopwindow() {
// 利用layoutInflater獲得View
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.popwindowlayout, null);
// 下面是兩種方法得到寬度和高度 getWindow().getDecorView().getWidth()
PopupWindow window = new PopupWindow(view,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.WRAP_CONTENT);
// 設置popWindow彈出窗體可點擊,這句話必須添加,并且是true
window.setFocusable(true);
// 實例化一個ColorDrawable顏色為半透明
ColorDrawable dw = new ColorDrawable(0xb0000000);
window.setBackgroundDrawable(dw);
// 設置popWindow的顯示和消失動畫
window.setAnimationStyle(R.style.mypopwindow_anim_style);
// 在底部顯示
window.showAtLocation(MainActivity.this.findViewById(R.id.start),
Gravity.BOTTOM, 0, 0);
// 這里檢驗popWindow里的button是否可以點擊
Button first = (Button) view.findViewById(R.id.first);
first.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
System.out.println("第一個按鈕被點擊了");
}
});
//popWindow消失監聽方法
window.setOnDismissListener(new OnDismissListener() {
@Override
public void onDismiss() {
System.out.println("popWindow消失");
}
});
}
}
其中window.setFocusable(true)和window.setBackgroundDrawable()必須填寫,如果是想讓popWindow半透明,就是上面的那個方法,
如果只是單純的調用這個方法就這樣寫window.setBackgroundDrawable(new BitmapDrawable());
關于popupWindow顯示位置的具體方法,你可以看這個博客 popupWindow在指定位置上的顯示