라디오 버튼을 동적으로 추가하고 내용을 잃지 않는 방법
-
12-11-2019 - |
문제
내 레이아웃이 다음과 같습니다
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="Please select the restaurant you want to rate: " />
<RadioGroup android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/SelectRestaurant">
</RadioGroup>
<Button android:layout_width="100px"
android:layout_height="wrap_content"
android:text="Rate it"
android:id="@+id/submitSelection"/>
</LinearLayout>
.
Java 파일의 RadioGroup에 RadioButons를 추가하면 RadioButons가 서로 겹치고 제출 버튼이 사라졌습니다.나는 RadioButtons가 다른 뷰의 다른 요소를 무시하고 있다고 생각합니다.다른 요소를 잃지 않고이 라디오 버튼을 동적으로 추가 할 수 있습니까?
Java 코드는 다음과 같습니다.
setContentView(R.layout.submit_select);
RadioGroup radioGroup = (RadioGroup)findViewById(R.id.SelectRestaurant);
for (String restaurant : restaurants) {
RadioButton radioButton = new RadioButton(getBaseContext());
radioButton.setText(restaurant);
radioGroup.addView(radioButton);
}
radioGroup.invalidate();
dismissDialog(DIALOG_SUBMIT_ID);
rateItButton = (Button) findViewById(R.id.submitSelection);
rateItButton.setOnClickListener(this);
. 해결책
너비와 높이에 대한 wrap_content가있는 레이아웃 매개 변수를 만들고 그룹에 단추를 추가 할 때 layoutParams를 추가하십시오.
radiogroup.addView(newRadioButton, layoutParams);
. 제휴하지 않습니다 StackOverflow