Just like the title, EditText in Android has its own padding. Generally speaking, setting the background to null is enough, but because of the use of an unfamiliar declarative framework, it took a few minutes to solve it, but it took a lot of time. half a day~
Simple requirements, related blog
- Android Advanced Road - Remove EditText inner margin
- The road to Android advancement - EditText input font adaptation
In fact, this is just a very simple small requirement. I didn’t want to but encountered some small problems. I simply spent some time to lay a solid foundation
trigger scene
This scene is exactly a UI effect that I need to adapt in my current development, so I wrote another Demo. After some tests, the summary is recorded here
my thinking
For the overall layout, the following two methods can be used (I tried both when the initial problem occurred, but I didn’t find the key to the problem at the time)
- Linear Vertical Layout + Frame Layout (EditText+TextView)
- Linear vertical layout + linear horizontal layout (EditText+TextView)
After the overall layout plan is finalized, it is mainly for EditText
routine processing;
- It stands to reason that I only need to set
background = null
, but after setting, only the bottom line is removed, and the upper and lower margins still exist; - I
EditText
set a fixed height for , butEditText
the display is incomplete (occluded), and the inner margin still exists and covers the input content
Because I'm using 声明式布局+splitties框架
, I'm wondering if there are special settings in the framework? In order to eliminate the problem, I first tried the same processing method in xml, and found that the expected effect can be achieved; so the background = null
method is feasible, but it is not enough to meet our needs , and then I thought after a sleep: “既然是上下边距的问题,那去除内边距应该可以”
, after trying finally adopted 设置 padding + background
the method
我想现在采用这种布局方式的朋友应该还不是很多,故此留一份伪代码,用于各位了解我所遇场景和解决方式
Regarding troubleshooting, I checked whether the framework has been set internally, but it seems that the theme and the like are not set
Then I thought about looking at the source code of EditText, but the pursuit may not be right, and I will study it when I have time in the future
After searching, some people said that
EditText
the defaultbackground
is oneInsetDrawable
,InsetDrawable
you can search and learn about it by yourself, I will only make a simple record below
common scene
The following scenario is only my personal experience, from the original EditText effect -> remove the original default effect processing method and corresponding scenario
original scene
The original EditText without any settings, by default there will be底部线条+上下内边距
example
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="原始的EditText" />
Set the background scene
By setting EditText , background
you can 去除其默认效果
(margin and bottom horizontal line), I know that there are three common
- null (more commonly used)
- Color value
- drawable (can be a shape or a picture)
example
background = null
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="@null"
android:hint="background 为 null" />
background = color value
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="#00000000"
android:hint="background 为 色值" />
background = drawable
If the background image is not set, the background image height will be used by default
<EditText
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="10dp"
android:background="@drawable/icon_font_bg"
android:hint="background 为 drawable" />
set padding
Because the default effect has upper and lower margins, we can also achieve the basic effect by setting padding
Only set padding
When only padding is set, the default bottom line will not disappear
example
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:hint="padding 为 0"
android:padding="0dp" />
set padding + background
我所遇到的问题,就是通过这种方式达到理想效果的
example
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@null"
android:hint="padding=0 + background=null"
android:padding="0dp" />
demo test
This is the demo I used to test EditText
the display scene, memo here
Effect
xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="原始的EditText" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="@null"
android:hint="background 为 null" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="#00000000"
android:hint="background 为 色值" />
<EditText
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="10dp"
android:background="@drawable/icon_font_bg"
android:hint="background 为 drawable" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:hint="padding 为 0"
android:padding="0dp" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="null"
android:hint="padding=0 + background=null"
android:padding="0dp" />
</androidx.appcompat.widget.LinearLayoutCompat>