我試圖微調使用下面的代碼動態添加到我的應用程序:一個按鈕,點擊添加微調動態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獲取視圖(相對或線性),然後向其添加微調器,或者將您創建的線性佈局添加到該佈局。 –