2016-05-18 54 views
0

我试图附加显示按钮EDITTEXT如图所示,在下面的代码片段附上显示按钮来编辑文本字段的Android

<EditText 
     android:id="@+id/phone" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:inputType="phone" 
     android:maxLength="11" 
     android:layout_below="@+id/TextView01" 
     android:layout_centerHorizontal="true" 
     android:ems="10"/> 

    <Button 
     android:id="@+id/register" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignRight="@+id/phone" 
     android:layout_below="@+id/phone" 
     android:text="Show" /> 

这是什么,我想实现

插图

enter image description here

有没有一种方法可以将按钮立即贴近编辑区?

+0

创建一个自定义绘制,并将其分配到两个.. – Mohit

+0

水平的LinearLayout配售两个项目一起会保持他们两个彼此相邻(除非有任何填充或利润)。 –

回答

3

您的按钮,创建的EditText绘制XML并将其设置为背景... 乘坐LinearLayout与取向水平和weightsum == 1,把无论是在它

button_border.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle"> 
    <solid android:color="@android:color/darker_gray"/> 
    <stroke 
     android:width="1px" 
     android:color="@android:color/darker_gray"/> 
</shape> 

edit_border.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle"> 
    <solid android:color="@android:color/white"/> 
    <stroke 
     android:width="1px" 
     android:color="@android:color/darker_gray"/> 
</shape> 

main_layout.xml

<LinearLayout 
    android:id="@+id/ll" 
    android:layout_width="match_parent" 
    android:layout_height="50dp" 
    android:orientation="horizontal" 
    android:weightSum="1" > 

    <EditText 
     android:id="@+id/et" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight=".8" 
     android:text="aaa" 
     android:background="@drawable/edit_border" /> 

    <Button 
     android:id="@+id/bt" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight=".2" 
     android:text="Send" 
     android:background="@drawable/button_border" /> 
</LinearLayout> 
+0

我正在使用相对布局方向。 – Blaze

+0

是否可以实现这一点在你的相对布局,它会工作...我的父母布局是相对只有..你需要分配ID线性布局也...看到答案 – Mohit

+0

的变化,是的不使用' '如果你不需要两个空间 – Mohit

0

我们使用android:layout_weight =“1”以及android:layout_width =“0dp”来创建一个灵活的控件。根据不同的尺寸比例调整重量数量。

之后,我们在两个控件上使用android:layout_margin,这样每个结果的加权大小都是相等的。

<LinearLayout 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:orientation="horizontal" > 

<EditText 
    android:layout_width="0dp" 
    android:layout_weight="1" 
    android:layout_height="wrap_content" 
    android:layout_marginRight="8dp" /> 

<Button 
    android:id="@+id/btn_search" 
    android:layout_width="0dp" 
    android:layout_weight="1" 
    android:layout_height="wrap_content" 
    android:text="Search" 
    android:layout_marginLeft="8dp" /> 
</LinearLayout> 
相关问题