关于Editext获得焦点后,View上移问题。

editText获得焦点,系统的软键盘会自动弹出,导致view所在位置上移。

问题一:

描述:点击editText后键盘弹起,此时需要显示popupWindow。显示之前先把软键盘关闭,在现实popupwindow,会发现popupwindow不在原来设定的位置显示了,位置被上移了。

解决方案:

设置popupwindow属性:

//解决关闭软件盘后,popupwindow显示位置上移问题

popWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

new Handler().postDelayed(new Runnable() {
    @Override
    public void run() {
        popWindow.showAsDropDown(ln_search_layout);
    }
}, 50);
问题二:

描述:Android实现软键盘弹出后,EdiText上移,背景不动。

1.默认情况:键盘弹出后,EditText跟随键盘的弹出上移,背景图也跟随上移切顶部被屏幕遮住了。如图:




 

2.实现软键盘弹出后,EdiText上移,背景不动。见效果图:(QQ登录页面就是这样的效果可以参考)

具体实现:

布局:

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

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:overScrollMode="never"
        android:scrollbars="none">

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

            <ImageView
                android:id="@+id/imageView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="centerCrop"
                android:src="@mipmap/timg"/>
        </LinearLayout>
    </ScrollView>

    <RelativeLayout
        android:id="@+id/viewEdit"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_alignParentBottom="true"
        android:background="#88f1abcd">

        <EditText
            android:id="@+id/editText"
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:layout_alignParentBottom="true" />
    </RelativeLayout>
</RelativeLayout>

注:布局不能移动的部分(这里使用ImageView举例)需要放在一个LinearLayout或者RelativeLayout里,并且外层需要套一个ScrollView,少一层都不行。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.yhy.testviewdemo">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".TestActivity"
           android:windowSoftInputMode="adjustResize" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

注:此处必须设置windowSoftInputMode="adjustResize"

package com.yhy.testviewdemo;

import android.graphics.Rect;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class TestActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题
        setContentView(R.layout.activity_test);
        ImageView imageView = (ImageView) findViewById(R.id.imageView);
        Rect rect = new Rect();//动态设置不能移动部分的高度
        getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) imageView.getLayoutParams();
        params.height = rect.bottom - rect.top;
    }


} 

注:

getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);///取得整个视图部分,注意,如果你要设置标题样式,这个必须出现在标题样式之后,否则会出错
int top = rect.top;状态栏的高度,所以rect.height,rect.width分别是系统的高度的宽度
View v = getWindow().findViewById(Window.ID_ANDROID_CONTENT);///获得根视图
int top2 = v.getTop();///状态栏标题栏的总高度,所以标题栏的高度为top2-top
int width = v.getWidth();///视图的宽度,这个宽度好像总是最大的那个
int height = v.getHeight();视图的高度,不包括状态栏和标题栏
如果只想取得屏幕大小,可以用
Display display = getWindowManager().getDefaultDisplay() ;

参考:https://blog.csdn.net/xuewater/article/details/25109615

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值