简体   繁体   中英

How to create an EditText of dynamic fixed width

I'm working on an app where my xml looks a bit like this:

<TableRow
    android:layout_width="match_parent"
    android:layout_height="fill_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/receiver_name"
        android:layout_weight="1" />
    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        android:maxLength="30"
        android:maxLines="1" />

</TableRow>

What I want, is that no matter what device you have, the TextView should take up 1/4th of the width, and the EditText should take up 3/4ths of the width.

As it is now, the code works - until you start typing. The EditText gets wider and wider as you type more and more into it. How do I make it so that my EditText stays the same width that is assigned to it by the layout_weight parameters?

you should use LinearLayout like that :

<LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent">

<TextView
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:text="@string/receiver_name"
    android:layout_weight="1" />
<EditText
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="3"
    android:maxLength="30"
    android:maxLines="1" />

</LinearLayout>

hope that can help you =)

Just like Janoub said you could use the LinearLayout and weights value. http://developer.android.com/guide/topics/ui/layout/linear.html But you can also use table layout with 4 columns so your edittext catches 3 columns and the textview 1. http://www.compiletimeerror.com/2013/07/android-tablelayout-example.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM