我试图微调使用下面的代码动态添加到我的应用程序:一个按钮,点击添加微调动态Android中
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startTime = (EditText)findViewById(R.id.EditTextST);
startDate = (EditText)findViewById(R.id.editTextSD);
LinearLayout linearlayout = new LinearLayout(this);
//RelativeLayout linearlayout = (RelativeLayout)findViewById(R.id.main_layout);
ArrayList<String> spinnerArray = new ArrayList<String>();
spinnerArray.add("one");
spinnerArray.add("two");
spinnerArray.add("three");
spinnerArray.add("four");
spinnerArray.add("five");
Spinner spinner = new Spinner(this);
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item, spinnerArray);
spinner.setAdapter(spinnerArrayAdapter);
linearlayout.addView(spinner);
setContentView(linearlayout);
}
这增加了微调,但在我的XML与替换我的一切动态创建的微调器。 我想通过单击按钮将布局添加到布局中(我使用XML创建的布局),而不是替换布局。请帮忙。
编辑:这是我的XML文件
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
android:id="@+id/main_layout"
android:layout_width="match_parent"
android:layout_height="900dp" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="@string/select_week"
android:textAppearance="?android:attr/textAppearanceMedium" />
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:text="@string/sun" />
<CheckBox
android:id="@+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/checkBox1"
android:layout_alignBottom="@+id/checkBox1"
android:layout_toRightOf="@+id/checkBox1"
android:text="@string/mon" />
<CheckBox
android:id="@+id/checkBox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/checkBox2"
android:layout_toRightOf="@+id/checkBox2"
android:text="@string/tue" />
<CheckBox
android:id="@+id/checkBox4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/checkBox3"
android:layout_alignBottom="@+id/checkBox3"
android:layout_toRightOf="@+id/checkBox3"
android:text="@string/wed" />
<CheckBox
android:id="@+id/checkBox5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/checkBox1"
android:layout_toLeftOf="@+id/checkBox2"
android:text="@string/thu" />
<CheckBox
android:id="@+id/checkBox6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/checkBox2"
android:layout_below="@+id/checkBox2"
android:text="@string/fri" />
<CheckBox
android:id="@+id/checkBox7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/checkBox2"
android:layout_toRightOf="@+id/checkBox2"
android:text="@string/sat" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/checkBox5"
android:layout_marginTop="18dp"
android:text="@string/select_week_number"
android:textAppearance="?android:attr/textAppearanceMedium" />
<CheckBox
android:id="@+id/CheckBox01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/checkBox02"
android:layout_alignBottom="@+id/checkBox02"
android:layout_alignLeft="@+id/checkBox5"
android:text="@string/one" />
<CheckBox
android:id="@+id/CheckBox03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/checkBox02"
android:layout_alignBottom="@+id/checkBox02"
android:layout_toRightOf="@+id/checkBox6"
android:text="@string/three" />
<CheckBox
android:id="@+id/checkBox02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView2"
android:layout_toRightOf="@+id/checkBox5"
android:text="@string/two" />
<CheckBox
android:id="@+id/CheckBox04"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/CheckBox03"
android:layout_alignBottom="@+id/CheckBox03"
android:layout_toRightOf="@+id/textView2"
android:text="@string/four" />
<TextView
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/CheckBox01"
android:layout_marginTop="16dp"
android:text="@string/select_start_date"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/editTextSD"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/CheckBox01"
android:layout_below="@+id/TextView01"
android:layout_toLeftOf="@+id/CheckBox04"
android:ems="10"
android:inputType="text|date"
android:onClick="showDatePickerDialog" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/EditTextST"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/editTextSD"
android:layout_alignBottom="@+id/editTextSD"
android:layout_alignLeft="@+id/CheckBox04"
android:ems="10"
android:inputType="time"
android:onClick="showTimePickerDialog" />
<TextView
android:id="@+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editTextSD"
android:layout_below="@+id/editTextSD"
android:layout_marginTop="17dp"
android:text="@string/select_end_date"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/EditText01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/TextView02"
android:layout_alignRight="@+id/editTextSD"
android:layout_below="@+id/TextView02"
android:ems="10"
android:inputType="text|date"
android:onClick="showDatePickerDialog" />
<EditText
android:id="@+id/EditText02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/EditText01"
android:layout_alignBottom="@+id/EditText01"
android:layout_alignLeft="@+id/EditTextST"
android:ems="10"
android:inputType="time"
android:onClick="showTimePickerDialog" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/EditText01"
android:layout_below="@+id/EditText01"
android:layout_marginTop="17dp"
android:text="@string/reminder"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Spinner
android:id="@+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView3"
android:entries="@array/reminder_times"
android:paddingLeft="-10dp"
android:gravity="left"
tools:listitem="@android:layout/activity_list_item" />
<Button
android:id="@+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView3"
android:layout_below="@+id/spinner1"
android:layout_marginTop="119dp"
android:text="@string/add_reminder" />
</RelativeLayout>
</ScrollView>
你有尝试过吗? – Riser
您正在将contentView设置为您创建的线性布局,因此进行替换,使用findViewById获取视图(相对或线性),然后向其添加微调器,或者将您创建的线性布局添加到该布局。 –