EditText无法填满剩余空间。

9

我有一个文本视图“ To”,一个用于输入联系人的编辑框和一个搜索按钮,它们都在同一行。文本视图和按钮位置良好,但问题是编辑框太小了,我希望它占据所有剩余的空间。以下是代码:

<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>

请在下面回答一个RelativeLayout。 - Mouhamed Ndiaye
2个回答

19
<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>

使用这个布局,我将您的布局转换为线性布局(默认为水平方向),并在editText上添加了一个layout_weight属性,它将占据剩余的空间。


4

如果你需要回答这个问题并在不提供替代方案的情况下解决这个帖子,答案是相当简单的。

你需要告诉每个控件根据彼此的位置进行定位,并应用于应该占用可用空间的控件,即使在LinearLayout中,这也会将每个其他对齐方式的控件推出屏幕,在RelativeLayout中它可以完美地工作。

android:layout_width = "match_parent"

<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>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接