This seems straightforward, but I can't figure out how to do it. I have a horizontal layout with an EditText and two ImageButtons. I want the ImageButtons to be of a fixed size, and the EditText to take up the remaining space in the layout. How can this be accomplished?
<LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content"> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content"> </EditText> <ImageButton android:src="@drawable/img1" android:layout_width="50dip" android:layout_height="50dip"> </ImageButton> <ImageButton android:src="@drawable/img2" android:layout_width="50dip" android:layout_height="50dip"> </ImageButton> </LinearLayout>
android:layout_marginTop XML attribute is used to specify extra space on the top side of this view. Thus android:layout_marginTop="10dp" on 2nd textView is specifying 10 dp of space between it and above view.
If you want to keep the layout the same (ie, buttons after the text), use the layout_weight
property, along with fill_parent
, thus:
<LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content"> <EditText android:layout_weight="1" android:layout_width="fill_parent" android:layout_height="wrap_content"> </EditText> <ImageButton android:src="@drawable/img1" android:layout_width="50dip" android:layout_height="50dip"> </ImageButton> <ImageButton android:src="@drawable/img2" android:layout_width="50dip" android:layout_height="50dip"> </ImageButton> </LinearLayout>
Giving the EditText
'weight' makes it get figured out last and gives precedence to the buttons. Play with the different layout_weights and see how much fun you can have!
try this in relative layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content"> <ImageButton android:id="@+id/btn1" android:src="@drawable/icon" android:layout_width="50dip" android:layout_height="50dip" android:layout_alignParentRight="true"/> <ImageButton android:id="@+id/btn2" android:src="@drawable/icon" android:layout_width="50dip" android:layout_height="50dip" android:layout_toLeftOf="@+id/btn1"/> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_toLeftOf="@+id/btn2"> </EditText> </RelativeLayout>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With