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

摘要:
PopupWindow是实现弹出菜单的好方法。当然,我们也有一个PopupMenu类,它也可以实现弹出菜单,但这太有限了,所以不建议使用。此示例的效果如下:单击按钮后,菜单从屏幕右侧滑动到屏幕,单击按钮/空白后,菜单消失。布局文件是一个按钮,我不会发布代码。以下是菜单布局:˂?

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

用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>

免责声明:文章转载自《用PopupWindow实现弹出菜单(弹出的菜单采用自定义布局)》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇UVM_COOKBOOK学习【DUT-Testbench Connections】socket网络编程(四)——epoll多路复用问题下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

build.gradle文件详解&amp;lt;三&amp;gt;

 参考:http://blog.csdn.net/baidu_31093133/article/details/51860637 build.gradle配置参数详解      //声明是Android程序      apply plugin: 'com.android.application'      android {   //程序在编译的时候...

Object-C知识点 (二) 控件的实用属性

开发过程中的组件不常用但是很实用的属性!!!!!! #pragma mark -- UIColor colorWithPatternImage: 根据图片显示颜色,会将图片裁剪 #pragma mark--Button //自适应 [self.button sizeToFit]; //禁止触摸事件的2种方式 //方式一: 会改变按钮状态,颜色会变灰 s...

如何创建启动界面Splash Screen

启动界面Splash Screen在应用程序是很常用的,往往在启动界面中显示产品Logo、公司Logo或者开发者信息,如果应用程序启动时间比较长,那么启动界面就是一个很好的东西,可以让用户耐心等待这段枯燥的时间。Android 应用程序创建一个启动界面Splash Screen非常简单。比如创建一个工程MySample,主Acitity就叫MySample...

Android之shape属性详解

有时候 ,为了满足一些需求,我们要用到 shape 去定义 一些背景,shape 的用法 跟图片一样 ,可以给View设置 Android:background=”@drawable/shape”, 定义的shape 文件,放在 res/shape 目录下 通常我们可以用shape 做 button 的背景选择器,也可以做切换tab 时,底部的下划线。...

Android2.3.7源码结构分析

对Andorid系统进行分析或者系统功能定制的时候,我们经常需要在众多文件中花费大量时间定位所需关注的部分。为了减轻这部分枯燥而不可避免的工作,本文对2.3.7版本的源码结构进行了简单分析。希望对刚加入安卓大军的朋友们有所帮助。下图为2.3.7_r1版本的根目录结构:Makefile (makefile文件) bionic (bionic C库,...

bootstrap基本标签总结2

[布局]bootstrap基本标签总结2  缩略图 <div class="container"> <div class="row"> <div class="col-xs-6 col-md-3"> <a href="https://tool.4xseo...