我尝试使用以下代码将微调器动态添加到我的应用程序:
@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>
最佳答案
这就是你所做的
- 添加了一个新的 LinearLayout。
- 在上面的布局中添加了一个微调器。
- 然后您设置内容 View 。
我认为这是可行的。
- 您可以使用
findViewById(R.id.root_layout)
获取根布局。 - 现在创建一个微调器。按照上面代码中的正确操作向其添加项目。
- 现在将微调器添加到您的
root_layout
。
你的错误
您创建了一个新布局并将其设置为主要内容,但您从未将其添加到 root_layout。
调试
如果你想看看你的布局有什么布局层次。打开 ddms View 并单击那里的层次查看器选项。它位于相机选项附近。单击它并获取屏幕,你将看到你的布局层次将显示包含微调器的 linearLayout。
这里有一个代码来演示它。
package com.example.testproject;
导入android.app.Activity; 导入 android.os.Bundle; 导入 android.widget.ArrayAdapter; 导入 android.widget.LinearLayout; 导入 android.widget.RelativeLayout; 导入 android.widget.Spinner;
公共(public)类 MainActivity 扩展 Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/**
* This is what you have done!!!
*/
LinearLayout layout=new LinearLayout(this);
String[] strings={"1","2","3"};
Spinner spinner=new Spinner(this);
spinner.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item,strings));
layout.addView(spinner);
/**
* Comment this line when you use the below mentioned solution.
*/
setContentView(layout);
/**
* This is what you should have done.
*/
RelativeLayout root_layout=(RelativeLayout)findViewById(R.id.root_layout);
root_layout.addView(spinner);
}
希望对你有帮助。
关于android - 在 Android 中动态单击按钮添加微调器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21177674/