实现popWindow一般分为三个步骤:
1.创建popWindow对象
2.设置点击事件监听
3.显示popwindow
第一步:创建popwindow布局文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/pop_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignParentBottom="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="10dp">
<TextView
android:id="@+id/text_hot_male"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@drawable/pop_hottext_male"
android:text="热门男神"
android:textSize="16dp"
android:gravity="center"
/>
<TextView
android:id="@+id/text_hot_female"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@drawable/pop_hottext_male"
android:text="热门女神"
android:textSize="16dp"
android:gravity="center"
android:layout_marginTop="6dp"
/>
<TextView
android:id="@+id/text_confim"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@drawable/bg_pop_confim"
android:layout_marginTop="6dp"
android:text="取消"
android:textSize="18dp"
android:gravity="center"
android:textColor="@color/white"
android:textStyle="bold"
/>
</LinearLayout>
</RelativeLayout>
建立名为pop_mainselect.xml布局文件,添加三个TextView.
第二步,建立java文件,创建popWindow对象
public class HotMainPop extends PopupWindow {
private Context mContext;
private View view;
private TextView text_mainmale;
private TextView text_mainfemal;
private TextView cancel;
public HotMainPop(Context mContext,View.OnClickListener itemsOnclick){
this.view = LayoutInflater.from(mContext).inflate(R.layout.pop_mainselect,null);
text_mainmale = (TextView) view.findViewById(R.id.text_hot_male);
text_mainfemal = (TextView) view.findViewById(R.id.text_hot_female);
cancel = (TextView) view.findViewById(R.id.text_confim);
cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dismiss();
}
});
//设置按钮监听
text_mainmale.setOnClickListener(itemsOnclick);
text_mainfemal.setOnClickListener(itemsOnclick);
//设置外部可点击
this.setOutsideTouchable(true);
// mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
this.view.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
int height = view.findViewById(R.id.pop_layout).getTop();
int y = (int) event.getY();
if (event.getAction() == MotionEvent.ACTION_UP) {
if (y < height) {
dismiss();
}
}
return true;
}
});
/* 设置弹出窗口特征 */
// 设置视图
this.setContentView(this.view);
// 设置弹出窗体的宽和高
this.setHeight(RelativeLayout.LayoutParams.MATCH_PARENT);
this.setWidth(RelativeLayout.LayoutParams.MATCH_PARENT);
// 设置弹出窗体可点击
this.setFocusable(true);
// 实例化一个ColorDrawable颜色为半透明
// ColorDrawable dw = new ColorDrawable(0xb0000000);
// 设置弹出窗体的背景
// this.setBackgroundDrawable(dw);
// 设置弹出窗体显示时的动画,从底部向上弹出
this.setAnimationStyle(R.style.select_hot_anim);
}
}
这里将点击事件回调给所调用popwindow的界面,也就是,popwindow的按钮点击事件不再内部实现,而是抛给调用者去实现。另外还有读者发现这里设置了popwindow出现和退出的动画,下面说明一下:
首先在res文件下建立anim文件:在里面继续先创建弹出动画文件,名为:pop_enter_anim,具体如下:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="200"
android:fromYDelta="100%p"
android:toYDelta="0"/>
<!--<alpha-->
<!--android:duration="200"-->
<!--android:fromAlpha="0.0"-->
<!--android:toAlpha="1.0"/>-->
</set>
继续创建退出动画,名为:pop_exit_anim,具体如下:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="200"
android:fromYDelta="0"
android:toYDelta="50%p"/>
<!--<alpha-->
<!--android:duration="200"-->
<!--android:fromAlpha="1.0"-->
<!--android:toAlpha="0.0"/>-->
</set>
然后再styles.xml文件建立动画style,如下:
<!--主页面popwindow-->
<style name="select_hot_anim" parent="android:Animation">
<item name="android:windowEnterAnimation">@anim/pop_enter_anim</item>
<item name="android:windowExitAnimation">@anim/pop_exit_anim</item>
</style>
this.setAnimationStyle(R.style.select_hot_anim);这句话就是给popwindow添加出现和消息动画
下面就可以调用了:
public void showPopFormBottom() {
hotMainPop = new HotMainPop(this, onClickListener);
//需要展示在哪个位置 main_view是主界面布局跟布局id
hotMainPop.showAtLocation(findViewById(R.id.main_view), Gravity.BOTTOM, 0, 0);
}
private View.OnClickListener onClickListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.text_hot_male:
break;
case R.id.text_hot_female:
break;
}
}
};
效果如下:
这样就最简单实现了