android开发步步为营之105:解决键盘弹起页面被顶上去问题

           这个问题,我想大家经常碰到,网上回答的很多,但是没有找到我想要的,网上提供的解决方案:1、比如Android:windowsoftinputmode="adjustpan"  2、使用scrollview 两种都没有解决我的问题,后来我就各种调试啊,各种的Android:windowsoftinputmode组合,都不可以解决,后来,仔细想了下会不会页面的RelativeLayout的问题,然后将页面的布局换了一遍,最外层的RelativeLayout换成LinearLayout或者FrameLayout,问题解决。下面来分享一下。

           先看下之前的效果(编辑第一个标签):

            

           现在的效果(同样编辑第一个标签)

           

           原先的页面设计:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout_meme"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <include
        android:id="@+id/layout_head"
        layout="@layout/layout_head"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:layout_alignParentTop="true"></include>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/layout_head"
        android:layout_marginTop="22dp"
        android:gravity="center">

        <RelativeLayout
            android:id="@+id/layout_meme_container"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content">

            <ImageView
                android:id="@+id/img_meme"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:background="@color/preview_bg"
                android:scaleType="fitXY"
                android:src="@drawable/preview_default" />
        </RelativeLayout>
    </RelativeLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal">

        <EditText
            android:id="@+id/et_message"
            android:layout_width="wrap_content"
            android:layout_height="40dp"
            android:layout_marginBottom="8dp"
            android:layout_marginEnd="10dp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginStart="10dp"
            android:layout_marginTop="8dp"
            android:layout_weight="11"
            android:background="@drawable/shape_textview_background"
            android:hint="@string/lbl_input_texts"
            android:maxLength="100"
            android:textColor="#666666"
            android:textCursorDrawable="@null"
            android:textSize="16sp" />

        <ImageView
            android:id="@+id/img_random"
            android:layout_width="60dp"
            android:layout_height="40dp"
            android:layout_gravity="center_vertical"
            android:layout_marginEnd="5dp"
            android:layout_marginRight="5dp"
            android:layout_weight="1"
            android:background="@drawable/selector_button_bg"
            android:scaleType="center"
            android:src="@drawable/ic_random_meme" />
    </LinearLayout>


</RelativeLayout>


           现在的页面设计:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout_meme"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <include
        android:id="@+id/layout_head"
        layout="@layout/layout_head"
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:layout_alignParentTop="true"></include>

    <FrameLayout
        android:id="@+id/layout_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_below="@+id/layout_head"
        android:layout_marginTop="22dp"
        android:gravity="center">

        <ImageView
            android:id="@+id/img_meme"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:background="@color/preview_bg"
            android:scaleType="fitXY"
            android:src="@drawable/preview_default" />
        <RelativeLayout
            android:layout_gravity="center_horizontal"
            android:id="@+id/layout_meme_container"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content">
        </RelativeLayout>
    </FrameLayout>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal">

        <EditText
            android:id="@+id/et_message"
            android:layout_width="wrap_content"
            android:layout_height="40dp"
            android:layout_marginBottom="8dp"
            android:layout_marginEnd="10dp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginStart="10dp"
            android:layout_marginTop="8dp"
            android:layout_weight="11"
            android:background="@drawable/shape_textview_background"
            android:hint="@string/lbl_input_texts"
            android:maxLength="100"
            android:textColor="#666666"
            android:textCursorDrawable="@null"
            android:textSize="16sp" />

        <ImageView
            android:id="@+id/img_random"
            android:layout_width="60dp"
            android:layout_height="40dp"
            android:layout_gravity="center_vertical"
            android:layout_marginEnd="5dp"
            android:layout_marginRight="5dp"
            android:layout_weight="1"
            android:background="@drawable/selector_button_bg"
            android:scaleType="center"
            android:src="@drawable/ic_random_meme" />
    </LinearLayout>


</LinearLayout>

           另外textview设置onTouch事件

memeTextView.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    //如果标签在下面,点击时则页面向上顶起,保证编辑的textview可见
                    if(memeTextView.getTop()>=CommonUtil.getScreenHeight(MemeMakeActivity.this)/2)
                    {
                        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

                    }else {
                        //如果标签在页面上面,点击时则页面不需要向上顶起,保证编辑的textview可见
                        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
                    }

                    imgEdit.setVisibility(View.VISIBLE);
                    mCurrentMemeText = memeTextView;
                    String title = txtTitle.getText().toString();
                    mEtText.setText(title);
                    mEtText.setSelection(title.length());
                    clearChoosedStatus(mCurrentMemeText);
                    return false;
                }
            });



           总结:设计页面的时候,尽量少用的RelativeLayout,多用用LinearLayout。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值