键盘弹出不顶activity的bug

因为在全屏模式下   是不好使的

需要在activity    setContentView(R.layout.XXXX)之后调用;

AndroidBug5497Workaround.assistActivity(this);


importandroid.app.Activity;

importandroid.graphics.Rect;

importandroid.view.View;

importandroid.view.ViewTreeObserver;

importandroid.widget.FrameLayout;

public classAndroidBug5497Workaround {

// For more information, see https://code.google.com/p/android/issues/detail?id=5497

// To use this class, simply invoke assistActivity() on an Activity that already has its content view set.

public static voidassistActivity(Activity activity) {

newAndroidBug5497Workaround(activity);

}

privateViewmChildOfContent;

private intusableHeightPrevious;

privateFrameLayout.LayoutParamsframeLayoutParams;

privateAndroidBug5497Workaround(finalActivity activity) {

FrameLayout content = (FrameLayout) activity.findViewById(android.R.id.content);

mChildOfContent= content.getChildAt(0);

mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(newViewTreeObserver.OnGlobalLayoutListener() {

public voidonGlobalLayout() {

possiblyResizeChildOfContent(activity);

}

});

frameLayoutParams= (FrameLayout.LayoutParams)mChildOfContent.getLayoutParams();

}

private voidpossiblyResizeChildOfContent(Activity activity) {

intusableHeightNow = computeUsableHeight(activity);

if(usableHeightNow !=usableHeightPrevious) {

intusableHeightSansKeyboard =mChildOfContent.getRootView().getHeight();

intheightDifference = usableHeightSansKeyboard - usableHeightNow;

if(heightDifference > (usableHeightSansKeyboard/4)) {

// keyboard probably just became visible

frameLayoutParams.height= usableHeightSansKeyboard - heightDifference;

}else{

// keyboard probably just became hidden

frameLayoutParams.height= usableHeightSansKeyboard;

}

mChildOfContent.requestLayout();

usableHeightPrevious= usableHeightNow;

}

}

private intcomputeUsableHeight(Activity activity) {

Rect r =newRect();

mChildOfContent.getWindowVisibleDisplayFrame(r);

if(r.top==0){

r.top=getStatusBarHeight(activity);//状态栏目的高度

}

return(r.bottom- r.top);

}

public intgetStatusBarHeight(Activity activity) {

intresult =0;

intresourceId = activity.getResources().getIdentifier("status_bar_height","dimen","android");

if(resourceId >0) {

result = activity.getResources().getDimensionPixelSize(resourceId);

}

returnresult;

}

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 6,510评论 0 17
  • Day1: 在代码中通过R.string.hello_world可以获得该字符串的引用; 在XML中通过@stri...
    冰凝雪国阅读 1,441评论 0 5
  • 哎呀呀 ,马上就要面临找工作了,媛媛心里紧张呀. 作为一个即将毕业的Android程序媛,开始面临找工作了,...
    左神话阅读 4,850评论 7 59
  • 动态创建fragment的流程 1.0 新建一个类继承fragment. 2.0 在自定义的fragment里面复...
    左神话阅读 2,233评论 0 3
  • 这是谷歌官方给我们提供的一个兼容低版本安卓设备的软件包,里面包囊了只有在安卓3.0以上可以使用的api。而view...
    Ten_Minutes阅读 5,756评论 1 19