安卓:软键盘推高了整体布局,需要调整列表视图 [英] Android: soft keyboard pushes up whole layout, need to resize the listview

查看:180
本文介绍了安卓:软键盘推高了整体布局,需要调整列表视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个全屏幕聊天布局在那里我有一个在顶部标志一个头,然后沿着一个ListView所有的聊天消息,并在底部我有EDITTEXT和BUTTOM发送聊天信息。现在发生了什么,当我点击EDITTEXT是,整个布局得到推升。我需要的是,带徽标的头一直留在他们和列表视图来得到调整。

这不会做的伎俩:

 安卓windowSoftInputMode =adjustResize
 

这既不是:

 安卓windowSoftInputMode =adjustPan
 

也许是因为我有一个全屏幕的布局?

我需要找到不同的方式来调整我的ListView一次键盘是present,任何想法?在此先感谢!

下面是我的布局          

 <的LinearLayout

    机器人:layout_width =match_parent
    机器人:layout_height =70dp
    机器人:背景=@可绘制/ ss99_yellow_area_navigation_bar
    机器人:baselineAligned =假
    机器人:方向=横向>

    < ImageView的
        机器人:ID =@ + ID /后退
        机器人:layout_width =45dp
        机器人:layout_height =match_parent
        机器人:layout_marginBottom =10dp
        机器人:layout_marginLeft =5DP
        机器人:layout_marginTop =5DP
        机器人:adjustViewBounds =真
        机器人:可点击=真
        机器人:SRC =@可绘制/ ss99_button_back/>

    < ImageView的
        机器人:ID =@ + ID /标志
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =75dp
        机器人:layout_weight =1
        机器人:adjustViewBounds =真
        机器人:SRC =@可绘制/ ss99_logo_small/>

< / LinearLayout中>

< RelativeLayout的
    机器人:layout_width =match_parent
    机器人:layout_height =45dp
    机器人:layout_marginTop = -  2DP
    机器人:背景=@可绘制/ ss9_yellow_bar_chat_new>

    <的TextView
        机器人:ID =@ + ID / userOpponent
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentLeft =真
        机器人:layout_centerVertical =真
        机器人:layout_marginLeft =5DP
        机器人:文本=中文字
        机器人:textAppearance =机器人:ATTR / textAppearanceMedium/>

    <的TextView
        机器人:ID =@ + ID / USERUSER
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignBaseline =@ + ID / userOpponent
        机器人:layout_alignBottom =@ + ID / userOpponent
        机器人:layout_alignParentRight =真
        机器人:layout_marginRight =5DP
        机器人:文本=中文字
        机器人:textAppearance =机器人:ATTR / textAppearanceMedium/>

< / RelativeLayout的>

    <的ListView
        机器人:ID =@ + ID / chatList
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:cacheColorHint =#00000000
        机器人:分隔=#00000000
        机器人:listSelector =#00000000
     机器人:layout_weight =1
      机器人:stackFromBottom =真
        机器人:transcriptMode =alwaysScroll>
    < / ListView控件>

    < RelativeLayout的
        机器人:layout_width =match_parent
        机器人:layout_height =60dp
        机器人:背景=@可绘制/ ss9_yellow_area_for_bottom_and_chat_string>

           < ImageView的
                    机器人:ID =@ + ID /字段1
                    机器人:layout_width =220DP
                    机器人:layout_height =WRAP_CONTENT
                    机器人:layout_alignParentLeft =真
                    机器人:layout_alignParentTop =真
                    机器人:layout_gravity =中心
                    机器人:layout_marginLeft =20dp
                    机器人:layout_marginTop =10dp
                    机器人:adjustViewBounds =真
                    机器人:scaleType =centerInside
                    机器人:SRC =@可绘制/ ss1_field_big/>

                <的EditText
                    机器人:ID =@ + ID / chatEdit
                    机器人:layout_width =200dp
                    机器人:layout_height =WRAP_CONTENT
                    机器人:layout_alignBottom =@ + ID /字段1
                    机器人:layout_alignLeft =@ + ID /字段1
                    机器人:layout_alignTop =@ + ID /字段1
                    机器人:layout_marginBottom =3DP
                    机器人:layout_marginLeft =10dp
                    机器人:layout_marginTop =1DP
                    机器人:背景=@空
                    机器人:inputType =textAutoCorrect | textAutoComplete
                    机器人:textAppearance =机器人:ATTR / textAppearanceMedium>

                < /的EditText>

    < ImageView的
        机器人:ID =@ + ID / chatOk
        机器人:layout_width =60dp
        机器人:layout_height =60dp
        机器人:layout_alignParentRight =真
        机器人:layout_alignParentTop =真
        机器人:layout_marginBottom =5DP
        机器人:adjustViewBounds =假
        机器人:baselineAlignBottom =假
        机器人:可点击=真
        机器人:cropToPadding =假
        机器人:scaleType =fitEnd
        机器人:SRC =@可绘制/ ss4_button_go_small/>

    < / RelativeLayout的>

< / LinearLayout中>
 

解决方案

好了,所以我其实解决了这个我自己,但我认为这是不超过一个黑客,所以我可能会在以后讨论这个。但问题是它的工作原理(上银河S4测试这一点)

我调整我的ListView根据显示softKeyboard的高度并调整其大小回来一次键盘消失了。

 私有静态布尔keyboardHidden = TRUE;
私有静态诠释reduceHeight = 0;
// ...
// ...
最后查看decorView = this.getWindow()getDecorView()。
decorView.getViewTreeObserver()。addOnGlobalLayoutListener(新ViewTreeObserver.OnGlobalLayoutListener(){
    @覆盖
    公共无效onGlobalLayout(){
        矩形RECT =新的矩形();
        decorView.getWindowVisibleDisplayFrame(RECT);

        INT displayHeight = rect.bottom  -  rect.top;
        INT高= decorView.getHeight();
        布尔keyboardHiddenTemp =(双)displayHeight /身高> 0.8;
        INT mylistviewHeight = mylistview.getMeasuredHeight();

        如果(keyboardHiddenTemp!= keyboardHidden){
            keyboardHidden = keyboardHiddenTemp;

            如果(!keyboardHidden){

                reduceHeight =身高 -  displayHeight;

                LinearLayout.LayoutParams mParam =新LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,mylistviewHeight  -  reduceHeight);
                mylistview.setLayoutParams(mParam);
                mylistview.requestLayout();

            } 其他 {

                LinearLayout.LayoutParams mParam =新LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,mylistviewHeight + reduceHeight);
                mylistview.setLayoutParams(mParam);
                mylistview.requestLayout();


            }
        }

    }
});
 

I have a full screen chat layout where I have a header with a logo on the top, then follows a listView with all chat messages and at the bottom I have the editText and the buttom to send the chat message. Now what happens when I click in the editText is, that the whole layout gets pushed up. What I need is, the header with the logo to always stay where they are and the listView to gets resized.

this does not do the trick:

android:windowSoftInputMode="adjustResize"

this neither:

android:windowSoftInputMode="adjustPan"

maybe because I have a full screen layout?

I need to find a different way to resize my listView once a keyboard is present, any ideas? Thanks in advance!

Here is my layout

<LinearLayout

    android:layout_width="match_parent"
    android:layout_height="70dp"
    android:background="@drawable/ss99_yellow_area_navigation_bar"
    android:baselineAligned="false"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/back"
        android:layout_width="45dp"
        android:layout_height="match_parent"
        android:layout_marginBottom="10dp"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="5dp"
        android:adjustViewBounds="true"
        android:clickable="true"
        android:src="@drawable/ss99_button_back" />

    <ImageView
        android:id="@+id/logo"
        android:layout_width="wrap_content"
        android:layout_height="75dp"
        android:layout_weight="1"
        android:adjustViewBounds="true"
        android:src="@drawable/ss99_logo_small" />

</LinearLayout>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="45dp"
    android:layout_marginTop="-2dp"
    android:background="@drawable/ss9_yellow_bar_chat_new" >

    <TextView
        android:id="@+id/userOpponent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="5dp"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/userUser"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/userOpponent"
        android:layout_alignBottom="@+id/userOpponent"
        android:layout_alignParentRight="true"
        android:layout_marginRight="5dp"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</RelativeLayout>

    <ListView
        android:id="@+id/chatList"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:cacheColorHint="#00000000"
        android:divider="#00000000"
        android:listSelector="#00000000" 
     android:layout_weight="1"
      android:stackFromBottom="true"
        android:transcriptMode="alwaysScroll">
    </ListView>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:background="@drawable/ss9_yellow_area_for_bottom_and_chat_string" >

           <ImageView
                    android:id="@+id/field1"
                    android:layout_width="220dp"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentTop="true"
                    android:layout_gravity="center"
                    android:layout_marginLeft="20dp"
                    android:layout_marginTop="10dp"
                    android:adjustViewBounds="true"
                    android:scaleType="centerInside"
                    android:src="@drawable/ss1_field_big" />

                <EditText
                    android:id="@+id/chatEdit"
                    android:layout_width="200dp"
                    android:layout_height="wrap_content"
                    android:layout_alignBottom="@+id/field1"
                    android:layout_alignLeft="@+id/field1"
                    android:layout_alignTop="@+id/field1"
                    android:layout_marginBottom="3dp"
                    android:layout_marginLeft="10dp"
                    android:layout_marginTop="1dp"
                    android:background="@null"
                    android:inputType="textAutoCorrect|textAutoComplete"
                    android:textAppearance="?android:attr/textAppearanceMedium" >

                </EditText>

    <ImageView
        android:id="@+id/chatOk"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginBottom="5dp"
        android:adjustViewBounds="false"
        android:baselineAlignBottom="false"
        android:clickable="true"
        android:cropToPadding="false"
        android:scaleType="fitEnd"
        android:src="@drawable/ss4_button_go_small" />

    </RelativeLayout>

</LinearLayout>

解决方案

okay so I actually solved this myself, but I think this is not more than a hack so I might be revisit this at a later point. but the point is it works (tested this on a Galaxy S4)

I resize my listView according to the height of the displayed softKeyboard and resize it back once the keyboard is gone.

private static boolean keyboardHidden = true;
private static int reduceHeight = 0;
//...
//...
final View decorView = this.getWindow().getDecorView();
decorView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
    @Override
    public void onGlobalLayout() {
        Rect rect = new Rect();
        decorView.getWindowVisibleDisplayFrame(rect);

        int displayHeight = rect.bottom - rect.top;
        int height = decorView.getHeight();
        boolean keyboardHiddenTemp = (double)displayHeight / height > 0.8 ;
        int mylistviewHeight = mylistview.getMeasuredHeight();

        if (keyboardHiddenTemp != keyboardHidden) {                 
            keyboardHidden = keyboardHiddenTemp;

            if (!keyboardHidden) {      

                reduceHeight = height - displayHeight;

                LinearLayout.LayoutParams mParam = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,mylistviewHeight - reduceHeight); 
                mylistview.setLayoutParams(mParam);
                mylistview.requestLayout();

            } else {

                LinearLayout.LayoutParams mParam = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,mylistviewHeight + reduceHeight); 
                mylistview.setLayoutParams(mParam);
                mylistview.requestLayout();


            }
        }

    }
}); 

这篇关于安卓:软键盘推高了整体布局,需要调整列表视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆