仿抖音评论,弹出PopuWindow位于屏幕底端时EditText遮挡问题

我们都知道popuwind弹出时,如果popuwindow里面有edittext会使软件盘遮挡不弹出,需要设置如下代码。

//设置弹出窗体需要软键盘,
window.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
//再设置模式,和Activity的一样,覆盖。
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

我这里是低仿抖音,高仿腾讯微视做的,popuWindow的Edittext是位于屏幕底部的,弹出后会有一些遮挡

因为设置了Edittext的background为null

 

然后设置了Edittext的background为透明之后,成功变得好看了许多。

好,图片粘贴完毕,开始干货代码走起!!!

首先是Activity:

//弹出popuWindow
private void ShowPopuWindow(int height) {
    View contentView = LayoutInflater.from(this).inflate(R.layout.faxian_message_item, null, false);
    AutoUtils.auto(contentView);
    window = new PopupWindow(this);
    window.setContentView(contentView);
    window.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
    //设置高度
    int screenHeigh = getResources().getDisplayMetrics().heightPixels;
    window.setHeight(Math.round(screenHeigh * 0.7f));
    window.setOutsideTouchable(true);
    window.setTouchable(true);
    window.setFocusable(true);
    //设置弹出窗体需要软键盘,
    window.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
    //再设置模式,和Activity的一样,覆盖。
    window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

    // 实例化一个ColorDrawable颜色为半透明
    ColorDrawable dw = new ColorDrawable(0xb0000000);
    // 设置弹出窗体的背景
    window.setBackgroundDrawable(dw);
    //window.update();
    window.setAnimationStyle(R.style.popwin_anim_style);
    window.showAtLocation(layout, Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, height);
    //设置背景颜色
    RelativeLayout popu_layout = (RelativeLayout) contentView.findViewById(R.id.layout);
    popu_layout.setBackgroundColor(Color.argb(105,0,0,0));
    LinearLayout bottom_layout = (LinearLayout) contentView.findViewById(R.id.bottom_layout);
    RecyclerView recyclerView = (RecyclerView) contentView.findViewById(R.id.recycleView);
    LinearLayoutManager manager = new LinearLayoutManager(FaXianActivity.this, LinearLayoutManager.VERTICAL, false);
    recyclerView.setLayoutManager(manager);
    adapter = new PopuwindowAdapter(FaXianActivity.this, list);
    recyclerView.setAdapter(adapter);
    initMessage();
    ImageView close = (ImageView) contentView.findViewById(R.id.image_close);
    close.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            window.dismiss();
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);//开启或者关闭软键盘
            edit_text.setText("");
        }
    });
    edit_text = (EditText) contentView.findViewById(R.id.edit_message);
    commit = (TextView) contentView.findViewById(R.id.text_commit);
    commit1 = (TextView) contentView.findViewById(R.id.text_commit1);
    message_num = (TextView) contentView.findViewById(R.id.message_num);
    commit.setEnabled(false);
    commit1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) { 
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);//开启或者关闭软键盘
            edit_text.setText("");
        }
    });
    edit_text.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }

        @Override
        public void afterTextChanged(Editable s) {
            String trim = s.toString().trim();
            if (!TextUtils.isEmpty(trim)) {
                //个人较懒,直接控件显示隐藏了
                commit.setVisibility(View.GONE);
                commit1.setVisibility(View.VISIBLE);
            } else {
                commit.setVisibility(View.VISIBLE);
                commit1.setVisibility(View.GONE);
            }
        }
    });
}

popuWindow的动画:

首先Style文件里写一个:

<style name="popwin_anim_style">
    <item name="android:windowEnterAnimation">@anim/menushow</item>
    <item name="android:windowExitAnimation">@anim/menuhide</item>
</style>

弹出动画menuShow:

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

关闭动画menuhide:

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

 

然后是popuWindow的布局:

?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <TextView
        android:id="@+id/message_num"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20px"
        android:gravity="center"
        android:text="共0条评论"
        android:textColor="@color/colorWhite"
        android:textSize="30px"
        android:textStyle="bold" />

    <ImageView
        android:id="@+id/image_close"
        android:layout_width="35px"
        android:layout_height="35px"
        android:layout_alignParentRight="true"
        android:layout_marginTop="20px"
        android:layout_marginRight="20px"
        android:src="@mipmap/close_popu" />


    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycleView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/bottom_layout"
        android:layout_below="@+id/message_num"
        android:layout_marginTop="20px"></android.support.v7.widget.RecyclerView>

    <LinearLayout
        android:id="@+id/bottom_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="#000000"
        android:orientation="horizontal"
        >

        <EditText
            android:id="@+id/edit_message"
            android:layout_width="0px"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginLeft="30px"
            android:layout_weight="1"
            android:hint="说点实在的"
            android:maxLines="3"
            android:background="@drawable/edit_end"
            android:textColor="@color/white"
            android:textColorHint="@color/white"
            android:textSize="30px" />

        <TextView
            android:id="@+id/text_commit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5px"
            android:layout_marginRight="20px"
            android:background="@drawable/message_shape"
            android:layout_gravity="center_vertical"
            android:gravity="center"
            android:paddingLeft="20px"
            android:paddingTop="12px"
            android:paddingRight="20px"
            android:paddingBottom="12px"
            android:text="发送"
            android:textColor="@color/black"
            android:textCursorDrawable="@null"
            android:textSize="28px"
            android:visibility="visible" />

        <TextView
            android:id="@+id/text_commit1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5px"
            android:layout_marginRight="20px"
            android:background="@drawable/message_shape_red"
            android:gravity="center"
            android:paddingLeft="20px"
            android:paddingTop="12px"
            android:paddingRight="20px"
            android:paddingBottom="12px"
            android:text="发送"
            android:layout_gravity="center_vertical"
            android:textColor="@color/white"
            android:textSize="28px"
            android:visibility="gone" />
    </LinearLayout>


</RelativeLayout>

发送按钮的圆形边框:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <corners android:radius="20px"/>
    <stroke android:color="@color/white"/>
    <solid android:color="@color/white"/>
</shape>

重点来了!!!EditText的下划线,在Drwaable文件夹下创建:

这个是edit_end的xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/edit_select" android:state_focused="false" />
    <item android:drawable="@drawable/edit_select" android:state_focused="true" />
</selector>

这个是edit_select的xml:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:bottom="15px"
        android:top="15px">
        <shape>
            <solid android:color="@android:color/transparent" />
            <stroke
                android:color="@color/black"
                android:width="10px" />
            <padding android:bottom="22px"
                android:top="32px"
                />
        </shape>
    </item>
</layer-list>

最后贴一张效果图:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值