保持ListView的最后一个可见项目时的ListView控件的大小而变化控件、小而、项目、ListView

2023-09-04 08:02:58 作者:不择手段๑

我要的是简单的,至少我认为这将是简单的。 我只想一个窗口,一个EditText是在屏幕的底部,其余空间被填充的ListView。 不幸的是,它并没有如我所料工作。我要的是下面的图片。有没有简单的方法来做到这一点的XML,或者我应该写一些特殊的code这个?

我有问题的Andr​​oid源$ C ​​$ C。

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =FILL_PARENT
机器人:方向=垂直>
<的ListView
    机器人:ID =@ + ID / demolist
    机器人:layout_width =match_parent
    机器人:layout_height =FILL_PARENT
    机器人:layout_weight =1
     >
< / ListView控件>
    <的EditText
        机器人:ID =@ + ID / editText1
        机器人:layout_width =match_parent
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_weight =0>
    < /的EditText>
< / LinearLayout中>
 

解决方案

你想要什么

最后可见项目没有键盘的

最后可见项目通过键盘的

最后可见项目有了更大的输入的

这是我做到了。

摘要

设置机器人:stackFromBottom =真正的和机器人:transcriptMode =alwaysScroll在列表视图 设置机器人:windowSoftInputMode =adjustPan在活动 呼叫 adapter.notifyDataSetChanged(); 当你添加新的聊天记录,这样的列表视图将一直滚动到最后位置 设置机器人:inputType =textMultiLine&放大器; 机器人:MAXLINES =4中的EditText种植文本框中

详细信息

活动的

 <活动机器人:名称=MainActivity
机器人:screenOrientation =画像
机器人:windowSoftInputMode =adjustPan>< /活性GT;
 

聊天视图的

  

 < RelativeLayout的
    机器人:ID =@ + ID / chat_header
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =40dp
    机器人:填充=3DP>

    <按钮
        机器人:ID =@ + ID / btnBackChat
        机器人:layout_width =35dp
        机器人:layout_height =35dp
        机器人:layout_alignParentLeft =真
        机器人:背景=@可绘制/ back_button/>

    <的TextView
        机器人:ID =@ + ID / txt_name
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignBottom =@ + ID / btnBackChat
        机器人:layout_centerHorizo​​ntal =真
        机器人:文字颜色=@机器人:彩色/白
        机器人:TEXTSIZE =18sp/>

    < ImageView的
        机器人:ID =@ + ID / imgViewProfileChat
        机器人:layout_width =35dp
        机器人:layout_height =35dp
        机器人:layout_alignParentRight =真
        />
< / RelativeLayout的>

<的ListView
    机器人:ID =@ + ID / ListView1的
    机器人:layout_width =match_parent
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_above =@ + ID /表
    机器人:layout_below =@ + ID / chat_header
    机器人:stackFromBottom =真
    机器人:transcriptMode =alwaysScroll/>

< RelativeLayout的
    机器人:ID =@ + ID /表
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_alignParentBottom =真
    机器人:layout_alignParentLeft =真
    机器人:方向=垂直>

    <的ImageButton
        机器人:ID =@ + ID / btnAttach
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentLeft =真
        机器人:layout_alignParentTop =真
        机器人:contentDescription =@字符串/附加
        机器人:SRC =@可绘制/ attach_icon/>

    <的EditText
        机器人:ID =@ + ID / txtChat
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_toLeftOf =@ + ID / btnSend
        机器人:layout_toRightOf =@ + ID / btnAttach
        机器人:inputType =textMultiLine
        机器人:MAXLINES =4/>

    <按钮
        机器人:ID =@ + ID / btnSend
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_alignParentRight =真
        机器人:layout_alignParentTop =真
        机器人:文本=@字符串/发送/>
< / RelativeLayout的>
 
ListView控件

     

What I want is simple, well at least I thought it would be simple. I just want a window where an EditText is on the bottom of the screen, and the rest of the space is filled with ListView. Unfortunately, it did not work as I expected. What I want is the following image. Is there any easy way to do this in the XML, or should I write some special code for this?

My Problematic Android Source Code.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
    android:id="@+id/demolist"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:layout_weight="1"
     >
</ListView>
    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" 
        android:layout_weight="0">
    </EditText>
</LinearLayout >

解决方案

What you Want

Last Visible Item Without Keyboard

Last Visible Item With Keyboard

Last Visible Item With larger input

Here is how i did it.

Summary

Set android:stackFromBottom="true" and android:transcriptMode="alwaysScroll" in your list view Set android:windowSoftInputMode="adjustPan" in Activity Call adapter.notifyDataSetChanged(); when you add new chats so that the list view will always scroll to the last position Set android:inputType="textMultiLine" & android:maxLines="4" in EditText for growing text box

Details

Activity

<activity android:name="MainActivity"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustPan"></activity>

Chat View

<RelativeLayout
    android:id="@+id/chat_header"
    android:layout_width="fill_parent"
    android:layout_height="40dp"
    android:padding="3dp" >

    <Button
        android:id="@+id/btnBackChat"
        android:layout_width="35dp"
        android:layout_height="35dp"
        android:layout_alignParentLeft="true"
        android:background="@drawable/back_button" />

    <TextView
        android:id="@+id/txt_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/btnBackChat"
        android:layout_centerHorizontal="true"
        android:textColor="@android:color/white"
        android:textSize="18sp" />

    <ImageView
        android:id="@+id/imgViewProfileChat"
        android:layout_width="35dp"
        android:layout_height="35dp"
        android:layout_alignParentRight="true"
        />
</RelativeLayout>

<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/form"
    android:layout_below="@+id/chat_header"
    android:stackFromBottom="true"
    android:transcriptMode="alwaysScroll"/>

<RelativeLayout
    android:id="@+id/form"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:orientation="vertical" >

    <ImageButton
        android:id="@+id/btnAttach"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:contentDescription="@string/attach"
        android:src="@drawable/attach_icon" />

    <EditText
        android:id="@+id/txtChat"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_toLeftOf="@+id/btnSend"
        android:layout_toRightOf="@+id/btnAttach"
        android:inputType="textMultiLine"
        android:maxLines="4" />

    <Button
        android:id="@+id/btnSend"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:text="@string/send" />
</RelativeLayout>