Android PopupWindow实现从顶部弹出下拉菜单左、中、右

 1.首先在布局文件中定义一个按钮,用于触发弹出菜单的操作:

<Button
    android:id="@+id/btn_popup_menu"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="弹出菜单" />

  2.在代码中实现菜单的弹出操作。这里我们使用PopupWindow来实现:

public class MainActivity extends AppCompatActivity {

    private Button mBtnPopupMenu;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mBtnPopupMenu = findViewById(R.id.btn_popup_menu);
        mBtnPopupMenu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showPopupMenu();
            }
        });
    }

    private void showPopupMenu() {
        View popupView = LayoutInflater.from(this).inflate(R.layout.popup_menu, null);

        // 创建PopupWindow对象,其中参数1是要显示的View对象,参数2是PopupWindow的宽度,参数3是PopupWindow的高度,参数4表示PopupWindow是否具有焦点
        PopupWindow popupWindow = new PopupWindow(popupView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);

        // 设置PopupWindow的背景
        popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

        // 设置PopupWindow的进入和退出动画
        popupWindow.setAnimationStyle(R.style.PopupTopAnim);

        // 显示PopupWindow,其中参数1是锚点View,参数2和参数3分别表示PopupWindow的X轴和Y轴的偏移量
        popupWindow.showAtLocation(mBtnPopupMenu, Gravity.TOP | Gravity.CENTER_HORIZONTAL, 0, 0);
    }
}

3.实现菜单布局文件popup_menu.xml。这里我们以从顶部中间弹出为例:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="@android:color/white">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="左侧"
        android:textSize="18sp"
        android:padding="12dp"
        android:gravity="center_vertical"
        android:background="?android:attr/selectableItemBackground" />

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/divider_color" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="中间"
        android:textSize="18sp"
        android:padding="12dp"
        android:gravity="center_vertical"
        android:background="?android:attr/selectableItemBackground" />

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/divider_color" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="右侧"
        android:textSize="18sp"
        android:padding="12dp"
        android:gravity="center_vertical"
        android:background="?android:attr/selectableItemBackground" />

</LinearLayout>

4.接下来实现弹出动画样式PopupTopAnim。在res目录下的values文件夹中新建一个anim.xml文件,内容如下:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:fromYDelta="-100%"
        android:toYDelta="0"
        android:duration="300" />
</set>

这里的fromYDelta和toYDelta属性分别表示动画开始时和结束时的Y轴偏移量,这里设置为从上方滑入。

5.最后需要注意的是,如果需要实现从左侧或右侧弹出菜单,只需要修改showPopupMenu()方法中的Gravity值和弹出动画PopupLeftAnim、PopupRightAnim即可。具体可参考以下代码:

private void showPopupMenu() {
    View popupView = LayoutInflater.from(this).inflate(R.layout.popup_menu, null);

    // 创建PopupWindow对象,其中参数1是要显示的View对象,参数2是PopupWindow的宽度,参数3是PopupWindow的高度,参数4表示PopupWindow是否具有焦点
    PopupWindow popupWindow = new PopupWindow(popupView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT, true);

    // 设置PopupWindow的背景
    popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

    // 设置PopupWindow的进入和退出动画
    popupWindow.setAnimationStyle(R.style.PopupLeftAnim);

    // 显示PopupWindow,其中参数1是锚点View,参数2和参数3分别表示PopupWindow的X轴和Y轴的偏移量
    popupWindow.showAtLocation(mBtnPopupMenu, Gravity.LEFT | Gravity.CENTER_VERTICAL, 0, 0);
}

这里我们以从左侧弹出为例,Gravity值设置为LEFT | CENTER_VERTICAL,弹出动画样式为PopupLeftAnim。从右侧弹出的代码类似,只需要将Gravity值改为RIGHT | CENTER_VERTICAL,弹出动画样式改为PopupRightAnim即可。

这样,一个完整的Android PopupWindow实现从顶部弹出下拉菜单的例子就完成了。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值