ImageButton (right-aligned, fixed width) and EditText (left-aligned, remaining space width)

StackOverflow https://stackoverflow.com/questions/16275007

Pregunta

I'm trying to put, in the same row, an ImageButton (right align, fixed width) and an EditText (left align, taken up all the remaining space), but I can't.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="50dp"
    android:orientation="horizontal"
    xmlns:android="http://schemas.android.com/apk/res/android">
<EditText
    android:layout_gravity="center_vertical"
    android:layout_width="fill_parent"
    android:layout_height="40dp"
    android:text="EditText"
    android:textSize="18sp" />
<ImageButton
    android:layout_gravity="center_vertical"
    android:layout_width="40dp"
    android:layout_height="40dp" />
</LinearLayout>

I can't use the property "layout_weight" because I want to fix the width of the ImageButton. If I use "layout_width=fill_parent" in the EditText, the ImageButton disappears.

Could you help me?

Thanks!!!

¿Fue útil?

Solución 2

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="50dp"
    android:orientation="horizontal" >
<EditText
    android:layout_gravity="center_vertical"
    android:layout_width="0dp"
    android:layout_weight="1"
    android:layout_height="40dp"
    android:text="EditText"
    android:textSize="18sp" />
<ImageButton
    android:layout_gravity="center_vertical"
    android:layout_width="40dp"
    android:layout_height="40dp" />
</LinearLayout>

Just i change layout_width of EditText fill_parent to 0dp and set layout_weight="1", so it will stretched and take remaining space.

Check this for LinearLayout weight concept.

Otros consejos

If you can't use layout_weight, use RelativeLayout. In the RelativeLayout first put the ImageButton and align it to right of RelativeLayout. Then put the EditText with layout_width="fill_parent" place it to left of ImageButton

Try this

<RelativeLayout 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <ImageButton 
        android:layout_width="40dp" 
        android:layout_height="40dp"
        android:layout_alignParentRight="true"
        android:id="@+id/id_image_button"/>

    <EditText android:layout_width="fill_parent"
        android:layout_height="40dp"
        android:layout_toLeftOf="@id/id_image_button"/>

</RelativeLayout>

Can you try the following?:

<EditText
android:layout_gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:text="EditText"
android:textSize="18sp"
android:layout_weight="1" />
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top