PopupWindow(点击处正下方弹出窗体)

其实最近也是在总结一些技术知识,因为做过去之后老忘记,所以最近打算做一个总结,不能一边做一边忘啊,哈哈哈,其实我的总结也是一边结合自己所做的项目,一边参阅网上大神的文章,主要是做一个笔记,闲话少说,今天给大家介绍PopupWindow下方弹出窗体

要做一个类似于微信的右上角点击出现一个PopupWindow的窗体,话不多说,先直接上图

如上图,点击红色处,弹出黄色框框,黄色框框内的'条件查询','扫一扫'同样点击可以跳转到响应页面.

你要知道的是:PopupWindow是没有默认布局的,所以黄框子里的布局是需要我们自己设计写出来的

 //相对于父控件的位置(例如正中央Gravity.CENTER,下方Gravity.BOTTOM等),可以设置偏移或无偏移  
public void showAtLocation(View parent, int gravity, int x, int y)
 //显示在指定anchor控件的正下方,没有偏移
public void showAsDropDown(View anchor)
 //相对指定的某个控件的位置,有偏移;xoff表示x轴的偏移,正值表示向左,负值表示向右;yoff表示相对y轴的偏移,正值是向下,负值是向上;
public void showAsDropDown(View anchor, int xoff, int yoff)

参见详细解释 点击打开链接

废话不多说了,直接上核心代码了!

黄框子内的布局代码很简单,大家可以自己设计(原谅我的命名很low)

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorBottom"
    android:paddingBottom="2dp">

    <TextView
        android:id="@+id/main_memu_pop_item_highserch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="8dp"
        android:gravity="center"
        android:drawableLeft="@mipmap/highsearch_icon"
        android:text="条件查询"
        android:textColor="#ffffff"/>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#fff"
        />

    <TextView
        android:id="@+id/main_memu_pop_item_sao"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="8dp"
        android:gravity="center"
        android:drawableLeft="@mipmap/qr_icon"
        android:text="扫一扫"
        android:textColor="#ffffff"/>

</LinearLayout>

之后在代码中获取红框控件的id,然后设置点击事件,弹出黄框框中的PopupWindow;

一下是弹出PopupWindow窗体的核心代码

扫描二维码关注公众号,回复: 1878216 查看本文章
//点击展示 条件查询  扫一扫
    private void showPopupWindow(){

        View contentView = LayoutInflater.from(MainActivity.this).inflate(R.layout.main_right_memu_item, null);
        mPopWindow = new PopupWindow(contentView);
        mPopWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        mPopWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        //点击外部取消显示
        mPopWindow.setOutsideTouchable(true);
//        mPopWindow.setBackgroundDrawable(new BitmapDrawable());//括号内过时
        mPopWindow.setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));

        TextView highQueryTv = contentView.findViewById(R.id.main_memu_pop_item_highserch);
        TextView saoTv = contentView.findViewById(R.id.main_memu_pop_item_sao);

        //条件查询的页面
        highQueryTv.setOnClickListener(this);
        //扫一扫的页面
        saoTv.setOnClickListener(this);
        //条件查询的页面
       mPopWindow.showAsDropDown(memu);
}代码中有具体解释,这里就不多说了.

到这里,你就可以简单实现,上图中的PopupWindow窗体下方弹出的效果了;

烦请大家尊重原创者版权,转载请标明出处:http://blog.csdn.net/JOYU_/article/details/79296154

猜你喜欢

转载自blog.csdn.net/joyu_/article/details/79296154