我想在TextInputLayout上为我的EditText应用白色背景。然而,当我这样做时,EditText的输入/提示似乎在底部,我理解这是由于浮动标签的保留空间所致。但是,我希望我的EditText位于中心,并且当浮动标签出现时,它们两者都会居中。有没有办法可以实现这一点?
试试这个:
public static void addTextInputLayoutWatcher(TextInputLayout inputLayout) {
EditText editText = inputLayout.getEditText();
if (editText == null)
return;
float offset = inputLayout.getResources().getDimensionPixelOffset(R.dimen.inputlayout_focus_offset);
editText.setOnFocusChangeListener((v, hasFocus) -> {
float absY = initTag(inputLayout);
if (hasFocus || editText.getText().length() > 0) {
inputLayout.setY(absY + offset);
} else {
inputLayout.setY(absY);
}
});
}
private static float initTag(TextInputLayout inputLayout) {
float absY;
Object tag = inputLayout.getTag();
if (tag == null) {
// Setting for the first time
absY = inputLayout.getY();
inputLayout.setTag(absY);
} else {
absY = (float) tag;
}
return absY;
}