当我不知道两个视图的文本量时如何对齐?

问题描述

| 我有两个要对齐其高度的EditText视图。问题是我无法预测哪个视图将包含更多文本,因此使用
android:layout_alignTop
android:layout_alignBottom
将不起作用。我正在尝试的布局如下。我已经尝试过
android:layout_alignTop
android:layoutAlignBottom
,但结果却不令人满意。 版式1
<?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_alignTop
android: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: 版式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:(很抱歉,由于rep问题,不能发布两个以上的超链接。) 当提前不知道任一视图中的文本量时,是否可以对齐EditText视图的高度?     

解决方法

        好吧,我不得不说,这是我第一次发现TableLayout有用。请注意,文本“ LOOKS”在预览中被截断了(因为它确实很大),但是在编译和运行时,您会看到该文本是可滚动的。 TableView为您完成所有棘手的工作。我认为这对您有用。 刚刚意识到您希望文本视图也看起来相同大小-这很容易-给它们一个透明的背景并使表格的背景具有背景。 编辑-还有一件事-由于框架错误,我不得不使用它:
android:inputType=\"none\"
android:editable=\"false\"
使文本可滚动和不可编辑。另外,@ mandel的一个好处是-如果为api级以下的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>