自定义 TextInputLayout class,在布局文件中使用时的额外上边距
Custom TextInputLayout class, extra top margin when used in layout files
创建自定义 TextInputLayout
视图 class 时,自定义 class 的布局文件看起来很正常,但在 activity/fragment 布局文件中使用时,视图放入一些额外的最高保证金,我不明白为什么。
这是自定义视图布局文件:
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.textfield.TextInputLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text_input_layout"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="My Custom TextInputLayout">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/edit_text_date"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</com.google.android.material.textfield.TextInputLayout>
当我检查这个自定义布局文件的设计预览时,它看起来很正常,只有很小的浮动提示边距:
下面是自定义视图class(我在class中使用了ViewBinding,虽然我认为这与问题无关,但它仍然出现常规inflate()
):
class CustomTextInputLayout(context: Context, attrs: AttributeSet): TextInputLayout(context, attrs) {
val binding: CustomTextInputLayoutBinding = CustomTextInputLayoutBinding.inflate(LayoutInflater.from(context), this, true)
}
所有设置完成后,我在 Activity 布局中使用它,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".MainActivity">
<com.vinnorman.customtextfieldtesting.CustomTextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
但是,当我 运行 应用程序并在 activity_main.xml 设计预览中看到它时,它的上边距要大得多:
关于为什么会有这个额外的顶部边距,以及如何摆脱它,有什么想法吗?
如果您在布局检查器中查看您的布局,您会看到您有一个 TextInputLayout 嵌入在另一个 TextInputLayout 中。这是因为您的自定义视图本身就是一个 TextInputLayout,并且您将布局中的另一个 TextInputLayout 膨胀到其中。您在 Android Studio 设计器中看不到它,但只在 device/emulator.
上看到
解决这个问题的方法是使用 merge
标签。有关详细信息,请参阅 Use the <merge> tag。
创建自定义 TextInputLayout
视图 class 时,自定义 class 的布局文件看起来很正常,但在 activity/fragment 布局文件中使用时,视图放入一些额外的最高保证金,我不明白为什么。
这是自定义视图布局文件:
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.textfield.TextInputLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text_input_layout"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="My Custom TextInputLayout">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/edit_text_date"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</com.google.android.material.textfield.TextInputLayout>
当我检查这个自定义布局文件的设计预览时,它看起来很正常,只有很小的浮动提示边距:
下面是自定义视图class(我在class中使用了ViewBinding,虽然我认为这与问题无关,但它仍然出现常规inflate()
):
class CustomTextInputLayout(context: Context, attrs: AttributeSet): TextInputLayout(context, attrs) {
val binding: CustomTextInputLayoutBinding = CustomTextInputLayoutBinding.inflate(LayoutInflater.from(context), this, true)
}
所有设置完成后,我在 Activity 布局中使用它,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".MainActivity">
<com.vinnorman.customtextfieldtesting.CustomTextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
但是,当我 运行 应用程序并在 activity_main.xml 设计预览中看到它时,它的上边距要大得多:
关于为什么会有这个额外的顶部边距,以及如何摆脱它,有什么想法吗?
如果您在布局检查器中查看您的布局,您会看到您有一个 TextInputLayout 嵌入在另一个 TextInputLayout 中。这是因为您的自定义视图本身就是一个 TextInputLayout,并且您将布局中的另一个 TextInputLayout 膨胀到其中。您在 Android Studio 设计器中看不到它,但只在 device/emulator.
上看到解决这个问题的方法是使用 merge
标签。有关详细信息,请参阅 Use the <merge> tag。