• 聊天页面输入框和发送按钮的布局问题 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>
  • 相关阅读:
    js联系题目
    js运算符
    太极图
    第一周 Welcome
    对 vscode 自动格式化的结果不太满意,我们该如何自己调整直至自己满意为止
    ASP.NET MVC5.0 OutputCache不起效果
    对照实验(1)-批量清理系统临时文件
    ES6
    19.局部变量和全局变量
    18.函数定义和参数
  • 原文地址:https://www.cnblogs.com/tonglingqijie/p/4692419.html
Copyright © 2020-2023  润新知