Android 弹框菜单系列之王者荣耀设置PopupWindow

一.效果图

 

二.快速实现:

1.主函数代码:

import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.qd.douyinwu.R;
import com.example.qd.douyinwu.adapter.MyListAdapter;

import java.util.ArrayList;
import java.util.List;

public class PopupWindowWzryActivity extends AppCompatActivity implements View.OnClickListener {
    private TextView startTime, endTime;
    private RelativeLayout rlStartTime, rlEndTime;

    private PopupWindow popupWindow;
    private ListView mlistView; //pop里面列表
    private List<String> popData = new ArrayList<>(); //存放pop列表数据
    private ArrayAdapter<String> popDataAdapter;
    private ImageView ivArrowR,ivArrowL;
    private boolean isShowL = false;
    private boolean isShowR = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_popupwindow_wzry);
        initView();
        initPopData();
    }
    /**
     * pop里面数据
     */
    private void initPopData() {
        popData = new ArrayList<>();
        for (int i = 0; i <=11; i++) {
            popData.add(i+1+"月");
        }
    }

    private void initView() {
        startTime = (TextView) findViewById(R.id.tv_start_time);
        endTime = (TextView) findViewById(R.id.tv_end_time);
        rlStartTime = (RelativeLayout) findViewById(R.id.rl_start_time);
        rlEndTime = (RelativeLayout) findViewById(R.id.rl_end_time);
        ivArrowR = (ImageView) findViewById(R.id.iv_arrow_r);
        ivArrowL = (ImageView) findViewById(R.id.iv_arrow_l);
        rlStartTime.setOnClickListener(this);
        rlEndTime.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_start_time://开始时间
                initPop(startTime);
                if (popupWindow != null && !popupWindow.isShowing()) {
                    popupWindow.showAsDropDown(rlStartTime, 0, 0);
                    isShowL = true;
                    ivArrowL.setImageResource(R.mipmap.arrow_top);
                }else {
                    isShowL = false;
                    ivArrowL.setImageResource(R.mipmap.arrow_down);
                }
                break;
            case R.id.rl_end_time://结束时间
                initPop(endTime);
                if (popupWindow != null && !popupWindow.isShowing()) {
                    popupWindow.showAsDropDown(rlEndTime, 0, 0);
                    isShowR = true;
                    ivArrowR.setImageResource(R.mipmap.arrow_top);
                }else {
                    isShowR = false;
                    ivArrowR.setImageResource(R.mipmap.arrow_down);
                }
                break;


        }
    }

    /**
     *  初始化popupwindow
     */
    private void initPop(final TextView textView) {
        mlistView = new ListView(this);
        popDataAdapter = new ArrayAdapter<>(this, R.layout.popup_text_item, popData);
        mlistView.setAdapter(popDataAdapter);
        mlistView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                textView.setText(popData.get(position));
                //选完之后关闭pop
                popupWindow.dismiss();
                isShowR = false;
                ivArrowR.setImageResource(R.mipmap.arrow_down);
            }
        });
        popupWindow = new PopupWindow(mlistView,rlStartTime.getWidth(), ActionBar.LayoutParams.WRAP_CONTENT, true);
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                popupWindow.dismiss();
                isShowL = false;
                ivArrowL.setImageResource(R.mipmap.arrow_down);
            }
        });
        popupWindow.setBackgroundDrawable(ContextCompat.getDrawable(this, R.drawable.shape_black));
        popupWindow.setAnimationStyle(R.style.popmenu_animation); //动画
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true); //点击pop外消失

    }
}

2.主函数布局:activity_popupwindow_wzry.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="16dp"
    android:paddingRight="16dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_marginTop="20dp"
        android:orientation="horizontal">

        <RelativeLayout
            android:id="@+id/rl_start_time"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="0.5"
            android:background="@drawable/shape_black"
            android:gravity="center_vertical"
            android:paddingLeft="14dp"
            android:paddingRight="10dp">

            <TextView
                android:id="@+id/tv_start_time"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:text="1月"
                android:textColor="#333333"
                android:textSize="14sp" />

            <ImageView
                android:id="@+id/iv_arrow_l"
                android:layout_width="12dp"
                android:layout_height="match_parent"
                android:layout_alignParentRight="true"
                android:src="@mipmap/arrow_down" />

        </RelativeLayout>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:gravity="center"
            android:text="-" />

        <RelativeLayout
            android:id="@+id/rl_end_time"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="0.5"
            android:background="@drawable/shape_black"
            android:gravity="center_vertical"
            android:paddingLeft="14dp"
            android:paddingRight="10dp">

            <TextView
                android:id="@+id/tv_end_time"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:text="6月"
                android:textColor="#333333"
                android:textSize="14sp" />

            <ImageView
                android:id="@+id/iv_arrow_r"
                android:layout_width="12dp"
                android:layout_height="match_parent"
                android:layout_alignParentRight="true"
                android:src="@mipmap/arrow_down" />

        </RelativeLayout>
    </LinearLayout>
</RelativeLayout>

3.popup_text_item.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:textColor="#333333"
    android:textSize="14sp"
    android:paddingLeft="13dp"
    android:gravity="center_vertical"
    android:layout_height="40dp">
</TextView>

4.动画属性:popmenu_animation


    <style name="popmenu_animation" parent="android:Animation">
        <item name="android:windowEnterAnimation">@anim/pop_show_anim</item>
        <item name="android:windowExitAnimation">@anim/pop_hide_anim</item>
    </style>

pop_show_anim.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true"
    android:fillEnabled="true" >

    <scale
        android:duration="200"
        android:fromXScale="1.0"
        android:fromYScale="0.0"
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:pivotX="50%"
        android:pivotY="0%"
        android:toXScale="1.0"
        android:toYScale="1.0" >
    </scale>

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

</set>

pop_hide_anim.xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillEnabled="true"
    android:fillAfter="true">

    <scale
        android:duration="200"
        android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:pivotX="50%"
        android:pivotY="0%"
        android:toXScale="1.0"
        android:toYScale="0.0" >
    </scale>

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

</set>

5.背景边框:shape_black

<shape xmlns:android="http://schemas.android.com/apk/res/android">
<!--无圆角边框-->
<solid android:color="@android:color/white" />
<!--填充的颜色-->
<!--描边-->
<stroke
    android:width="1dp"
    android:color="#ccc" />
</shape>

图标请到阿里巴巴矢量图下载:https://www.iconfont.cn/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值