开发者

How can I align two EditText views when I do not know the amount of text ahead of time for both views?

开发者 https://www.devze.com 2023-03-12 01:40 出处:网络
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 using android:layout_alignTop and android:layout_alignBottom will

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 using android:layout_alignTop and android:layout_alignBottom will not work. The layout I am experimenting with is below. I have already tried android:layout_alignTop and android:layoutAlignBottom but they do not yield satisfactory results.

Layout 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>

Figure for Layout 1: http://imgur.com/P6KV7.png

Layout 2 with android:layout_alignTop and android:layout_alignBottom

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" 
androi开发者_如何学JAVAd: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>

Figure for Layout 2: http://imgur.com/x6fyI.png

But, if I were to change the text on the first EditText then some of the text in that box gets cut-off. See Layout 3 and Figure 3 below:

Layout 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>

Figure 3 for Layout 3 : (Sorry, cannot post more than two hyperlinks because of rep issues.)

Is there a way to align the heights of EditText views when the amount of text in either view is not known ahead of time?


Well, I have to say, this is the first time I've ever found a TableLayout useful. Note the text LOOKS cut off in the preview (as it's really big), but when you compile and run, you'll see that the text is scrollable. The TableView does all the tricky stuff for you. I think this will work for you.
Just realised that you want the textviews to look the same size also - that's easy - give them a transparent background and make the background of the table have the background. EDIT - One more thing - due to a framework bug, I had to use this:

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

to get the text both scrollable and non-editable. Also, a good point from @mandel - use "fill_parent" not "match_parent" if programming for android devices of an api level less than 8.

<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>
0

精彩评论

暂无评论...
验证码 换一张
取 消