【问题标题】:Space to right and left inside EditTextEditText 内左右两边的空格
【发布时间】:2014-07-09 20:25:48
【问题描述】:

我正在尝试创建一个EditText,里面有 2 个按钮(左右)。我想在每一边都给它一个填充,这样文本就不会出现在按钮后面。但是我不能。我试过padding, margin...但对我没有用。可能是什么错误?

                   <LinearLayout
                        android:id="@+id/buscar_linear"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="4dp"
                        android:orientation="horizontal"
                        android:visibility="gone" >

                        <FrameLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content" >

                            <EditText
                                android:id="@+id/buscar_monumento"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:ems="15"
                                android:gravity="center_horizontal"
                                android:hint="Buscar..."
                                android:imeOptions="actionDone"
                                android:inputType="text"
                                android:paddingLeft="40dp"
                                android:paddingRight="40dp"
                                android:textColor="@color/negro"
                                android:textStyle="bold" >
                            </EditText>

                            <Button
                                android:id="@+id/buscar_lupa"
                                android:layout_width="22dp"
                                android:layout_height="22dp"
                                android:layout_gravity="left|center_vertical"
                                android:layout_marginLeft="7dp"
                                android:background="@drawable/buscar_lupa" />

                            <Button
                                android:id="@+id/buscar_salir"
                                android:layout_width="25dp"
                                android:layout_height="25dp"
                                android:layout_gravity="right|center_vertical"
                                android:layout_marginRight="7dp"
                                android:background="@drawable/buscar_salir" />
                        </FrameLayout>
                    </LinearLayout>

新代码

                  <LinearLayout
                        android:id="@+id/buscar_linear"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="4dp"
                        android:orientation="horizontal"
                        android:visibility="gone" >

                            <EditText
                                android:id="@+id/buscar_monumento"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:ems="15"
                                android:gravity="center_horizontal"
                                android:hint="Buscar..."
                                android:imeOptions="actionDone"
                                android:inputType="text"
                                android:paddingLeft="40dp"
                                android:paddingRight="40dp"
                                android:textColor="@color/negro"
                                android:textStyle="bold" >
                            </EditText>

                            <Button
                                android:id="@+id/buscar_lupa"
                                android:layout_width="22dp"
                                android:layout_height="22dp"
                                android:layout_gravity="left|center_vertical"
                                android:layout_marginLeft="7dp"
                                android:background="@drawable/buscar_lupa" />

                            <Button
                                android:id="@+id/buscar_salir"
                                android:layout_width="25dp"
                                android:layout_height="25dp"
                                android:layout_gravity="right|center_vertical"
                                android:layout_marginRight="7dp"
                                android:background="@drawable/buscar_salir" />

                    </LinearLayout>

【问题讨论】:

  • 首先,Framelayout 应该只承载一个 View。其次,您没有将 2 个按钮放在 EditText 内。
  • 只需删除 FrameLayout。
  • 我正在尝试不使用 Framelayout。但是按钮消失了

标签: android android-edittext padding


【解决方案1】:

不要使用 FrameLayout,而是使用 RelativeLayout
framelayout 最好用于单个项目,如果您坚持使用 FrameLayout

,则最好将 LinearLayout 包裹在 Button 和 TextView 周围 或者更好的是,将父视图组 LinearLayout 更改为 RelativeLayout,否则 ADT 将发出有关无用视图组的警告

【讨论】:

  • 我正在尝试不使用 Framelayout。但是按钮消失了 android:layout_width="wrap_parent" 在 EditText
  • 你确定 framelayout 是针对单个项目的吗?你从哪里弄来的?
  • 将 EditText 中的“width”编辑为“wrap_content”而不是“match_parent”
  • 我已更改,但对我不起作用。代码更新
  • 填充不存在
【解决方案2】:

如果你只需要编辑文本两侧的两个按钮,最简单的方法就是这样

<LinearLayout
        android:id="@+id/buscar_linear"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="4dp"
        android:orientation="horizontal">
        <Button android:background="@drawable/ic_launcher"
            android:id="@+id/buscar_lupa"
            android:layout_width="22dp"
            android:layout_height="22dp"
            android:layout_gravity="left|center_vertical"
            android:layout_marginLeft="7dp"/>

            <EditText
                android:id="@+id/buscar_monumento"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:ems="15"
                android:gravity="center_horizontal"
                android:hint="Buscar..sdsdsdsds."
                android:text="1234567890123456789012345678901234567890"
                android:imeOptions="actionDone"
                android:inputType="text"
                android:textColor="#f00"
                android:textStyle="bold" >
            </EditText>



            <Button android:background="@drawable/ic_launcher"
                android:id="@+id/buscar_salir"
                android:layout_width="25dp"
                android:layout_height="25dp"
                android:layout_gravity="right|center_vertical"
                android:layout_marginRight="7dp"/>

    </LinearLayout>

【讨论】:

  • 此代码会将按钮置于编辑文本之外。我想显示里面的按钮
  • 你不能在editText中放置任何视图。但是你可以让它看起来像里面,在不同的屏幕配置、设备主题、editText/Button背景图像中可能会也可能不会。或者您可以使用精心设计的背景图像设置像上面这样的视图组。
【解决方案3】:

首先,在您的新代码段中,您将父线性布局可见性设置为消失

其次使用相对布局很简单

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<EditText
    android:id="@+id/buscar_monumento"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="15"
    android:hint="Buscar..."
    android:imeOptions="actionDone"
    android:inputType="text"
    android:paddingLeft="40dp"
    android:paddingRight="40dp"
    android:textColor="@color/negro"
    android:textStyle="bold" >
</EditText>

<Button
    android:id="@+id/buscar_lupa"
    android:layout_width="22dp"
    android:layout_height="22dp"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:background="@drawable/buscar_lupa" />

<Button
    android:id="@+id/buscar_salir"
    android:layout_width="25dp"
    android:layout_height="25dp"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:layout_marginRight="7dp"
    android:background="@drawable/buscar_salir" />

【讨论】:

    猜你喜欢
    • 2018-12-27
    • 2015-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-14
    • 2016-09-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多