Android - 动态编辑文本不可见

时间:2015-08-04 08:35:59

标签: java android android-layout android-fragments android-edittext

我需要在静态编辑文本下面显示动态添加的n个编辑文本。 我正在添加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()正确显示。新视图已添加但不可见!

1 个答案:

答案 0 :(得分:1)

您好,尝试添加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>