Eliminación del acolchado izquierdo en un Android EditText
El fondo predeterminado de EditText parece poner ~ 4dp de relleno a la izquierda. Esto causa desalineación con otros widgets.
Hice una aplicación simple para demostrar. Captura de pantalla:
- Android que el diseño a utilizar para desbordar objetos a la siguiente línea
- ClassCastException: android.support.v7.widget.LinearLayoutCompat $ LayoutParams no se puede convertir en android.widget.LinearLayout $ LayoutParams
- Alinear la vista de texto según la selección de idioma (LEFT -RIGHT)
- Cómo extraer el texto del elemento seleccionado en el listView
- La interfaz de usuario del selector de fechas se rompe al azar
Diseño:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context=".MainActivity"> <TextView android:background="#00ff00" android:text="@string/hello_world" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="HINT" android:text="TEXT" android:singleLine="true"/> <TextView android:background="#00ff00" android:text="@string/hello_world" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout>
¿Hay alguna forma de evitar que haga esto?
- ¿Hay una manera de comprobar si androide WindowManager ya contiene una vista?
- Dibujar círculo HSV en tiempo de ejecución
- Menú Opciones de ICS de Android con imágenes
- Problema con la última fila de GridView en Android
- Layout_width y layout_height no se está aplicando a edittext desde el tema
- Botón Añadir positivo a Diálogo
- Android: cómo usar el color del estilo en XML de otro diseño
- StaggeredGridView establece el ancho y la altura para determinada imagen
Una solución es utilizar margen negativo en los lados (-4dp)
Esto sólo funcionará cuando el fondo de EditText coincida con el fondo de Layout y no tenga nada en los lados en el Layout que estaría cubierto por el EditText
¿Has probado a agregar android: background = "@ android: color / transparent"?
Hice el mismo ejemplo con este resultado
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context=".MainActivity"> <TextView android:background="#00ff00" android:text="@string/hello_world" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="HINT" android:text="TEXT" android:background="@android:color/transparent" android:singleLine="true"/> <TextView android:background="#00ff00" android:text="@string/hello_world" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout>
Segunda vía con líneas
Añadir un archivo xml en drawable como este
<?xml version="1.0" encoding="utf-8"?> <layer-list xmlns:android="http://schemas.android.com/apk/res/android"> <item> <shape android:shape="rectangle"> <solid android:color="@android:color/transparent" /> <!--background color of box--> </shape> </item> <item android:top="-2dp" android:right="-2dp" android:left="-2dp"> <shape> <solid android:color="@android:color/transparent" /> <stroke android:width="1dp" android:color="#000000" /> <!-- color of stroke --> </shape> </item> </layer-list>
Entonces en su diseño, agregue esto en su EdiciónTexto android: background = "@ drawable / edittext_background"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context=".MainActivity"> <TextView android:background="#00ff00" android:text="@string/hello_world" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="HINT" android:text="TEXT" android:background="@drawable/edittext_background" android:singleLine="true" android:layout_marginBottom="3dp"/> <TextView android:background="#00ff00" android:text="@string/hello_world" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout>
Y tendrás esto
Cambiar el fondo de EditText a la costumbre, tiene nueve patch padding
Si desea alinear sólo el texto y no la línea inferior, puede sobrescribir el relleno mediante programación.
Así que digamos que este diseño es para una actividad. Una vez creada la actividad, puedes hacer algo como:
findViewById(R.id.edit_text).setPaddding( 0, old_top_paddding, old_right_paddding, old_bottom_padding);
La clave es volver a configurar el relleno una vez que se ha aplicado el fondo. Es posible que tenga que ver los casos en que el fondo se establece de nuevo, así que tal vez una idea es anular los setBackground*()
y volver a configurar el relleno allí.
Utilice android:layout_marginLeft="-4dp"
en el campo de edición de texto de la siguiente manera.
<EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="HINT" android:text="TEXT" android:layout_marginLeft="-4dp" android:singleLine="true"/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <TextView android:background="#00ff00" android:text="@string/hello_world" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="HINT" android:text="TEXT" android:singleLine="true"/> <TextView android:background="#00ff00" android:text="@string/hello_world" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout>
Quitar el relleno del propio LinearLayout 🙂
Captura de pantalla:
- SDK de Google Drive para OCR
- Cómo configurar el orden de las bibliotecas en el classpath para Android Studio?