2016-09-27 41 views
-1

我加入一个的LinearLayout视图中显示,这种方式:动态添加TextInputLayout不LinearLayout中

LinearLayout ll=(LinearLayout)findViewById(R.id.linearlayout); 
TextInputLayout til=new TextInputLayout(MainActivity.this); 
til.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 
LinearLayout.LayoutParams.WRAP_CONTENT)); 
Log.i("TAG","BEFORE: "+ll.getChildCount()); 
ll.addView(til); 
Log.i("TAG","AFTER: "+ll.getChildCount()); 

这样,没有显示直到对象,但补充说,因为在第二日志我看到这个数字增加了一个,我刚刚添加的观点。

为什么我看不见?如何在布局中显示新视图?

谢谢。

+0

我认为它不会显示,因为你的'TextInputLayout'是空的。尝试在'TextInputLayout'中添加'TextInputEditText'。 – pike

+0

尝试膨胀它:http://stackoverflow.com/questions/31294230/textinputlayout-not-showing-when-view-added-programmatically – codemirel

回答

1

我想你忘记了什么是添加一个EditText或TextInputEditText到TextInputLayout。

Android的文件说:

[TextInputLayout是]布局它包装一个EditText(或子孙)来显示一个浮动标签时的提示被隐藏,由于用户输入的文本。

EditText editText = new EditText(this); 

    RelativeLayout.LayoutParams editTextParams = new RelativeLayout.LayoutParams(
      RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
    editText.setLayoutParams(editTextParams); 

    editText.setTextSize(16); 
    editText.setTextColor(getResources().getColor(R.color.black)); 
    editText.setHint("hint"); 
    editText.setHintTextColor(getResources().getColor(R.color.gray)); 

    textInputLayout.addView(editText, editTextParams); 
+0

这是错误的。非常感谢你。 – Fustigador

0

til零高度。而不是使用这样的:

til.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));

+0

这种方式是之前我做了一些改变,并没有奏效。 – Fustigador

1

TextInputLayout什么,而不必EditText的孩子。

创建如下布局文件text_input_layout.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.TextInputLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <EditText 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" /> 

</android.support.design.widget.TextInputLayout> 

现在,添加,只是夸大这种布局和下面addView()LinearLayout

LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linearlayout); 
TextInputLayout textInputLayout = (TextInputLayout) LayoutInflator.from(this).inflate(R.layout.text_input_layout, null); 
linearLayout.addView(textInputLayout); 

希望这有助于...

注意:您也可以将TextInputEditText作为0123子项

相关问题