2017年8月26日,周结(二十三),PopupWindow的简单使用

来源:互联网 发布:90后听的经典网络歌曲 编辑:程序博客网 时间:2024/05/02 01:22

这周任务之中大部分还是调整或者是修改UI。

项目中很多东西还是不懂,所以功能方面我没有参与太多开发。

最大的收获就是学会了使用 Popupwindow

项目中需要实现一个从下往上弹出的提示框,所以先写了两个动画文件。

一个用来 show :

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android">    <translate        android:duration="500"        android:fromYDelta="100%p"        android:toYDelta="0" />    <alpha        android:duration="500"        android:fromAlpha="0.0"        android:toAlpha="1.0" /></set>
一个用来 hide :

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android">    <translate        android:duration="500"        android:fromYDelta="0"        android:toYDelta="50%p" />    <alpha        android:duration="500"        android:fromAlpha="1.0"        android:toAlpha="0.0" /></set>
最后在 styles 文件中添加这样一个 Item

<style name="mypopwindow_anim_style">    <item name="android:windowEnterAnimation">@anim/popshow_anim</item>    <!-- 指定显示的动画xml -->    <item name="android:windowExitAnimation">@anim/pophidden_anim</item>    <!-- 指定消失的动画xml --></style>
接着写一下需要显示的弹窗的布局文件

<?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:background="@drawable/pop_lyt__bg"    android:orientation="vertical">    <TextView        android:textSize="15sp"        android:textStyle="bold"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_marginTop="25dp"        android:gravity="center"        android:text="No Location"        android:textColor="@color/black" />    <TextView        android:textColor="@color/black_alp50"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_marginTop="15dp"        android:gravity="center"        android:text="In order to tell you how air quality is in your area, you need to grant AirPOP access to your phone's location in 'Settings'>'Privacy'>'Location Services'." />    <LinearLayout        android:layout_marginTop="25dp"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_marginLeft="60dp"        android:layout_marginRight="60dp"        android:background="@drawable/btn_open_settting"        android:gravity="center">        <TextView            android:layout_marginTop="10dp"            android:layout_marginBottom="10dp"            android:id="@+id/pop_btn"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_gravity="center"            android:text="Open Settings"            android:textAllCaps="false"            android:textColor="@color/white_alp70"            android:textSize="20sp" />    </LinearLayout></LinearLayout>

效果如下


接着在 Java 代码中添加如下代码:

private PopupWindow popupWindow;
View popwin = View.inflate(getContext(), R.layout.location_error_popwindow, null);        int width = (int) ((getResources().getDisplayMetrics().widthPixels) * 0.9);        int height = (int) ((getResources().getDisplayMetrics().heightPixels) * 0.35);        popupWindow = new PopupWindow(popwin, width, height);        TextView setting = (TextView) popwin.findViewById(R.id.pop_btn);        setting.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);                startActivityForResult(intent, 0);            }        });        popupWindow.setFocusable(true);// 取得焦点        //注意  要是点击外部空白处弹框消息  那么必须给弹框设置一个背景色  不然是不起作用的//        popupWindow.setBackgroundDrawable(getResources().getDrawable(R.color.black));        //点击外部消失        popupWindow.setOutsideTouchable(true);        //设置可以点击        popupWindow.setTouchable(true);        //进入退出的动画        popupWindow.setAnimationStyle(R.style.mypopwindow_anim_style);        popupWindow.showAtLocation(turboModelBuilding.lytLocationError, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0);
最终手机上的显示效果如下:


原创粉丝点击