我使用TextInputLayout来显示提示,但我无法将其垂直居中。我总是这样:
如果EditText / TextInputEditText中没有任何文字,我想垂直显示提示。我尝试过基本的想法(重力,layout_gravity等)。到目前为止,唯一的方法是添加一些“魔术”填充,但我想以更清洁的方式做到这一点。我正在考虑测量顶部提示标签高度,并在它不可见时将其添加为底部边距,并在可见时删除相同的边距,但我还不太了解TextInputLayout源代码。有人知道怎么做吗?
编辑:
我尝试了这个建议的答案:
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:background="@color/grey_strong">
<android.support.design.widget.TextInputEditText
android:layout_width="match_parent"
android:layout_height="90dp"
android:background="@color/red_light"
android:gravity="center_vertical"
android:hint="Test"/>
</android.support.design.widget.TextInputLayout>
我明白了:
“大”提示仍未垂直居中。它略低于中心,因为“小”提示(在灰色背景中,在顶部,仅在场聚焦时可见)在顶部占据一些空间并推动EditText。
答案 0 :(得分:5)
TextInputLayout
的当前实现似乎无法做到这一点。但是你可以通过使用TextInputEditText
的填充来实现你想要的效果。
假设你有TextInputLayout
和TextInputEditText
这样:
<android.support.design.widget.TextInputLayout
android:id="@+id/text_input_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FAA"
android:hint="Text hint">
<android.support.design.widget.TextInputEditText
android:id="@+id/text_input_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#AAF" />
</android.support.design.widget.TextInputLayout>
正如您所看到的,TextInputLayout
由一个顶部区域组成,用于保存小版本中的提示,一个底部区域用于保存大版本中的提示(以及输入内容)。当视图失去焦点且编辑文本为空时,提示将在蓝色空间内移动。另一方面,当视图获得焦点或编辑文本内部有一些文本时,提示将移动到红色空间。
所以我们想做的是:
TextInputEditText
底部没有焦点和文字时添加额外的填充,此填充等于红色区域高度; TextInputEditText
内置焦点或文字时删除此填充。假设你按照以下方式检索你的观点:
private lateinit var textInputLayout: TextInputLayout
private lateinit var textInputEditText: TextInputEditText
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
...
textInputLayout = view.findViewById(R.id.text_input_layout)
textInputEditText = view.findViewById(R.id.text_input_edit_text)
...
}
以下是一个可用于计算顶部红色空间(以像素为单位)的实现示例。
private fun getTextInputLayoutTopSpace(): Int {
var currentView: View = textInputEditText
var space = 0
do {
space += currentView.top
currentView = currentView.parent as View
} while (currentView.id != textInputLayout.id)
return space
}
然后你可以像这样更新填充:
private fun updateHintPosition(hasFocus: Boolean, hasText: Boolean) {
if (hasFocus || hasText) {
textInputEditText.setPadding(0, 0, 0, 0)
} else {
textInputEditText.setPadding(0, 0, 0, getTextInputLayoutTopSpace())
}
}
现在你必须在两个地方调用这个方法:创建视图时(实际上我们需要等待视图完全测量)和焦点改变时。
textInputLayout.viewTreeObserver.addOnPreDrawListener(object : ViewTreeObserver.OnPreDrawListener {
override fun onPreDraw(): Boolean {
if (textInputLayout.height > 0) {
textInputLayout.viewTreeObserver.removeOnPreDrawListener(this)
updateHintPosition(textInputEditText.hasFocus(), !textInputEditText.text.isNullOrEmpty())
return false
}
return true
}
})
textInputEditText.setOnFocusChangeListener { _, hasFocus ->
updateHintPosition(hasFocus, !textInputEditText.text.isNullOrEmpty())
}
一个问题是TextInputLayout
的高度正在变化,因此所有视图都在移动,并且它看起来并不真正居中。您可以通过将TextInputLayout
置于FrameLayout
内并固定高度并将其垂直居中来解决此问题。
最后,你可以动画所有的东西。更改填充时,您只需使用支持库的TransitionManager
。
您可以在此链接中看到最终结果:https://streamable.com/la9uk
完整的代码如下所示:
布局:
<FrameLayout
android:layout_width="match_parent"
android:layout_height="60dp"> <-- Adapt the height for your needs -->
<android.support.design.widget.TextInputLayout
android:id="@+id/text_input_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:background="#FAA"
android:hint="Text hint">
<android.support.design.widget.TextInputEditText
android:id="@+id/text_input_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#AAF" />
</android.support.design.widget.TextInputLayout>
</FrameLayout>
代码:
private lateinit var textInputLayout: TextInputLayout
private lateinit var textInputEditText: TextInputEditText
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
super.onCreateView(inflater, container, savedInstanceState)
val view = inflater.inflate(R.layout.your_layout, container, false)
textInputLayout = view.findViewById(R.id.text_input_layout)
textInputEditText = view.findViewById(R.id.text_input_edit_text)
textInputLayout.viewTreeObserver.addOnPreDrawListener(object : ViewTreeObserver.OnPreDrawListener {
override fun onPreDraw(): Boolean {
// Wait for the first draw to be sure the view is completely measured
if (textInputLayout.height > 0) {
textInputLayout.viewTreeObserver.removeOnPreDrawListener(this)
updateHintPosition(textInputEditText.hasFocus(), !textInputEditText.text.isNullOrEmpty(), false)
return false
}
return true
}
})
textInputEditText.setOnFocusChangeListener { _, hasFocus ->
updateHintPosition(hasFocus, !textInputEditText.text.isNullOrEmpty(), true)
}
return view
}
private fun updateHintPosition(hasFocus: Boolean, hasText: Boolean, animate: Boolean) {
if (animate) {
TransitionManager.beginDelayedTransition(textInputLayout)
}
if (hasFocus || hasText) {
textInputEditText.setPadding(0, 0, 0, 0)
} else {
textInputEditText.setPadding(0, 0, 0, getTextInputLayoutTopSpace())
}
}
private fun getTextInputLayoutTopSpace(): Int {
var currentView: View = textInputEditText
var space = 0
do {
space += currentView.top
currentView = currentView.parent as View
} while (currentView.id != textInputLayout.id)
return space
}
我希望这能解决你的问题。
答案 1 :(得分:1)
请使用此代码 如果要在Center
中显示EditText提示<android.support.design.widget.TextInputLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
<android.support.design.widget.TextInputEditText
android:layout_width="match_parent"
android:layout_height="90dp"
android:hint="Test"
android:gravity="center" />
如果要在垂直居中显示EditText提示以及左对齐
<android.support.design.widget.TextInputLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
<android.support.design.widget.TextInputEditText
android:layout_width="match_parent"
android:layout_height="90dp"
android:hint="Test"
android:gravity="center_vertical" />
或
<android.support.design.widget.TextInputLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
<android.support.design.widget.TextInputEditText
android:layout_width="match_parent"
android:layout_height="90dp"
android:hint="Test"
android:gravity="center|left" />
答案 2 :(得分:0)
找到解决方案:
<android.support.design.widget.TextInputLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<android.support.design.widget.TextInputEditText
android:layout_width="200dp"
android:layout_height="90dp"
android:hint="Test"
android:gravity="center_vertical" />
90dp作为高度就是一个例子。如果你能提供你的xml,我可以根据你的情况进行调整。您只需设置android:gravity="center_vertical"
,TextInputLayout
应该wrap_content
作为身高
希望有所帮助:)
答案 3 :(得分:0)
当我使用主题"Widget.MaterialComponents.TextInputLayout.FilledBox.Dense"
和密码可见性切换按钮时,我遇到了这个问题。
所以我最终根据这个问题的答案创建了自定义类。
自定义类别:
package com.mycompany
import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.view.ViewTreeObserver
import com.google.android.material.textfield.TextInputEditText
import com.google.android.material.textfield.TextInputLayout
import com.mycompany.R
class CustomTextInputEditText : TextInputEditText {
//region Constructors
constructor(context: Context) : super(context)
constructor(context: Context, attrs: AttributeSet) : super(context, attrs)
constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr)
//endregion
//region LifeCycle
override fun onAttachedToWindow() {
super.onAttachedToWindow()
textInputEditText.setOnFocusChangeListener { _, hasFocus ->
updateHintPosition(hasFocus, !textInputEditText.text.isNullOrEmpty())
}
textInputEditText.viewTreeObserver.addOnPreDrawListener(object : ViewTreeObserver.OnPreDrawListener {
override fun onPreDraw(): Boolean {
if ((textInputLayout?.height ?: 0) > 0) {
textInputLayout?.viewTreeObserver?.removeOnPreDrawListener(this)
updateHintPosition(textInputEditText.hasFocus(), !textInputEditText.text.isNullOrEmpty())
return false
}
return true
}
})
}
//endregion
//region Center hint
private var paddingBottomBackup:Int? = null
private var passwordToggleButtonPaddingBottomBackup:Float? = null
private val textInputEditText: TextInputEditText
get() {
return this
}
private val textInputLayout:TextInputLayout?
get(){
return if (parent is TextInputLayout) (parent as? TextInputLayout) else (parent?.parent as? TextInputLayout)
}
private val passwordToggleButton:View?
get() {
return (parent as? View)?.findViewById(R.id.text_input_password_toggle)
}
private fun updateHintPosition(hasFocus: Boolean, hasText: Boolean) {
if (paddingBottomBackup == null)
paddingBottomBackup = paddingBottom
if (hasFocus || hasText)
textInputEditText.setPadding(paddingLeft, paddingTop, paddingRight, paddingBottomBackup!!)
else
textInputEditText.setPadding(paddingLeft, paddingTop, paddingRight, paddingBottomBackup!! + getTextInputLayoutTopSpace())
val button = passwordToggleButton
if (button != null){
if (passwordToggleButtonPaddingBottomBackup == null)
passwordToggleButtonPaddingBottomBackup = button.translationY
if (hasFocus || hasText)
button.translationY = - getTextInputLayoutTopSpace().toFloat() * 0.50f
else
button.translationY = passwordToggleButtonPaddingBottomBackup!!
}
}
private fun getTextInputLayoutTopSpace(): Int {
var currentView: View = textInputEditText
var space = 0
do {
space += currentView.top
currentView = currentView.parent as View
} while (currentView !is TextInputLayout)
return space
}
//endregion
//region Internal classes
data class Padding(val l: Int, val t: Int, val r: Int, val b: Int)
//endregion
}
用法:
<com.google.android.material.textfield.TextInputLayout
style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox.Dense"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:hint="Password"
app:passwordToggleEnabled="true">
<com.mycompany.CustomTextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword" />
</com.google.android.material.textfield.TextInputLayout>
答案 4 :(得分:0)
也许有点晚了,但是...基于@JFrite答复...您可以创建一个类来改进代码:
class TextInputCenterHelper constructor(val textInputLayout: TextInputLayout, val textInputEditText: TextInputEditText){
init {
textInputLayout.viewTreeObserver.addOnPreDrawListener(object : ViewTreeObserver.OnPreDrawListener {
override fun onPreDraw(): Boolean {
if (textInputLayout.height > 0) {
textInputLayout.viewTreeObserver.removeOnPreDrawListener(this)
updateHintPosition(textInputEditText.hasFocus(), !textInputEditText.text.isNullOrEmpty(), false)
return false
}
return true
}
})
textInputEditText.setOnFocusChangeListener { _, hasFocus ->
updateHintPosition(hasFocus, !textInputEditText.text.isNullOrEmpty(), true)
}
}
private fun updateHintPosition(hasFocus: Boolean, hasText: Boolean, animate: Boolean) {
if (animate) {
TransitionManager.beginDelayedTransition(textInputLayout)
}
if (hasFocus || hasText) {
textInputEditText.setPadding(0, 0, 0, 0)
} else {
textInputEditText.setPadding(0, 0, 0, getTextInputLayoutTopSpace())
}
}
private fun getTextInputLayoutTopSpace(): Int {
var currentView: View = textInputEditText
var space = 0
do {
space += currentView.top
currentView = currentView.parent as View
} while (currentView.id != textInputLayout.id)
return space
}
并使用它:
TextInputCenterHelper(your_text_input_layout, your_text_input_edit_text)
我希望这可以帮助某人!
答案 5 :(得分:0)
问题已于2019年4月5日解决。这是提交的内容: https://github.com/material-components/material-components-android/commit/4476564820ff7a12f94ffa7fc8d9e10221b18eb1
您可以使用已解决错误的最新版本(2020年7月23日)。看一下changelog(“ TextInputLayout”部分): https://github.com/material-components/material-components-android/releases/tag/1.3.0-alpha02
只需更新您的gradle中的库即可:
implementation 'com.google.android.material:material:1.3.0-alpha02'
对我有用。
答案 6 :(得分:0)
我发现此处发布的解决方案存在一些问题,因此增加了一个涵盖基础的问题:
public class CenteredTextInputEditText extends TextInputEditText {
private Integer paddingBottomBackup = null;
// region Constructors
public CenteredTextInputEditText(Context context) {
super(context);
}
public CenteredTextInputEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CenteredTextInputEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
// endregion
// region LifeCycle
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
if (getOnFocusChangeListener() == null) {
setOnFocusChangeListener((v, hasFocus) -> updateHintPosition(hasFocus));
}
getViewTreeObserver()
.addOnPreDrawListener(
new OnPreDrawListener() {
@Override
public boolean onPreDraw() {
if (getHeight() > 0) {
getViewTreeObserver().removeOnPreDrawListener(this);
updateHintPosition(hasFocus());
return false;
}
return true;
}
});
}
// endregion
// region Center hint
private void updateHintPosition(boolean hasFocus) {
boolean hasText = getText() != null && !Strings.isNullOrEmpty(getText().toString());
if (paddingBottomBackup == null) {
paddingBottomBackup = getPaddingBottom();
}
int bottomPadding = paddingBottomBackup;
if (!hasFocus && !hasText) {
bottomPadding += getTextInputTopSpace();
}
setPadding(getPaddingLeft(), getPaddingTop(), getPaddingRight(), bottomPadding);
if (hasFocus) {
KeyboardUtils.openKeyboardFrom(this);
}
}
private int getTextInputTopSpace() {
View currentView = this;
int space = 0;
do {
space += currentView.getTop();
currentView = (View) currentView.getParent();
} while (!(currentView instanceof TextInputLayout));
return space;
}
// endregion
public void addAdditionalFocusListener(Consumer<Boolean> consumer) {
setOnFocusChangeListener(
new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
consumer.accept(hasFocus);
updateHintPosition(hasFocus);
}
});
}
}
答案 7 :(得分:0)
我发现以TextInput Layout为中心的最简单方法是仅在XML中启用SetHelperEnabled或通过在TextInputLayout中以编程方式插入这些属性
app:helperTextEnabled="true"
app:helperText=" "
您还可以通过在内部EditText中添加paddingTop
来进一步调整多余的填充
<androidx.cardview.widget.CardView
android:id="@+id/cardView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="32dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/textView2"
app:layout_constraintTop_toBottomOf="@+id/textViewSeekModText">
<com.google.android.material.textfield.TextInputLayout
android:layout_width="409dp"
android:layout_height="wrap_content"
android:background="#FDFDFD"
app:helperText=" "
app:helperTextEnabled="true">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@null"
android:hint="hint" />
</com.google.android.material.textfield.TextInputLayout>
</androidx.cardview.widget.CardView>
答案 8 :(得分:0)
只需在editText或其后代中添加paddingVertical
答案 9 :(得分:0)
这是我们如何实现
要使提示居中,请使用 app:boxBackgroundMode="filled"
如果要删除编辑文本底线,请使用
app:boxStrokeWidth="0dp"
app:boxStrokeWidthFocused="0dp"
app:boxStrokeColor="@color/white"
app:boxBackgroundColor="@color/white"
完整代码
<com.google.android.material.textfield.TextInputLayout
app:boxBackgroundMode="filled"
app:boxStrokeWidth="0dp"
app:boxStrokeWidthFocused="0dp"
app:boxStrokeColor="@color/white"
app:boxBackgroundColor="@color/white"
android:id="@+id/layout_main"
android:paddingVertical="@dimen/dp_6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="@dimen/dp_4"
android:hint="@string/your_hint"
android:paddingHorizontal="@dimen/dp_16"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/ed_txt"
android:inputType="textNoSuggestions"
android:layout_width="match_parent"
android:layout_height="?listPreferredItemHeightSmall"
android:gravity="bottom"
android:paddingVertical="@dimen/dp_8" />
</com.google.android.material.textfield.TextInputLayout>
答案 10 :(得分:-1)
我知道这不是最好的解决方案,但它有效。您只需要向“TextInputEditText”添加填充:
android:paddingTop="18dp"
android:paddingBottom="18dp"
完整示例:
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/inputLoginEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:layout_marginEnd="15dp"
android:layout_marginTop="5dp"
android:backgroundTint="@color/light_blue_background"
android:textColor="@color/blue_button"
app:hintEnabled="false"
android:layout_gravity="center"
app:boxStrokeWidth="0dp"
app:boxStrokeWidthFocused="0dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textLabelEmail"
app:passwordToggleEnabled="false">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/etLoginEmail"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:backgroundTint="@color/light_blue_background"
android:hint="@string/hint_email"
android:inputType="textEmailAddress"
android:paddingTop="18dp"
android:paddingBottom="18dp"
android:maxLines="1"
android:textColor="@color/blue_button" />
</com.google.android.material.textfield.TextInputLayout>
它对我来说非常好。