开发者

LinearLayout Line up

I have a EditText and I want a ImageButton to the left of it. I want my EditText's width to fill_parent except leave room for the ImageButton. Right now the EditText isn't leaving any room. I have them nested in a LinearLayout.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
 开发者_JS百科   xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <LinearLayout 
        android:layout_height="wrap_content" 
        android:id="@+id/linearLayout1" 
        android:layout_width="fill_parent" 
        android:orientation="horizontal" 
        >
        <EditText 
            android:id="@+id/editText1" 
            android:layout_marginLeft="10dp" 
            android:layout_height="wrap_content" 
            android:layout_marginRight="10dp" 
            android:layout_width="fill_parent"
        />
        <ImageButton 
            android:id="@+id/imageButton1" 
            android:layout_height="wrap_content" 
            android:src="@drawable/icon" 
            android:layout_width="wrap_content"
        />
    </LinearLayout>

...

</LinearLayout>


Change android:layout_width="0dp" and add android:layout_weight="1" to your EditText.


The RelativeLayout can be used very easily to achieve this:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  >
    <EditText 
        android:id="@+id/editText1" 
        android:layout_marginLeft="10dp" 
        android:layout_height="wrap_content" 
        android:layout_marginRight="10dp" 
        android:layout_width="fill_parent"
    />

    <ImageButton 
        android:layout_toRightOf="@+id/editText1"
        android:id="@+id/imageButton1" 
        android:layout_height="wrap_content" 
        android:src="@drawable/icon" 
        android:layout_width="wrap_content"
    />

</RelativeLayout>

Works much cleaner than embedding layout into layout.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜