Android:popupwindow实现底部弹窗实例

       Android:popupwindow实现底部弹窗源码实例

 

 

一、PopupWindow这个类用来实现一个弹出框,可以使用任意布局的View作为其内容,这个弹出框是悬浮在当前activity之上的。

二、实例测试。

       1、在res目录下新建anim目录,在anim目录下建两个动画效果文件popshow_anim.xml和pophidden_anim.xml,用来控制菜单的弹出和隐藏。里面的是配置菜单弹出时的效果, translate 位置转移动画效果 duration 属性为动画持续时间 ,fromXDelta 属性为动画起始时 X坐标上的位置 toYDelta 属性为动画结束时 Y坐标上的位置 ,属性里面还可以加上%和p,例如:
            android:toXDelta=”100%”,表示自身的100%,也就是从View自己的位置开始。
            android:toXDelta=”80%p”,表示父层View的80%,是以它父层View为参照的。

popshow_anim.xml:

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

    <alpha
        android:duration="500"
        android:fromAlpha="0.0"
        android:toAlpha="1.0" />
</set>

  pophidden_anim.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:duration="800"
        android:fromYDelta="0"
        android:toYDelta="50%p" />
    <alpha
        android:duration="1000"
        android:fromAlpha="1.0"
        android:toAlpha="0.0" />
</set>

       2、style.xml里添加ipopwindow_anim_style

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
    <style name="ipopwindow_anim_style">
        <item name="android:windowEnterAnimation">@anim/popshow_anim</item>
        <!-- 指定显示的动画xml -->
        <item name="android:windowExitAnimation">@anim/pophidden_anim</item>
        <!-- 指定消失的动画xml -->
    </style>
</resources>

       3、layout目录下新建弹出框的布局文件popupwindow.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#2196F3"
    android:gravity="center_horizontal"
    >
 <LinearLayout
        android:layout_marginTop="30dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:id="@+id/tv_pop_name"
            android:textSize="16sp"
            android:textColor="#181717"
            android:layout_width="wrap_content"
            android:text="linux"
            android:layout_height="wrap_content"/>
        <TextView
            android:id="@+id/tv_pop_title"
            android:layout_marginLeft="20dp"
            android:textSize="16sp"
            android:textColor="#FF5722"
            android:layout_width="wrap_content"
            android:text="android"
            android:layout_height="wrap_content"/>
 <TextView
            android:id="@+id/tv_pop_share"
            android:gravity="right"
            android:layout_marginLeft="20dp"
            android:textSize="16sp"
            android:textColor="#E91E63"
            android:layout_width="match_parent"
            android:text="share"
            android:layout_height="wrap_content"/>

    </LinearLayout>

           <TextView
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:layout_margin="6dp"/>
    <TextView
        android:id="@+id/tv_pop_content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="President of the United States Donald John Trump is the 45th and current president of the United States. "/>
    <TextView
        android:layout_margin="6dp"
        android:layout_width="match_parent"
        android:layout_height="1dp" />
    <Button
        android:id="@+id/tv_pop_cancel"
        android:textSize="20sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="cancel"/>

</LinearLayout>

       4、layout目录下新建布局文件activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/btn_pop"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="50dp"
        android:onClick="openPopWindow"
        android:text="Click to open Pop Window" />

</LinearLayout>

 

       5、MainActivity.java,源码里面有相应注释。

package com.example.popupwindow_demo;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private PopupWindow popupWindow;
    private View contentView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        showPopwindow();
    }
    /**
     * 显示popupWindow
     */
    private void showPopwindow() {
        //加载弹出框的布局
        contentView = LayoutInflater.from(MainActivity.this).inflate(
                R.layout.popupwindow, null);
        // 设置按钮的点击事件
        Button button = (Button) contentView.findViewById(R.id.tv_pop_cancel);
        button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "button is pressed",
                        Toast.LENGTH_SHORT).show();
            }
        });
        popupWindow = new PopupWindow(contentView,
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setFocusable(true);// 取得焦点
        //注意  要是点击外部空白处弹框消息  那么必须给弹框设置一个背景色  不然是不起作用的
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        //点击外部消失
        popupWindow.setOutsideTouchable(true);
        //设置可以点击
        popupWindow.setTouchable(true);
        //进入退出的动画,指定刚才定义的style
        popupWindow.setAnimationStyle(R.style.ipopwindow_anim_style);



    }    // 按下android回退物理键 PopipWindow消失解决
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(event.getKeyCode()==KeyEvent.KEYCODE_BACK){
            if(popupWindow!=null&&popupWindow.isShowing()){
                popupWindow.dismiss();
                return true;
            }
        }
        return false;
    }
   /**
     * 按钮的监听
     * @param v
     */
    public void openPopWindow(View v) {
        //从底部显示
            popupWindow.showAtLocation(contentView, Gravity.BOTTOM, 0, 0);

    }
}

       6、源码的结构图 

       7、执行效果

 

三、本demo源码下载地址:

https://download.csdn.net/download/qq_37858386/12465111

 

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值