Удаление левого дополнения на Android EditText
По умолчанию фон EditText по умолчанию помещает ~ 4dp отступов слева. Это вызывает несоосность с другими виджетами.
Я продемонстрировал простое приложение. Снимок экрана:
![Левое дополнение на EditText]()
Разметка:
<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>
Есть ли способ остановить это?
Ответы
Ответ 1
Одним из способов решения проблемы является использование отрицательного поля по сторонам (-4dp)
Это будет работать только тогда, когда фон EditText соответствует фону Layout, и у вас нет ничего по сторонам в макете, которое было бы покрыто EditText
Ответ 2
Вы пытались добавить android: background = "@android: color/transparent"?
Я сделал тот же пример с этим результатом
<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>
![введите описание изображения здесь]()
Второй способ с линиями
Добавьте xml файл, который можно сделать таким, как
<?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>
то в вашем макете добавьте это в свой EditText 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>
и у вас будет это
![введите описание изображения здесь]()
Ответ 3
Измените фон EditText на пользовательский, у него есть дополнение с девятью патчами
Ответ 4
Если вы хотите выровнять только текст, а не строку под ним, вы можете программно переписать прокладку.
Итак, скажем, этот макет для активности. После того, как действие было создано, вы можете сделать что-то вроде:
findViewById(R.id.edit_text).setPaddding(
0,
old_top_paddding,
old_right_paddding,
old_bottom_padding);
Ключ состоит в том, чтобы повторно установить отступы после того, как фон был применен. Вам может потребоваться наблюдать за случаями, когда фон установлен снова, поэтому, возможно, одна идея - переопределить методы setBackground*()
и повторно установить там дополнение.
Ответ 5
Используйте android:layout_marginLeft="-4dp"
в текстовом поле редактирования следующим образом.
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="HINT"
android:text="TEXT"
android:layout_marginLeft="-4dp"
android:singleLine="true"/>