您的位置:首页 > 移动开发 > Android开发

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

2016-06-02 18:56 555 查看
           这个问题,我想大家经常碰到,网上回答的很多,但是没有找到我想要的,网上提供的解决方案: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。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: