聊天页面输入框和发送按钮的布局问题 Android

布局文件如下

 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
	<TableLayout 
	    android:id="@+id/bottom_content"
	    android:layout_width="fill_parent"
	    android:layout_height="50dp"
	    android:layout_alignParentBottom="true">
	    
	 <TableRow>
	     <TableRow android:layout_weight="3">
	        <EditText
      		android:hint = "@string/input_info"
      		android:id = "@+id/MessageText"/>
	 	</TableRow>
      	<TableRow android:layout_weight="1">
          <Button
      		android:text="@string/send_msg"
      		android:id = "@+id/MessageButton"/>
      	</TableRow>
	 </TableRow>
	</TableLayout>
 
 <ListView  
	android:id="@+id/list"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop = "true"
    android:layout_above="@+id/bottom_content"
    />
</RelativeLayout>


让输入框的宽度占屏幕宽度的3/4,发送按钮的宽度占屏幕宽度1/4。

 

但是当输入的文字过多并且关闭输入键盘或者打开输入键盘的时候,输入框的宽度会挤压发送按钮,当文字的宽度达到一定宽度,发送按钮会“消失”(看不见的状态)。

这里提供一个简单的解决办法:在Java代码中给该EditText设置一个监听器addTextChangedListener,监听输入内容的变化,然后再重写onTextChanged方法,使输入框的宽度占全屏的3/4,发送按钮的宽度占全屏的1/4。

核心代码如下,只需要在onTextChanged中设定输入框和发送按钮的宽度分别占3/4和1/4即可:

messageSend=(Button)findViewById(R.id.MessageButton);
editText=(EditText)findViewById(R.id.MessageText);
editText.addTextChangedListener(new TextWatcher() {	
	@Override
	public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
		editText.setWidth((3*screenWidth)/4);
		messageSend.setWidth(screenWidth/4);
	}	
	@Override
	public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,int arg3) {	
	}	
	@Override
	public void afterTextChanged(Editable arg0) {	
	}
});
<span style="font-family:Arial;BACKGROUND-COLOR: #ffffff"></span>
posted @ 2015-07-31 16:02  统领七界  阅读(1538)  评论(0编辑  收藏  举报