popupwindow仿侧滑菜单底部弹出

本文介绍如何使用PopupWindow实现一个从底部弹出并带有动画效果的侧滑菜单。作者首先尝试使用DrawerLayout和slidingMenu,但未成功,于是选择自定义PopupWindow并添加进入和退出动画。详细步骤包括创建动画XML文件、定义PopupWindow及其布局,并在Java逻辑代码中监听事件以弹出和隐藏菜单。
摘要由CSDN通过智能技术生成

poupupWindow扩展

今天做一个从底部弹出的侧滑菜单栏,但是用了谷歌的DrawerLayout和第三方的slidingMenu居然都不可以,想了一下,既然这样,那么我们就只有用popupwindow了,但是poupupwindow又没有动画效果,我们也就只有自定义动画效果了。

先看动画的xml文件:

这里写图片描述

上面的是弹出poupuwindow的动画:popupwindow_enter_anim,文件放置在res文件下的anim文件中,如果没有该文件价,那就新建一个。

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
    android:fromYDelta="0"
    android:toYDelta="100%p"
    android:duration="2000"/>

<alpha
    android:fromAlpha="1.0"
    android:toAlpha="0"
    android:duration="2000"/>
</set>

上面的这个是退出poupupwindow时的动画:popupwindow_exit_anim.

将这两个动画文件放入res目录下的style文件中:

    <!-- popupwindow 动画 -->
<style name="Animation">
    <item name="android:windowEnterAnimation">@anim/popupwindow_enter_anim</item>
    <item name="android:windowExitAnimation">@anim/popupwindow_exit_anim</item>
</style>

接下来就是如何去定义popupwindow:

由于我的poupupwindow是在listView中的条目当中的控件监听弹出的,所以我就不贴出完整代码了,贴出相关代码:

下面时popupwindow的布局文件:

图片长这样:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_margin="30dp"
android:layout_height="match_parent">
<LinearLayout
    android:layout_width="match_parent"
    android:gravity="center_vertical"
    android:layout_marginTop="10dp"
    android:layout_marginLeft="10dp"
    android:layout_height="wrap_content">
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:src="@mipmap/singer"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="歌手:"
        android:textSize="18sp"
        android:textColor="#000"
        android:padding="8dp"
        />
    <TextView
        android:id="@+id/tv_songs_details_ratist"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:textColor="#000"
        android:padding="8dp"
        />
</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:gravity="center_vertical"
    android:layout_marginTop="10dp"
    android:layout_marginLeft="10dp"
    android:layout_height="wrap_content">
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:src="@mipmap/album"
        android:paddingLeft="6dp"
        android:paddingRight="7dp"
        android:layout_marginTop="2dp"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="专辑:"
        android:textSize="18sp"
        android:textColor="#000"
        android:padding="8dp"
        />
    <TextView
        android:id="@+id/tv_songs_details_album"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:textColor="#000"
        android:padding="8dp"
        />
</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:gravity="center_vertical"
    android:layout_marginLeft="10dp"
    android:layout_marginTop="10dp"
    android:layout_height="wrap_content">
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:src="@mipmap/timing"
        android:paddingLeft="10dp"
        android:paddingRight="14dp"
        android:layout_marginTop="2dp"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="时长:"
        android:textSize="18sp"
        android:textColor="#000"
        android:padding="8dp"
        />
    <TextView
        android:id="@+id/tv_songs_details_time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:textColor="#000"
        android:padding="8dp"
        />
</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:gravity="center_vertical"
    android:layout_marginTop="10dp"
    android:layout_marginLeft="10dp"
    android:layout_height="wrap_content">
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:src="@mipmap/love"
        android:paddingLeft="10dp"
        android:paddingRight="9dp"
        android:layout_marginTop="2dp"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="大小:"
        android:textSize="18sp"
        android:textColor="#000"
        android:padding="8dp"
        />
    <TextView
        android:id="@+id/tv_songs_details_size"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:textColor="#000"
        android:padding="8dp"
        />
</LinearLayout>

</LinearLayout>

下面就是java逻辑代码:

监听事件点击之后弹出popupwindow:

iv_detals_menu.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ((TextView)popupwindow.findViewById(R.id.tv_songs_details_ratist)).setText(song.getArtist());
            ((TextView)popupwindow.findViewById(R.id.tv_songs_details_album)).setText(song.getAlbum());
            ((TextView)popupwindow.findViewById(R.id.tv_songs_details_time)).setText(song.getDuration());
            ((TextView)popupwindow.findViewById(R.id.tv_songs_details_size)).setText(song.getSize());
            showPopupWindow();//显示popupwindow
            notifyDataSetChanged();
        }
    });
popupwindow = inflater.inflate(R.layout.slidingmenu_songs_details,null);//这是上面的View

这是显示poupuwindow的代码:

   private void showPopupWindow() {
    WindowManager wm1 = activity.getWindowManager();
    int width = wm1.getDefaultDisplay().getWidth();//屏幕宽度
    PopupWindow popupWindow = new PopupWindow(popupwindow, width, LayoutParams.WRAP_CONTENT);
    popupWindow.setAnimationStyle(R.style.Animation);
    // 设置点击外部区域, 自动隐藏
    popupWindow.setOutsideTouchable(true); // 外部可触摸
    popupWindow.setBackgroundDrawable(inflate.getResources().getDrawable(R.drawable.bg_popupwindow_details)); // 设置空的背景, 响应点击事件
    popupWindow.setFocusable(true); //设置可获取焦点
    // 显示在指定控件下
    popupWindow.showAtLocation(iv_detals_menu,Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0);
}

OK,上面就是如何利用popupwindow仿侧滑菜单,但是侧滑菜单不能从底部上划,而我们的可以哦。

你们的点赞,就是我进步的动力哦。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值