我需要显示在静态Edit文本下方动态添加的n个Edit文本。
我要添加ET,但是有些不可见。怎么了?

这是我的代码:

private void addEditTextView(int numberOfViews){
    // Using layout params same as above static ET
    ViewGroup.LayoutParams layoutParams = staticEditText.getLayoutParams();

    for (int index = 0; index <= numberOfViews; index++) {
        final EditText newET = new EditText(getActivity());

        //Added below 2 lines just to make sure width and height are coming
        layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
        newET.setLayoutParams(layoutParams);
        newET.setHint("Select ME");
        newET.setFocusable(false);
        newET.setId(index);
        newET.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //TODO set time
                Log.d("Clicked...",newET.getId()+"");
            }
        });
        //parentLayout is Linear Layout with Vertical orientation
        parentLayout.addView(newET);
    }
}


XML代码:

 <LinearLayout
        android:id="@+id/parentLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <EditText
            android:id="@+id/staticEditText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="3dp"
            android:background="@drawable/rectangle_bg_dark_gray_border"
            android:focusable="false"
            android:hint="staticEditText"
            android:padding="7dp" />

    </LinearLayout>


更新:parentLayout.getChildCount()正确到来。新视图已添加,但不可见!

最佳答案

嗨,尝试添加Scrollview

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       xmlns:android="http://schemas.android.com/apk/res/android">
       <ScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <LinearLayout
                  android:id="@+id/parentLayout"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:orientation="vertical">
                  <!-- EDITTEXT here -->
            </LinearLayout>
      </ScrollView>
 </LinearLayout>

07-24 09:44
查看更多