用PopupWindow实现弹出菜单(弹出的菜单采用自定义布局)

    

用PopupWindow实现弹出菜单是一个比较好的方式。当然我们还有一个类PopupMenu也能实现弹出菜单,但那个太过于局限了,所以不是很推荐。

这个实例的效果是这样的:点击按钮后,一个菜单从屏幕的右边滑入到屏幕中,点击按钮/空白处后菜单消失。

布局文件时一个按钮,我就不贴出代码了。下面是菜单的布局:

复制代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="1" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="2" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="3" />

    <Button
        android:id="@+id/closet_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="关闭" />

</LinearLayout>
复制代码

 

MainActivity.java

复制代码
package com.kale.popup;


import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener{
    LayoutInflater inflater = null;
    private PopupWindow popupWindow;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        initPopWindow();
    }
    
    /** 
     * 初始化popWindow
     * */
    private void initPopWindow() {
        View popView = inflater.inflate(R.layout.menu, null);
        popupWindow = new PopupWindow(popView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        popupWindow.setBackgroundDrawable(new ColorDrawable(0));
        //设置popwindow出现和消失动画
        popupWindow.setAnimationStyle(R.style.PopMenuAnimation);
        Button btn01 = (Button)popView.findViewById(R.id.button1);
        btn01.setOnClickListener(this);
        Button btn02 = (Button)popView.findViewById(R.id.button2);
        btn02.setOnClickListener(this);
        Button btn03 = (Button)popView.findViewById(R.id.button3);
        btn03.setOnClickListener(this);
        Button closetBtn = (Button)popView.findViewById(R.id.closet_btn);
        closetBtn.setOnClickListener(this);

    }
    
    public void buttonListener(View v) {
        showPop(v, 0, 0, 0);
    }
    
    

    /** 
     * 显示popWindow
     * */
    public void showPop(View parent, int x, int y,int postion) {
        //设置popwindow显示位置
        popupWindow.showAsDropDown(parent);
        //获取popwindow焦点
        popupWindow.setFocusable(true);
        //设置popwindow如果点击外面区域,便关闭。
        popupWindow.setOutsideTouchable(true);
        popupWindow.update();

    }

    @Override
    public void onClick(View v) {
        Button btn = (Button) v;
        Toast.makeText(MainActivity.this, btn.getText(), 0).show();
        popupWindow.dismiss();
    }
    
}
复制代码

 

菜单的动画

style.xml

    <style name="PopMenuAnimation" parent="@android:style/Animation">
        <item name="android:windowEnterAnimation">@anim/slide_left_in</item>
        <item name="android:windowExitAnimation">@anim/slide_right_out</item>
    </style>

slide_left_in.xml

复制代码
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <translate
        android:duration="200"
        android:fromXDelta="100.0%p"
        android:toXDelta="0.0" />

</set>
复制代码

slide_right_out.xml

复制代码
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <translate
        android:duration="100"
        android:fromXDelta="0.0"
        android:toXDelta="100.0%p" />

</set>
复制代码
posted @   developer_Kale  阅读(2075)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
网站流量统计工具
点击右上角即可分享
微信分享提示