我在TextInputLayout中创建了一个EditText.我在运行时在我的代码中将drawableLeft设置为EditText,但是一旦我添加了drawableLeft,TextInputLayout中的浮动提示就会向右移动,使空间等于drawable宽度.但我不希望这个空间暗示,所以帮我解决这个问题!
Mik*_* M. 35
TextInputLayout使用辅助类 - CollapsingTextHelper来操纵其提示文本.这个帮助器的实例是私有的,并且没有公开与其布局相关的任何属性,因此我们需要使用一点反射来访问它.此外,每次TextInputLayout布局时都会设置和重新计算其属性,因此对子类TextInputLayout,覆盖其onLayout()方法以及在那里进行调整是有意义的.
import android.content.Context;
import android.graphics.Rect;
import android.support.design.widget.TextInputLayout;
import android.util.AttributeSet;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class CustomTextInputLayout extends TextInputLayout {
private Object collapsingTextHelper;
private Rect bounds;
private Method recalculateMethod;
public CustomTextInputLayout(Context context) {
this(context, null);
}
public CustomTextInputLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CustomTextInputLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
adjustBounds();
}
private void init() {
try {
Field cthField = TextInputLayout.class.getDeclaredField("mCollapsingTextHelper");
cthField.setAccessible(true);
collapsingTextHelper = cthField.get(this);
Field boundsField = collapsingTextHelper.getClass().getDeclaredField("mCollapsedBounds");
boundsField.setAccessible(true);
bounds = (Rect) boundsField.get(collapsingTextHelper);
recalculateMethod = collapsingTextHelper.getClass().getDeclaredMethod("recalculate");
}
catch (NoSuchFieldException | IllegalAccessException | NoSuchMethodException e) {
collapsingTextHelper = null;
bounds = null;
recalculateMethod = null;
e.printStackTrace();
}
}
private void adjustBounds() {
if (collapsingTextHelper == null) {
return;
}
try {
bounds.left = getEditText().getLeft() + getEditText().getPaddingLeft();
recalculateMethod.invoke(collapsingTextHelper);
}
catch (InvocationTargetException | IllegalAccessException | IllegalArgumentException e) {
e.printStackTrace();
}
}
}
Run Code Online (Sandbox Code Playgroud)
这个自定义类是常规类的替代品TextInputLayout,你可以用同样的方式使用它.例如:
<com.mycompany.myapp.CustomTextInputLayout
android:id="@+id/text_input_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Model (Example i10, Swift, etc.)"
app:hintTextAppearance="@style/TextLabel">
<android.support.design.widget.TextInputEditText
android:id="@+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableLeft="@drawable/bmw"
android:text="M Series" />
</com.mycompany.myapp.CustomTextInputLayout>
Run Code Online (Sandbox Code Playgroud)
笔记:
在移动到Material Components库时,辅助类和边界的字段名称已删除m前缀表示法.正如在评论中指出,他们现在命名collapsingTextHelper和collapsedBounds分别.
从API级别28(Pie)开始,非SDK接口(包括反射)存在某些限制,以访问SDK中通常无法访问的成员.但是,各种可用文档似乎表明不禁止对您自己的包中的组件进行反射.由于支持库不是平台SDK的一部分,并且在构建时合并到您的包中,因此该解决方案应该仍然有效.实际上,最近的测试没有发现问题,这仍然可以在可用的Pie仿真器上运行.