Android - 动态编辑文本不可见
Android - Dynamic Edit Text Not Visible
我需要在静态编辑文本下方显示动态添加的 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() 正确。正在添加新视图,但不可见!
您好,请尝试添加滚动视图
<?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>
我需要在静态编辑文本下方显示动态添加的 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() 正确。正在添加新视图,但不可见!
您好,请尝试添加滚动视图
<?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>