TextInputLayout没有显示时,视图中添加编程 [英] TextInputLayout not showing when View added programmatically

查看:355
本文介绍了TextInputLayout没有显示时,视图中添加编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到的 TextInputLayout 一些奇怪的行为:

I noticed some strange behaviour of TextInputLayout:

在我以下内容添加到我的布局:

When I add the following to my layout:

    <android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <EditText
            android:id="@+id/txtFirstName"
            style="@style/EditTextStyle"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:hint="In layout"
            android:singleLine="true" />
    </android.support.design.widget.TextInputLayout>

一切正常。

当我吹了类似的布局,如:

When I inflate a similar Layout like:

    View v = LayoutInflater.from(this).inflate(R.layout.edittext_w_surrounding_textinputlayout, null);
    EditText editText = (EditText) v.findViewById(R.id.editText);
    editText.setHint("Added programmatically");

    ViewGroup root = (ViewGroup) findViewById(R.id.root);
    root.addView(v);

TextInputLayout 不出现,的EditText 行为的标准方式。

the TextInputLayout doesn't appear and the EditText behaves the standard way.

任何想法是什么原因可能是?

Any ideas what the reason could be?

推荐答案

您应该改变的暗示,而不是的EditText,但TextInputLayout。所以这将是:

You should change hint, not on EditText, but on TextInputLayout. So it will be:

TextInputLayout v = (TextInputLayout) LayoutInflater.from(this).inflate(R.layout.edittext_w_surrounding_textinputlayout, null);
v.setHint("Added programmatically");

TextInputLayout都有它自己的暗示参数和布局膨胀时,它得到的暗示从它的孩子的EditText并在其上设置空的提示。

当你想改变编程提示你要调用textInputLayout.setHint(字符串文本),而不是改变的EditText提示

TextInputLayout has it's own hint parameter and when inflating from layout it get's hint from it's child EditText and set empty hint on it.

When you want to change hint programatically you have to call textInputLayout.setHint(String text) instead of changing EditText hint

这篇关于TextInputLayout没有显示时,视图中添加编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆