zoukankan      html  css  js  c++  java
  • 聊天页面输入框和发送按钮的布局问题 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判断是否是合法的端口号
    解决tomcat中文乱码
    使用java代码删除nexus maven仓库中的jar包、pom.xml等组件
    基于 Kubernetes 构建企业 Jenkins 持续集成平台
    minio部署
    elaticsearch 部署
    skywalking 8.0 配置文件
    使用node local dns来提升ClusterDNS服务质量
    kubernetes之监控Operator部署Prometheus
    夜莺监控+prometheus 架构图
  • 原文地址:https://www.cnblogs.com/tonglingqijie/p/4692419.html
Copyright © 2011-2022 走看看