首先,要為popupWindow 寫一個xml配置文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/margin_max"
android:layout_marginRight="@dimen/margin_max"
android:background="@color/white"
android:orientation="vertical" >
<!-- main content -->
<TextView
android:id="@+id/diet_pop_tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/margin_max"
android:text="@string/activity_aerobic"
android:textColor="@color/string_bgwhite_main"
android:textSize="@dimen/text_15" />
<!-- main content sub -->
<TextView
android:id="@+id/diet_pop_tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/recipe_choose_exam"
android:layout_marginLeft="@dimen/margin_max"
android:textColor="@color/string_bgwhite_sub"
android:textSize="@dimen/text_15" />
<!-- second title -->
<TextView
android:id="@+id/diet_pop_tv3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/diet_pop_title"
android:paddingLeft="@dimen/margin_max"
android:layout_marginLeft="@dimen/margin_max"
android:text="@string/activity_aerobic_content"
android:textColor="@color/white"
android:textSize="@dimen/text_15"
/>
<!-- second content -->
<TextView
android:id="@+id/diet_pop_tv4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/margin_max"
android:text="@string/activity_aerobic_suggest"
android:textColor="@color/string_bgwhite_main"
android:textSize="@dimen/text_15" />
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scaleType="fitStart"
android:background="@drawable/popupshadow"
/>
</LinearLayout>
</LinearLayout>
---------然後---再代碼中處理popupWindow,
// new popupwindow
View inflateView = getLayoutInflater().inflate(
R.layout.recipe_popwindow, null);//上面的xml文件,作為popupWindow的視圖
mPopupWindow = new PopupWindow(inflateView, LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT);
mPopupWindow.setFocusable(false);
mPopupWindow.setOutsideTouchable(true);
mPopupWindow.setAnimationStyle(R.style.AnimationPreview);//為popupWindow設置進入,退出的動畫效果
------下面配置進入退出的動畫:
anim文件夾下,進入動畫(由右下角進入,由小變大):
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/decelerate_interpolator">
<scale android:fromXScale=".1" android:toXScale="1.0"
android:fromYScale=".1" android:toYScale="1.0"
android:pivotX="100%p" android:pivotY="100%p"
android:duration="500"/>
<alpha android:fromAlpha="0" android:toAlpha="1.0"
android:duration="@android:integer/config_mediumAnimTime"/>
</set>
退出動畫(向右下角退出,由大變小,變透明):
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/decelerate_interpolator"
android:zAdjustment="top">
<scale android:fromXScale="1.0" android:toXScale=".5"
android:fromYScale="1.0" android:toYScale=".5"
android:pivotX="100%p" android:pivotY="100%p"
android:duration="@android:integer/config_mediumAnimTime" />
<alpha android:fromAlpha="1.0" android:toAlpha="0"
android:duration="@android:integer/config_mediumAnimTime"/>
</set>
--------在styles文件夾下配置style:
<style name="AnimationPreview">
<item name="android:windowEnterAnimation">@anim/zoomin</item>
<item name="android:windowExitAnimation">@anim/zoomout</item>
</style>
-------最後在代碼中設置popupWindow進入退出的判定:
public void openMenu(View v) {
// System.out.println(view.getId() + "-----id--" + view.getBottom()
// + "bottom-----view" + view.getTop());
if (!flag) {//flag初始化為false;
// set position
// mPopupWindow.showAtLocation(view, Gravity.LEFT, 0, 0);
mPopupWindow.showAsDropDown(v, 0, 0);
// view.findViewById(R.id.diet_sugget_ka).setVisibility(View.INVISIBLE);
flag = true;
} else {
mPopupWindow.dismiss();
flag = false;
}
}