使用PopupWindow实现功能,及步骤

PopupWindow详细介绍:

PopupWindow是一个以弹窗方式呈现的控件,可以用来显示任意视图(View),而且会浮动在当前活动(activity)的顶部”。因此我们可以通过PopupWindow实现各种各样的弹窗效果,PopupWindow自定义布局比较方便,而且在显示位置比较自由不受限制,因此受到众多开发者的青睐。

实现效果图:

 


关于实现这个功能准备以下几点:

1:在build.gradle中加入:compile 'com.jakewharton:butterknife:7.0.1'或者导入butterknife的jar包

   在最早我们通常用findviewbyid来进行绑定控件,这里我用Butterkinfe注解的方式来绑定空件,AndroidStudio有一个(Zelezny)这个插件,可以使大家在日常开发中更有效的缩短时间,下面教大家如何安装Zelezny这个插件:

(1):打开Settings选择Plugins,点击Browse Repositories
 



(2):在搜索框中输入Zelezny,选择Android ButterKnife Zelezny,点击lnstall




(3):最后点击Restart Android Studio重新启动即可



2:安装完插件以后教大家如何使用,首先我们将布局展示给大家:

  

<?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">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:background="@color/app_yellow">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="周边"
            android:textColor="@color/whiter"
            android:textSize="20dp" />
    </android.support.v7.widget.Toolbar>

    <!--从这里开始,即iushi自定义多条件-->
    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#E2E2E2"></View>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:background="@color/whiter"
        android:orientation="horizontal">

        <LinearLayout
            android:id="@+id/supplier_list_product"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/supplier_list_product_tv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="全部"
                android:textSize="14dp" />

            <ImageView
                android:layout_width="25dp"
                android:layout_height="25dp"
                android:src="@mipmap/icon_arrow_down" />
        </LinearLayout&
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
PopupWindow 是 Android 系统中的一个弹出窗口,可以显示在当前界面之上。使用 PopupWindow 可以实现下拉菜单、提示框、对话框等功能使用 PopupWindow步骤如下: 1.创建 PopupWindow 对象 ```java PopupWindow popupWindow = new PopupWindow(context); ``` 2.设置 PopupWindow 的布局和宽高 ```java View contentView = LayoutInflater.from(context).inflate(R.layout.popup_layout, null); popupWindow.setContentView(contentView); popupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT); popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT); ``` 3.设置 PopupWindow 的背景和动画效果 ```java popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); popupWindow.setAnimationStyle(R.style.PopupAnimation); ``` 4.设置 PopupWindow 的位置和显示方式 ```java popupWindow.showAsDropDown(anchorView, xoff, yoff, gravity); ``` 其中,`anchorView` 是弹出窗口的锚点 View,`xoff` 和 `yoff` 是弹出窗口相对于锚点 View 的偏移量,`gravity` 是弹出窗口的位置,可以是 `Gravity.LEFT`、`Gravity.RIGHT`、`Gravity.TOP` 或 `Gravity.BOTTOM`。 5.处理 PopupWindow 的事件 ```java popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() { @Override public void onDismiss() { // 处理弹出窗口消失事件 } }); ``` 以上就是 PopupWindow 的基本使用方法。需要注意的是,PopupWindow 的宽高应该根据实际需要设置,以避免出现显示不全或者过大的情况。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值