自定义底部弹出的PopupWindow【So Easy!】

第一步:

创建自己的xml文件。

<?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="wrap_content"
    android:background="@color/white"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/details_comment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@mipmap/comui_bar_top_shadow" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0px"
            android:layout_weight="1"
            android:minHeight="100px"
            android:gravity="center_vertical"
            android:orientation="horizontal"
            android:paddingLeft="30px"
            android:paddingRight="30px">

            <EditText
                android:id="@+id/details_comment_edit"
                android:layout_width="0px"
                android:layout_height="60px"
                android:layout_weight="1"
                android:background="@drawable/shape_search"
                android:gravity="center_vertical"
                android:hint="请输入评论"
                android:maxLength="3"
                android:paddingLeft="20px"
                android:paddingRight="20px"
                android:textSize="28px"
                android:textColorHint="#666666" />

            <TextView
                android:id="@+id/details_comment_submit"
                android:layout_width="120px"
                android:layout_height="50px"
                android:layout_marginLeft="20px"
                android:background="@drawable/shape_getcode"
                android:gravity="center"
                android:text="发送"
                android:textColor="@color/colorAccent"
                android:textSize="30px" />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

第二步:

上代码:

View view = View.inflate(getContext(), R.layout.popup_comment, null);
//此处可按需求为各控件设置属性
view.findViewById(R.id.details_comment_submit).setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
    }
});
AutoUtils.auto(view);
PopupWindow popupWindow = new PopupWindow(view);
//设置弹出窗口大小
popupWindow.setWidth(WindowManager.LayoutParams.FILL_PARENT);
popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
//必须设置以下两项,否则弹出窗口无法取消
popupWindow.setFocusable(true);
popupWindow.setBackgroundDrawable(new ColorDrawable(0x00000000));
//设置动画效果
popupWindow.setAnimationStyle(R.style.AnimBottom);
//设置显示位置,findViewById获取的是包含当前整个页面的view
popupWindow.showAtLocation(add, Gravity.BOTTOM, 0, 0);

解释:

1.

View view = View.inflate(getContext(), R.layout.popup_comment, null);

其中的popup_comment即为自定义的PopupWindow中的xml文件。

2.

popupWindow.setAnimationStyle(R.style.AnimBottom);

这个地方为设置动画效果。

第一步:

在style文件中加入:

<style name="AnimBottom" parent="@android:style/Animation">
    <item name="android:windowEnterAnimation">@anim/push_bottom_in</item>
    <item name="android:windowExitAnimation">@anim/push_bottom_out</item>
</style>

第二步:

在res文件夹下,创建anim文件夹。

在anim文件夹里面创建push_bottom_in.xml

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

在anim文件夹里面创建push_bottom_out.xml

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

3.

popupWindow.showAtLocation(findViewById(R.id.comment), Gravity.BOTTOM, 0, 0);
这个地方的findViewById(R.id.comment),为你要显示到的地方的控件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值