如何让EditText控件占用更多可用空间?

时间:2013-12-04 03:10:54

标签: android

在以下UI中,我希望btnAddress和btnDelete这两个按钮占用的空间最小, 和editnumber占用最大空间,我该怎么办?需要我使用RelativeLayout布局吗?谢谢!

BTW,以下代码,btnDelete按钮只显示一个部分。

enter image description here

   <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:gravity="left"
        android:orientation="horizontal" >

        <EditText
            android:id="@+id/editnumber"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"            
            android:phoneNumber="true"
            android:ems="10" >
        </EditText>

        <Button
            android:id="@+id/btnAddress"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"           
            android:text="@string/BtnAddressMin" />

        <Button
            android:id="@+id/btnDelete"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"            
            android:text="@string/BtnDeleteMin" />

    </LinearLayout>

3 个答案:

答案 0 :(得分:1)

android:layout_weight="1编辑文本。不需要将其更改为相对布局

       <EditText
        android:id="@+id/editnumber"
        android:layout_width="0dp"//no need to specifing the width if parent layout is horizontal
        android:layout_height="wrap_content"            
        android:phoneNumber="true"
        android:layout_weight="1";>
    </EditText>

执行此操作Edittext将占用最大空间,剩余空间将留给其他View。其他则无需更改xml中的任何内容

答案 1 :(得分:1)

使用EditText填充余下的空格,而不是使用ems固定layout_weight的长度。

<EditText
    android:id="@+id/editnumber"
    android:layout_width="0dp"
    android:layout_height="wrap_content"   
    android:layout_weight="1"         
    android:phoneNumber="true" />

修改:我不确定是否仍然推荐android:phoneNumber="true",但我会将其更改为android:inputType="phone"

答案 2 :(得分:1)

这应该这样做。给编辑编号一个权重1.将其余部分保留原样。

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:gravity="left"
    android:orientation="horizontal" >

    <EditText
        android:id="@+id/editnumber"
        android:layout_width="0dip"
        android:layout_height="wrap_content"  
        android:layout_weight="1"                  
        android:phoneNumber="true"
        android:ems="10" >
    </EditText>

    <Button
        android:id="@+id/btnAddress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/BtnAddressMin" />

    <Button
        android:id="@+id/btnDelete"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/BtnDeleteMin" />

</LinearLayout>
相关问题