2011-10-26 117 views
7

所以我有这个textview“到”,一个edittext输入联系人和一个搜索按钮,在一行中搜索联系人。 textview和按钮都很好,但问题是编辑文本很小,我希望它占据所有剩余的空间。下面的代码:EditText不会填满剩余的空间

<RelativeLayout 
     android:layout_height="wrap_content" 
     android:layout_width="fill_parent" 
     android:paddingTop="10dp"> 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="To: " 
      android:textColor="#000000" 
      android:paddingTop="10dp" 
      android:layout_toLeftOf="@+id/editText_recipient" 
      android:layout_alignParentLeft="true" 
      android:layout_marginLeft="10dp"/> 
     <EditText 
      android:id="@+id/editText_recipient" 
      android:layout_height="wrap_content" 
      android:layout_width="wrap_content" 
      android:hint="Enter Number" 
      android:inputType="number" 
      android:textSize="12sp" 
      android:layout_toLeftOf="@+id/button_recipient_picker" 
      android:layout_marginLeft="5dp"/> 
     <Button 
      android:id="@+id/button_recipient_picker" 
      android:layout_height="wrap_content" 
      android:layout_width="wrap_content" 
      android:text="Browse .." 
      android:layout_alignParentRight="true" 
      android:layout_marginLeft="5dp" 
      android:layout_marginRight="5dp" 
      android:onClick="doLaunchContactPicker"/> 
     </RelativeLayout> 
+0

回答下面的RelativeLayout – Br0thazS0ul

回答

17
<LinearLayout 
     android:layout_height="wrap_content" 
     android:layout_width="fill_parent" 
     android:paddingTop="10dp"> 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="To: " 
      android:textColor="#000000" 
      android:paddingTop="10dp" 
      android:layout_marginLeft="10dp"/> 
     <EditText 
      android:id="@+id/editText_recipient" 
      android:layout_height="wrap_content" 
      android:layout_width="wrap_content" 
      android:hint="Enter Number" 
      android:inputType="number" 
      android:textSize="12sp" 
      android:layout_marginLeft="5dp" 
      android:layout_weight="1" 
     /> 
     <Button 
      android:id="@+id/button_recipient_picker" 
      android:layout_height="wrap_content" 
      android:layout_width="wrap_content" 
      android:text="Browse .." 
      android:layout_marginLeft="5dp" 
      android:layout_marginRight="5dp" 
      android:onClick="doLaunchContactPicker"/> 
     </LinearLayout> 

使用此布局,我转换布局线性(水平默认值)。并添加了一个layout_weight到editText,它将占用剩余的空间。

+0

谢谢你:D它很棒:) – kev

2

对于那些需要这个答案并解决这个职位而不提供替代方案的人来说,答案相当简单。 您需要告诉每个控件相互之间的位置,并适用于应该占用空间android:layout_width = "match_parent"的控件,即使在一个LinearLayout,这将推动每个其他控件排列在屏幕外,在RelativeLayout它可以很好地工作

<RelativeLayout 
    android:layout_height="wrap_content" 
    android:layout_width="match_parent" 
    android:paddingTop="10dp"> 
    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="To: " 
     android:textColor="#000000" 
     android:paddingTop="10dp" 
     android:layout_toLeftOf="@+id/editText_recipient" 
     android:layout_alignParentLeft="true" 
     android:layout_marginLeft="10dp"/> 
    <EditText 
     android:id="@+id/editText_recipient" 
     android:layout_height="wrap_content" 
     android:layout_width="MATCH_PARENT" 
     android:hint="Enter number" 
     android:inputType="number" 
     android:textSize="12sp" 
     android:layout_toLeftOf="@+id/button_recipient_picker" 
     android:layout_marginLeft="5dp"/> 
    <Button 
     android:id="@+id/button_recipient_picker" 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:text="Browse .." 
     android:layout_alignParentRight="true" 
     android:layout_marginLeft="5dp" 
     android:layout_marginRight="5dp" 
     android:onClick="doLaunchContactPicker"/> 
    </RelativeLayout>