EditText를 프로그래밍 방식으로 LinearLayout
에 추가하려고합니다. 나는 누구의 클릭에 '+'ImageButton
을 추가했는데, 5 시까 지 EditText
을 추가 할 것입니다. 아래는 내 XML 파일의 발췌 문장입니다. 프로그래밍 방식으로 편집 텍스트를 추가 할 때 LinearLayout이 확장되지 않습니다.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/margin_16"
android:orientation="horizontal">
<LinearLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
</LinearLayout>
<ImageButton
android:id="@+id/add_field"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_marginBottom="@dimen/margin_16"
android:background="@drawable/cyan_top_rounded"
android:src="@drawable/add" />
</LinearLayout>
는 ID가 "컨테이너"와
LinearLayout
는 자식으로 글고있을 것이다. 다음은 EditText를 추가하기위한 코드입니다.
mAddField.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (count < 5) {
mAddField.setEnabled(true);
mContainer.addView(getTextField());
count++;
} else {
mAddField.setEnabled(false);
}
}
});
private EditText getTextField() {
EditText et = new EditText(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
et.setLayoutParams(params);
et.setText("abcd");
return et;
}
private void initUI() {
mAddField = (ImageButton) findViewById(R.id.add_field);
mContainer = (LinearLayout) findViewById(R.id.container);
EditText et = new EditText(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
et.setLayoutParams(params);
et.setText("abcd");
mContainer.addView(et);
}
그러나 ImageButton을 클릭해도 아무런 변화가 없습니다. EditText가 전혀 추가되지 않으면 LinearLayout도 확장되지 않습니다. 도와주세요.
android : layout_height = "match_parent"를 android : layout_height = "wrap_content"로 변경해보십시오. –
wrap_content 및 확인하려면 체중 및 높이를 제거하고 확인하십시오. –
네,이게 효과가 있어요. 두 LinearLayouts의 높이를 wrap_content로 설정했습니다. 고마워요. –