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