PopUpWindow(提高篇)

PopUpWindow(提高篇)

上一篇只是实现了一个最简单的PopUpWindow显示,本篇介绍显示带列表选择的PopUpWindow和带动画效果的PopUpWindow。

先看效果:
在这里插入图片描述

接下来看代码实现。

MainActivity.java

public class MainActivity extends AppCompatActivity {
    LinearLayout ll_main;
    PopupWindow popupWindow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ll_main = findViewById(R.id.ll_main);
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_open_simple_popupwindow:
                openSimplePopUpWindow();
                break;
            case R.id.btn_open_list_popupwindow:
                openListPopUpWindow();
                break;
            case R.id.btn_open_bottom_animation_list_popupwindow:
                openBottomAnimationListPopUpWindow();
                break;
            case R.id.btn_open_left_animation_list_popupwindow:
                openLeftAnimationListPopUpWindow();
                break;
        }

    }

    private void openSimplePopUpWindow() {
        View view = LayoutInflater.from(this).inflate(R.layout.layout_simple_popupwindow, null);
        popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setFocusable(true);
        popupWindow.showAtLocation(ll_main, Gravity.BOTTOM, 0, 0);
    }

    private void openListPopUpWindow() {
        View view = LayoutInflater.from(this).inflate(R.layout.layout_list_popupwindow, null);
        ListView listView = view.findViewById(R.id.listview);
        final String[] datas = {"清华大学", "北京大学", "上海交通大学", "上海复旦大学", "南京大学", "浙江大学", "东华理工大学"};
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, datas);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(MainActivity.this, datas[position], Toast.LENGTH_SHORT).show();
                dismissPopupWindow();
            }
        });
        popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setFocusable(true);
        popupWindow.showAtLocation(ll_main, Gravity.BOTTOM, 0, 0);
    }

    private void openBottomAnimationListPopUpWindow() {
        View view = LayoutInflater.from(this).inflate(R.layout.layout_list_popupwindow, null);
        ListView listView = view.findViewById(R.id.listview);
        final String[] datas = {"清华大学", "北京大学", "上海交通大学", "上海复旦大学", "南京大学", "浙江大学", "东华理工大学"};
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, datas);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(MainActivity.this, datas[position], Toast.LENGTH_SHORT).show();
                dismissPopupWindow();
            }
        });
        popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setFocusable(true);
        popupWindow.setAnimationStyle(R.style.bottom_animation);
        popupWindow.showAtLocation(ll_main, Gravity.BOTTOM, 0, 0);
    }

    private void openLeftAnimationListPopUpWindow() {
        View view = LayoutInflater.from(this).inflate(R.layout.layout_list_popupwindow, null);
        ListView listView = view.findViewById(R.id.listview);
        final String[] datas = {"清华大学", "北京大学", "上海交通大学", "上海复旦大学", "南京大学", "浙江大学", "东华理工大学"};
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, datas);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(MainActivity.this, datas[position], Toast.LENGTH_SHORT).show();
                dismissPopupWindow();
            }
        });
        popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setFocusable(true);
        popupWindow.setAnimationStyle(R.style.left_animation);
        popupWindow.showAtLocation(ll_main, Gravity.LEFT, 0, 0);
    }

    private void dismissPopupWindow() {
        if (popupWindow != null && popupWindow.isShowing()) {
            popupWindow.dismiss();
        }
    }

}

activity_main.xml

<?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:orientation="vertical"
    android:id="@+id/ll_main"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/btn_open_simple_popupwindow"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="打开一个最简单PopUpWindow!"
        android:onClick="onClick"
        />
    <Button
        android:id="@+id/btn_open_list_popupwindow"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="打开一个选择列表PopUpWindow!"
        android:onClick="onClick"
        />
    <Button
        android:id="@+id/btn_open_bottom_animation_list_popupwindow"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="打开一个带动画效果选择列表PopUpWindow!(底部弹出)"
        android:onClick="onClick"
        />
    <Button
        android:id="@+id/btn_open_left_animation_list_popupwindow"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="打开一个带动画效果选择列表PopUpWindow!(左侧弹出)"
        android:onClick="onClick"
        />
</LinearLayout>

PopUpWindow用的到布局layout_list_popupwindow.xml

<?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="match_parent"
    android:orientation="vertical">
    <View
        android:layout_width="match_parent"
        android:layout_height="5dp"
        android:background="@color/colorAccent"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="选择填报学校"
        android:textColor="@color/colorAccent"
        android:textSize="20sp"/>
    <View
        android:layout_width="match_parent"
        android:layout_height="0.1dp"
        android:background="@color/gray"/>
    <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <View
        android:layout_width="match_parent"
        android:layout_height="0.1dp"
        android:background="@color/gray"/>

</LinearLayout>

打开一个带列表选择的PopUpWindow。

private void openListPopUpWindow() {
        View view = LayoutInflater.from(this).inflate(R.layout.layout_list_popupwindow, null);
        ListView listView = view.findViewById(R.id.listview);
        final String[] datas = {"清华大学", "北京大学", "上海交通大学", "上海复旦大学", "南京大学", "浙江大学", "东华理工大学"};
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, datas);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(MainActivity.this, datas[position], Toast.LENGTH_SHORT).show();
                dismissPopupWindow();
            }
        });
        popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setFocusable(true);
        popupWindow.showAtLocation(ll_main, Gravity.BOTTOM, 0, 0);
    }

打开一个带列表选择的并且带动画效果的PopUpWindow,打开时是从底部慢慢移入,关闭是从底部慢慢移出。


private void openBottomAnimationListPopUpWindow() {
        View view = LayoutInflater.from(this).inflate(R.layout.layout_list_popupwindow, null);
        ListView listView = view.findViewById(R.id.listview);
        final String[] datas = {"清华大学", "北京大学", "上海交通大学", "上海复旦大学", "南京大学", "浙江大学", "东华理工大学"};
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, datas);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(MainActivity.this, datas[position], Toast.LENGTH_SHORT).show();
                dismissPopupWindow();
            }
        });
        popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setFocusable(true);
    	//实现动画效果
        popupWindow.setAnimationStyle(R.style.bottom_animation);
        popupWindow.showAtLocation(ll_main, Gravity.BOTTOM, 0, 0);
    }

打开一个带列表选择的并且带动画效果的PopUpWindow,打开是从左侧慢慢移入,关闭是从左侧慢慢移出。

private void openLeftAnimationListPopUpWindow() {
        View view = LayoutInflater.from(this).inflate(R.layout.layout_list_popupwindow, null);
        ListView listView = view.findViewById(R.id.listview);
        final String[] datas = {"清华大学", "北京大学", "上海交通大学", "上海复旦大学", "南京大学", "浙江大学", "东华理工大学"};
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, datas);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(MainActivity.this, datas[position], Toast.LENGTH_SHORT).show();
                dismissPopupWindow();
            }
        });
        popupWindow = new PopupWindow(view, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        popupWindow.setFocusable(true);
    	//实现动画效果
        popupWindow.setAnimationStyle(R.style.left_animation);
        popupWindow.showAtLocation(ll_main, Gravity.LEFT, 0, 0);
    }

要想显示一个带动画效果的PopUpWindow,只需调用setAnimationStyle,传入一个动画样式资源ID。

本篇用的到的动画效果介绍

底部移入动画

<?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.5"
        android:toAlpha="1.0" />
</set>

底部移出动画


<?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="100%p" />
    <alpha
        android:duration="500"
        android:fromAlpha="1.0"
        android:toAlpha="0.5" />
</set>

左侧移入动画

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="500"
        android:fromXDelta="-100%p"
        android:toYDelta="0" />
    <alpha
        android:duration="500"
        android:fromAlpha="0.5"
        android:toAlpha="1.0" />
</set>

左侧移出动画

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="500"
        android:fromXDelta="0.0"
        android:toXDelta="-100%p" />
    <alpha
        android:duration="500"
        android:fromAlpha="1.0"
        android:toAlpha="0.5" />
</set>

用到的动画样式

<style name="bottom_animation">
        <item name="android:windowEnterAnimation">@anim/enter_bottom</item>
        <item name="android:windowExitAnimation">@anim/exit_bottom</item>
    </style>
    <style name="left_animation">
        <item name="android:windowEnterAnimation">@anim/enter_left</item>
        <item name="android:windowExitAnimation">@anim/exit_left</item>
    </style>

到此PopUpWindow的使用就介绍完了,大家可以任意发挥,实现自己想要的效果。

完整demo

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值