android - 当我事先不知道两个 View 的文本量时,如何对齐两个 EditText View ?

标签 android alignment android-edittext

我有两个 EditText View ,我希望对齐它们的高度。问题是我无法预测哪个 View 会有更多文本,因此使用 android:layout_alignTopandroid:layout_alignBottom 将不起作用。我正在试验的布局如下。我已经尝试过 android:layout_alignTopandroid:layoutAlignBottom 但它们没有产生令人满意的结果。

布局一

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" 
android:layout_height="fill_parent">

<EditText
        android:id="@+id/text1" 
        android:layout_width="200dip"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"           
        android:padding="5dip"

        android:text="Text 1 bvhg hbjbj kjkbkj kjnjkn hlihj lklkkl"
        />

<EditText
        android:id="@+id/text2" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/text1"           
        android:padding="5dip"

        android:text="Text 2 lasndklsa lkmsclslk klmsldmlk lksdl"
        />


</RelativeLayout>

布局 1 图:http://imgur.com/P6KV7.png

带有 android:layout_alignTopandroid:layout_alignBottom 的布局 2

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" 
android:layout_height="fill_parent">

<EditText
        android:id="@+id/text1" 
        android:layout_width="200dip"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"   
        android:layout_alignTop="@+id/text2"
        android:layout_alignBottom="@+id/text2"

        android:padding="5dip"

        android:text="Text 1 bvhg hbjbj kjkbkj kjnjkn hlihj lklkkl"
        />

<EditText
        android:id="@+id/text2" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/text1"           
        android:padding="5dip"

        android:text="Text 2 lasndklsa lkmsclslk klmsldmlk lksdl"
        />


</RelativeLayout>

布局 2 图:http://imgur.com/x6fyI.png

但是,如果我要更改第一个 EditText 上的文本,那么该框中的某些文本会被截断。请参见下面的布局 3 和图 3:

布局三

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" 
android:layout_height="fill_parent">

<EditText
        android:id="@+id/text1" 
        android:layout_width="200dip"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"   
        android:layout_alignTop="@+id/text2"
        android:layout_alignBottom="@+id/text2"

        android:padding="5dip"

        android:text="Text 1 bvhg hbjbj kjkbkj kjnjkn hlihj lklkkl
                    1 bvhg hbjbj kjkbkj kjnjkn hlihj lklkkl    ncksjcksn
                    lkslksldcsdc
                    the end"
        />

<EditText
        android:id="@+id/text2" 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/text1"           
        android:padding="5dip"

        android:text="Text 2 lasndklsa lkmsclslk klmsldmlk lksdl"
        />


</RelativeLayout>

布局 3 的图 3:(抱歉,由于代表问题,不能发布两个以上的超链接。)

当事先不知道任一 View 中的文本量时,是否有一种方法可以对齐 EditText View 的高度?

最佳答案

好吧,我不得不说,这是我第一次发现 TableLayout 很有用。请注意预览中的文本看起来被截断了(因为它真的很大),但是当您编译并运行时,您会看到该文本是可滚动的。 TableView 为您完成所有棘手的事情。我认为这对你有用。
刚刚意识到您希望 textviews 看起来也一样大小 - 这很容易 - 给它们一个透明的背景并使表格的背景具有背景。 编辑 - 还有一件事 - 由于框架错误,我不得不使用它:

android:inputType="none"
android:editable="false"

使文本既可滚动又不可编辑。另外,@mandel 的一个好观点 - 如果为 api 级别低于 8 的 android 设备编程,请使用“fill_parent”而不是“match_parent”。

<TableLayout
    android:id="@+id/tableLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <TableRow
        android:layout_height="fill_parent"
        android:layout_width="wrap_content"
        android:id="@+id/tableRow1"
        android:gravity="center_vertical">
        <EditText
            android:inputType="none"
            android:editable="false"
            android:text="I have two EditText to align. The problem is that I cannot predict which view will have more tetText to align. The problem is that I cannot predict which view will have more text and hencem  I have two EditText views whose height I wish to align. The problem is that I cannot predict which view will have more text and hence"
            android:id="@+id/editText1"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent">
        </EditText>
        <EditText
            android:inputType="none"
            android:editable="false"
            android:text="I have two EditText views whose height I wish to alignhave more text and hence "
            android:id="@+id/editText2"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"></EditText>
    </TableRow>
</TableLayout>

关于android - 当我事先不知道两个 View 的文本量时,如何对齐两个 EditText View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6301323/

相关文章:

css - 通过 CSS 在图像上水平居中文本

java - 有没有办法在 RecyclerView 下添加按钮或任何内容

java - 如何停止 HttpURLConnection.getInputStream()?

Android Studio IDE 奇怪的行为(图形错误?)

android - 在 map Activity 中缩放和平移到纬度/经度跨度

css - 如何使用 CSS 对齐一个 div 容器内的 2 个 div(见图)

html - 无法居中

java - Android:每秒自动获取下一个列表项

android - 使用EditText完成后是否更新数据库中的值?

android - EditText 请求焦点不起作用