Popwindow

Android开发 专栏收录该内容
94 篇文章 0 订阅

 MainActivity:

package com.example.administrator.popwindow;

import android.app.ActionBar;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private TextView mKuang1,mKuang2,mKuang3;
    private TextView mXuanxiangyi,mXuanxianger,mXuanxiangsan;
    private PopupWindow popupWindow;
    private View popupView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mKuang1=findViewById(R.id.kuang1);
        mKuang2=findViewById(R.id.kuang2);
        mKuang3=findViewById(R.id.kuang3);
        mKuang1.setOnClickListener(this);
        mKuang2.setOnClickListener(this);
        mKuang3.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.kuang1:
                SortPop();
                break;
            case R.id.kuang2:
                SortPop();
                break;
            case R.id.kuang3:
                SortPop();
                break;
            case R.id.xuanxiangyi:
                Toast.makeText(MainActivity.this,"选项一",Toast.LENGTH_SHORT).show();
                break;
            case R.id.xuanxianger:
                Toast.makeText(MainActivity.this,"选项二",Toast.LENGTH_SHORT).show();
                break;
            case R.id.xuanxiangsan:
                Toast.makeText(MainActivity.this,"选项三",Toast.LENGTH_SHORT).show();
                break;
        }
    }

    /**
     * 排序弹出框
     */
    private void SortPop() {
        if (popupWindow == null) {
            popupView = View.inflate(MainActivity.this, R.layout.popwindow, null);
            mXuanxiangyi = popupView.findViewById(R.id.xuanxiangyi);
            mXuanxianger = popupView.findViewById(R.id.xuanxianger);
            mXuanxiangsan = popupView.findViewById(R.id.xuanxiangsan);
            mXuanxiangyi.setOnClickListener(this);
            mXuanxianger.setOnClickListener(this);
            mXuanxiangsan.setOnClickListener(this);
            // 参数2,3:指明popupwindow的宽度和高度
            popupWindow = new PopupWindow(popupView, ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.WRAP_CONTENT, true);
            int screenHeigh = getResources().getDisplayMetrics().heightPixels;
            popupWindow.setHeight(ActionBar.LayoutParams.WRAP_CONTENT);
            popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
                @Override
                public void onDismiss() {
                    lighton();
                }
            });

            popupWindow.setFocusable(true);
            // 设置点击popupwindow外屏幕其它地方消失
            popupWindow.setOutsideTouchable(true);
            // 在点击之后设置popupwindow的销毁
            if (popupWindow.isShowing()) {
                popupWindow.dismiss();
                lighton();
            }

        }
        // 在点击之后设置popupwindow的销毁
        if (popupWindow.isShowing()) {
            popupWindow.dismiss();
            lighton();
        }

        // 设置popupWindow的显示位置,此处是在手机屏幕底部且水平居中的位置
        if (Build.VERSION.SDK_INT != 24) {
            //只有24这个版本有问题,好像是源码的问题
            popupWindow.showAsDropDown(mKuang1);
        } else {
            //7.0 showAsDropDown没卵子用 得这么写
            int[] location = new int[2];
            mKuang1.getLocationOnScreen(location);
            int x = location[0];
            int y = location[1];
            popupWindow.showAtLocation(mKuang1, Gravity.NO_GRAVITY, 0, y + mKuang1.getHeight());
        }
    }

    /**
     * 设置手机屏幕亮度变暗
     */
    private void lightoff() {
        WindowManager.LayoutParams lp = this.getWindow().getAttributes();
        lp.alpha = 0.3f;
        this.getWindow().setAttributes(lp);
    }

    /**
     * 设置手机屏幕亮度显示正常
     */
    private void lighton() {
        WindowManager.LayoutParams lp = this.getWindow().getAttributes();
        lp.alpha = 1f;
        this.getWindow().setAttributes(lp);
    }
}

MianActivity布局:

<?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"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:layout_height="50dp">

        <TextView
            android:id="@+id/kuang1"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:text="弹出框1"
            android:gravity="center"
            android:layout_height="match_parent" />
        <View
            android:layout_width="1dp"
            android:background="#f2f2f2"
            android:layout_height="match_parent"/>
        <TextView
            android:id="@+id/kuang2"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:text="弹出框2"
            android:gravity="center"
            android:layout_height="match_parent" />
        <View
            android:layout_width="1dp"
            android:background="#f2f2f2"
            android:layout_height="match_parent"/>
        <TextView
            android:id="@+id/kuang3"
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:text="弹出框3"
            android:gravity="center"
            android:layout_height="match_parent" />

    </LinearLayout>

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

</LinearLayout>

popwindow布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:background="#fff"
    android:orientation="vertical"
    android:layout_height="wrap_content">

    <TextView
        android:padding="15dp"
        android:id="@+id/xuanxiangyi"
        android:layout_width="match_parent"
        android:text="选项一"
        android:layout_height="wrap_content" />

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

    <TextView
        android:padding="15dp"
        android:id="@+id/xuanxianger"
        android:layout_width="match_parent"
        android:text="选项二"
        android:layout_height="wrap_content" />
    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#f2f2f2"/>
    <TextView
        android:padding="15dp"
        android:id="@+id/xuanxiangsan"
        android:layout_width="match_parent"
        android:text="选项三"
        android:layout_height="wrap_content" />
    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#f2f2f2"/>
</LinearLayout>

下载地址:https://download.csdn.net/download/lanrenxiaowen/10736822

  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 精致技术 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值