【发布时间】:2016-09-10 23:49:11
【问题描述】:
如何将 TextInputLayout 错误水平居中?
我遇到了一个建议使用反射来引用错误文本视图的 SO 帖子。我想避免使用反射。
有什么方法可以做到这一点?
【问题讨论】:
-
你找到解决办法了吗?
标签: android android-design-library android-textinputlayout
如何将 TextInputLayout 错误水平居中?
我遇到了一个建议使用反射来引用错误文本视图的 SO 帖子。我想避免使用反射。
有什么方法可以做到这一点?
【问题讨论】:
标签: android android-design-library android-textinputlayout
您创建了一个继承自 TextInputLayout 的自己的类。然后重写 ShowError(string text, Drawable icon) 方法。如果调用错误,则将 textView 与错误居中。
public class TextInputLayout_Center : TextInputLayout
{
public override void ShowError(string text, Android.Graphics.Drawables.Drawable icon)
{
base.ShowError(text, icon);
centerErrorMessage(this);
}
void centerErrorMessage(ViewGroup view)
{
for (int i = 0; i < view.ChildCount; i++)
{
View v = view.GetChildAt(i);
if (v.GetType() == typeof(TextView))
{
v.LayoutParameters = new LayoutParams(ViewGroup.LayoutParams.MatchParent, v.LayoutParameters.Height);
((TextView)v).Gravity = GravityFlags.CenterHorizontal;
((TextView)v).TextAlignment = TextAlignment.Center;
}
if (v is ViewGroup)
{
centerErrorMessage((ViewGroup)v);
}
}
}
}
【讨论】: