<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="@color/colorAccent"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tv_popup_text"
android:layout_width="wrap_content"
android:layout_height="80dp"
android:text="我就是弹窗"
android:textSize="25sp"
android:textColor="#ffffffff"
android:layout_centerInParent="true"
android:gravity="center"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="250"
android:fromYDelta="100.0%"
android:toYDelta="0.0" />
</set>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="250"
android:fromYDelta="0.0"
android:toYDelta="100%" />
</set>
<style name="anim_popup_window">
<item name="android:windowEnterAnimation">@anim/popupwindow_in</item>
<item name="android:windowExitAnimation">@anim/popupwindow_out</item>
</style>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:id="@+id/layout_home"
android:background="#FFB5C5"
tools:context="com.lotus.popupwindowdemo.HomeActivity">
<TextView
android:id="@+id/tv_show_popup_window"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="点击显示PopupWindow" />
</LinearLayout>
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;
public class HomeActivity extends AppCompatActivity implements View.OnClickListener {
private LinearLayout layout_home;
private TextView tv_show_popup_window;
private PopupWindow mPopupWindow;
private TextView tv_popup_text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
// 引入窗口配置文件:即弹窗的界面
View popupView = getLayoutInflater().inflate( R.layout.layout_popupwindow, null);
popupView.setOnClickListener( this);
tv_popup_text = (TextView) popupView.findViewById(R.id.tv_popup_text);
tv_popup_text.setOnClickListener(this);
// PopupWindow实例化
mPopupWindow = new PopupWindow( popupView, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, true);
// 设置PopupWindow是否可触摸(设置为不可触摸,那弹出框内的任何控件都不能进行任何点击等等类似操作)
mPopupWindow.setTouchable( true);
// 设置非PopupWindow区域是否可触摸
// 1.若设置PopupWindow获得焦点和非PopupWindow区域可触摸,但实际上非PopupWindow区域的控件并不能响应点击事件等等
// 2.若设置PopupWindow不可获得焦点,则不管非PopupWindow区域被设置能否触摸,实际上非PopupWindow区域的控件都能响应点击事件等等
// 3.若设置PopupWindow不可获得焦点,非PopupWindow区域被设置能触摸,当点击非PopupWindow区域时能隐藏PopupWindow,而点击返回键并不能隐藏窗口,
// 此时通过按钮只能控制窗口的弹出,并不能控制消失,消失只能通过点击其他非PopupWindow区域
mPopupWindow.setOutsideTouchable( false);
// 如果不设置PopupWindow的背景,无论是点击外部区域还是Back键都无法dismiss弹框(但目前并没有发现此问题)
// mPopupWindow.setBackgroundDrawable( new BitmapDrawable( getResources(), (Bitmap) null));
// 设置PopupWindow显示和隐藏时的动画
mPopupWindow.setAnimationStyle(R.style.anim_popup_window);
// 设置PopupWindow是否可获得焦点
// 1.如果设置为可获得焦点,不管非PopupWindow区域被设置能否触摸,也会在点击屏幕非PopupWindow区域和点击返回键时,使PopupWindow隐藏
// 2.相反,如果设置为不可获得焦点,在点击屏幕非PopupWindow区域或点击返回键时,都不能使PopupWindow隐藏
mPopupWindow.setFocusable(false);
layout_home = (LinearLayout) this.findViewById(R.id.layout_home);
tv_show_popup_window = (TextView) this.findViewById( R.id.tv_show_popup_window);
tv_show_popup_window.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if ( mPopupWindow.isShowing()) {
// 隐藏窗口,如果设置了点击窗口外消失,则不需要此方式隐藏
mPopupWindow.dismiss();
tv_show_popup_window.setText("点击显示PopupWindow");
} else {
// 弹出窗口显示内容视图,默认以锚定视图的左下角为起点,这里为点击按钮
// mPopupWindow.showAsDropDown( view);//默认在view(tv_show_popup_window)的下方出现
mPopupWindow.showAtLocation( layout_home, Gravity.BOTTOM, 0, 0);
tv_show_popup_window.setText("点击使PopupWindow消失");
}
}
});
}
@Override
public void onClick(View view) {
switch ( view.getId()){
case R.id.tv_popup_text:
Toast.makeText( getApplicationContext(),"我是PopupWindow内的一个控件",Toast.LENGTH_SHORT).show();
break;
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有