1、 popupWindow.xml(即弹出的xml)(即图中的弹出的热门,最新,完结的布局)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ffffff"
>
<TextView
android:id="@+id/txt_fenlei_remn"
android:layout_width="60dp"
android:layout_height="40dp"
android:gravity="center"
android:text="热门"
android:background="#404040"
android:layout_marginTop="1dp"
android:layout_marginBottom="1dp"
android:textColor="#FFFFFF"
android:textSize="20sp"
/>
<TextView
android:id="@+id/txt_fenlei_zuixin"
android:layout_width="60dp"
android:layout_height="40dp"
android:gravity="center"
android:text="最新"
android:background="#404040"
android:layout_marginTop="2dp"
android:layout_marginBottom="1dp"
android:textColor="#FFFFFF"
android:textSize="20sp"
/>
<TextView
android:id="@+id/txt_fenlei_wanjie"
android:layout_width="60dp"
android:layout_height="40dp"
android:gravity="center"
android:text="完结"
android:textColor="#FFFFFF"
android:background="#404040"
android:layout_marginTop="2dp"
android:layout_marginBottom="1dp"
android:textSize="20sp"
/>
</LinearLayout>
2、 在代码中
1、声明he初始换对象
private PopupWindow popupWindow;//声明一个popupwindow对象
View view_pupup;//声明一个view 对象。(即要显示的view)
LayoutInflaterinflater=LayoutInflater.from(this);
view_pupup=inflater.inflate(R.layout.popupwindow_show,null);
2、// 热门点击事件,点击弹出popupwindow
public void LinClick(){
lin_popup.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
if (popupWindow!=null &&popupWindow.isShowing()){
popupWindow.setFocusable(false);
popupWindow.dismiss();//隐藏popupwindow
}else {
InitViewPopupWindow();//初始化popupwindow
}
}
});
}
3、 //初始化popupwindow
public void InitViewPopupWindow(){
//构造函数中设置了要显示的View,宽度 ,高度以及是否能获得焦点。
popupWindow=new PopupWindow(view_pupup,80,80,true);
popupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);//设置宽,高
popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setOutsideTouchable(true);点击popupWindow以外区域,popupWindow自动消失。
popupWindow.setTouchable(true);
popupWindow.setTouchInterceptor(new View.OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEventevent) {
return false;
// 这里如果返回true的话,touch事件将被拦截
// 拦截后 PopupWindow的onTouchEvent不被调用,这样点击外部区域无法dismiss
}
});
popupWindow.setBackgroundDrawable(newBitmapDrawable());//必须设置背景色
popupWindow.showAsDropDown(lin_popup,0,30);
popupWindow.setOnDismissListener(newPopupWindow.OnDismissListener() {
@Override
public void onDismiss(){
//必须有,可以什么都不用写
}
});
}
1、 popupWindow.xml(即弹出的xml)(即图中的弹出的热门,最新,完结的布局)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ffffff"
>
<TextView
android:id="@+id/txt_fenlei_remn"
android:layout_width="60dp"
android:layout_height="40dp"
android:gravity="center"
android:text="热门"
android:background="#404040"
android:layout_marginTop="1dp"
android:layout_marginBottom="1dp"
android:textColor="#FFFFFF"
android:textSize="20sp"
/>
<TextView
android:id="@+id/txt_fenlei_zuixin"
android:layout_width="60dp"
android:layout_height="40dp"
android:gravity="center"
android:text="最新"
android:background="#404040"
android:layout_marginTop="2dp"
android:layout_marginBottom="1dp"
android:textColor="#FFFFFF"
android:textSize="20sp"
/>
<TextView
android:id="@+id/txt_fenlei_wanjie"
android:layout_width="60dp"
android:layout_height="40dp"
android:gravity="center"
android:text="完结"
android:textColor="#FFFFFF"
android:background="#404040"
android:layout_marginTop="2dp"
android:layout_marginBottom="1dp"
android:textSize="20sp"
/>
</LinearLayout>
2、 在代码中
1、声明he初始换对象
private PopupWindow popupWindow;//声明一个popupwindow对象
View view_pupup;//声明一个view 对象。(即要显示的view)
LayoutInflaterinflater=LayoutInflater.from(this);
view_pupup=inflater.inflate(R.layout.popupwindow_show,null);
2、// 热门点击事件,点击弹出popupwindow
public void LinClick(){
lin_popup.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
if (popupWindow!=null &&popupWindow.isShowing()){
popupWindow.setFocusable(false);
popupWindow.dismiss();//隐藏popupwindow
}else {
InitViewPopupWindow();//初始化popupwindow
}
}
});
}
3、 //初始化popupwindow
public void InitViewPopupWindow(){
//构造函数中设置了要显示的View,宽度 ,高度以及是否能获得焦点。
popupWindow=new PopupWindow(view_pupup,80,80,true);
popupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);//设置宽,高
popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setOutsideTouchable(true);点击popupWindow以外区域,popupWindow自动消失。
popupWindow.setTouchable(true);
popupWindow.setTouchInterceptor(new View.OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEventevent) {
return false;
// 这里如果返回true的话,touch事件将被拦截
// 拦截后 PopupWindow的onTouchEvent不被调用,这样点击外部区域无法dismiss
}
});
popupWindow.setBackgroundDrawable(newBitmapDrawable());//必须设置背景色
popupWindow.showAsDropDown(lin_popup,0,30);
popupWindow.setOnDismissListener(newPopupWindow.OnDismissListener() {
@Override
public void onDismiss(){
//必须有,可以什么都不用写
}
});
}