如何配对两个EditTexts的高度 - Android

时间:2016-08-01 03:24:36

标签: android android-edittext android-xml

我的应用程序中有两个EditTexts并排。

这是他们使用1行输入

的样子

enter image description here

目前,当我在一个EditText中输入多行输入时会增长,但另一行保持相同的高度 - 如此处所示

enter image description here

我希望EditTexts的高度匹配。当一个人成长时,另一个人的身高就会达到 - 就像这里看到的那样

enter image description here

不确定如何达到此效果。我不想用新行填写其他EditText。我希望实际View增长。

这是我目前的布局

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:layout_marginTop="10dp"
    android:weightSum="6"
    android:orientation="horizontal">
    <EditText
        android:id="@+id/left"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        android:inputType="textMultiLine"
        android:gravity="top|start"/>
    <EditText
        android:id="@+id/right"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        android:inputType="textMultiLine"
        android:gravity="top|start"/>
</LinearLayout>

3 个答案:

答案 0 :(得分:0)

尝试在onTextChange方法中更改其他edittext的高度...示例

class SeasonsController < ApplicationController
  before_action :authenticate_user!
  before_action :ensure_admin_user!, except: [:show, :index]

  ...

  private

  def ensure_admin_user!
    render_error unless current_user.admin?
  end

  def render_error
    # whatever
  end
end

答案 1 :(得分:0)

请尝试以下方法,

<LinearLayout
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:orientation="horizontal"
android:weightSum="2">

<LinearLayout
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:layout_width="0dp"
    android:orientation="vertical"
    android:padding="5dp">

    <EditText
        android:gravity="top|start"
        android:id="@+id/left"
        android:inputType="textMultiLine"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:layout_width="match_parent" />
</LinearLayout>

<LinearLayout
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:layout_width="0dp"
    android:orientation="vertical"
    android:padding="5dp">

    <EditText
        android:gravity="top|start"
        android:id="@+id/right"
        android:inputType="textMultiLine"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:layout_width="match_parent" />
</LinearLayout>

</LinearLayout>

答案 2 :(得分:0)

我做了一些研究并遇到this回答。根据链接的答案,我创建了这个解决方案:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:layout_marginTop="10dp"
    android:orientation="horizontal"
    android:weightSum="6">

    <View
        android:id="@+id/strut"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_centerHorizontal="true" />

    <EditText
        android:id="@+id/left"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignRight="@id/strut"
        android:gravity="top|start"
        android:layout_alignBottom="@+id/right"
        android:inputType="textMultiLine" />

    <EditText
        android:id="@+id/right"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@id/strut"
        android:layout_alignParentRight="true"
        android:gravity="top|start"
        android:inputType="textMultiLine" />
</RelativeLayout>

在你的java代码中执行以下操作:

        editTextLeft = (EditText) findViewById(R.id.left);
        editTextRight = (EditText) findViewById(R.id.right);
        editTextLeft.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if (b) {
                    RelativeLayout.LayoutParams leftLayoutParams = (RelativeLayout.LayoutParams) editTextLeft.getLayoutParams();
                    RelativeLayout.LayoutParams rightLayoutParams = (RelativeLayout.LayoutParams) editTextRight.getLayoutParams();
                    leftLayoutParams.removeRule(RelativeLayout.ALIGN_BOTTOM);
                    rightLayoutParams.addRule(RelativeLayout.ALIGN_BOTTOM,editTextLeft.getId());
                    int lines = editTextRight.getLineCount() > editTextLeft.getLineCount() ? editTextRight.getLineCount() : editTextLeft.getLineCount();
                    editTextLeft.setMinLines(lines);
                    editTextRight.setMinLines(lines);
                    editTextLeft.setLayoutParams(leftLayoutParams);
                    editTextRight.setLayoutParams(rightLayoutParams);

                }
            }
        });
        editTextRight.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean b) {
                if(b) {
                    RelativeLayout.LayoutParams leftLayoutParams = (RelativeLayout.LayoutParams) editTextLeft.getLayoutParams();
                    RelativeLayout.LayoutParams rightLayoutParams = (RelativeLayout.LayoutParams) editTextRight.getLayoutParams();
                    rightLayoutParams.removeRule(RelativeLayout.ALIGN_BOTTOM);
                    leftLayoutParams.addRule(RelativeLayout.ALIGN_BOTTOM, editTextRight.getId());
                    int lines = editTextRight.getLineCount() > editTextLeft.getLineCount() ? editTextRight.getLineCount() : editTextLeft.getLineCount();
                    editTextLeft.setMinLines(lines);
                    editTextRight.setMinLines(lines);
                    editTextLeft.setLayoutParams(leftLayoutParams);
                    editTextRight.setLayoutParams(rightLayoutParams);
                }
            }
        });