[英]Add spinner with click of a button dynamically in Android
I was trying to add spinner to my app dynamically by using the following code: 我试图通过使用以下代码动态地将微调器添加到我的应用程序:
@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);
}
This adds the spinner but replaces everything I have in my XML with the dynamically-created spinner. 这会添加微调器,但用动态创建的微调器替换我在XML中的所有内容。 I want to add the spinner to the layout (which I have created by using XML) by clicking a button, not replacing the layout. 我想通过单击按钮将微调器添加到布局(我使用XML创建),而不是替换布局。 Please help. 请帮忙。
EDIT: Here is my XML file 编辑:这是我的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>
This is what You have done 这就是你所做的
This is what I think will work. 这是我认为可行的。
findViewById(R.id.root_layout)
. 您可以使用findViewById(R.id.root_layout)
获取根布局。 root_layout
. 现在将微调器添加到root_layout
。 Your Mistake 你的错误
You made a new layout and set it as the main content and you never added it to the root_layout. 您创建了一个新布局并将其设置为主要内容,并且您从未将其添加到root_layout。
Debugging 调试
If you want to see what layout hierarchy your layout has.Open ddms view and click on the heirarchy Viewer option there.Its located near the camera option.Click it and get the screen and you will see your layout hierarchy which will display a linearLayout containing a spinner. 如果你想看看你的布局有什么样的布局层次。打开ddms视图,然后点击那里的heirarchy Viewer选项。它位于相机选项附近。点击它并进入屏幕,你会看到你的布局层次结构,它将显示一个linearLayout包含一个微调器。
Here's a code to demonstrate it. 这是一个演示它的代码。
package com.example.testproject;
import android.app.Activity; import android.app.Activity; import android.os.Bundle; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.ArrayAdapter; import android.widget.LinearLayout; import android.widget.LinearLayout; import android.widget.RelativeLayout; import android.widget.RelativeLayout; import android.widget.Spinner; import android.widget.Spinner;
public class MainActivity extends Activity { 公共类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);
}
} }
Hope it helps you. 希望它能帮到你。
Mistake : You created new linear layout(which dose not contain any view) and added spinner to it then repaced your activity_main layout with this layout which only contains spinner. 错误 :您创建了新的线性布局(不包含任何视图)并添加了微调器,然后使用此布局重新定位您的activity_main布局,该布局仅包含微调器。
Easy way is to make Spinner invisible initially and make it visible when button is clicked 简单的方法是最初使Spinner不可见,并在单击按钮时使其可见
So declare spinner spinner visibility="gone" in XML 因此,在XML中声明spinner spinner visibility =“gone”
<Spinner
android:id="@+id/my_spin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
/>
In Oncreate 在Oncreate中
spinner = (Spinner) findViewById(R.id.my_spin);
Now make spinner visible on Click of button 现在,在Click of按钮上显示微调器
timer_spin.setVisibility(View.VISIBLE);
Note : 注意 :
GONE :This view is invisible, and it doesn't take any space for layout purposes. GONE :此视图不可见,并且不需要任何空间用于布局。
INVISIBLE :This view is invisible, but it still takes up space for layout purposes INVISIBLE :此视图不可见,但仍然占用了布局空间
VISIBLE :This view is visible. 可见 :这种观点是可见的。
Could you post your activity_main.xml
file located in the res/layout/
folder of your project. 您可以发布位于项目的res/layout/
文件夹中的activity_main.xml
文件。
In any case, you should be adding your spinner to your main_layout, not to some new linearlayout not connected to anything. 无论如何,你应该将你的微调器添加到你的main_layout,而不是一些没有连接到任何东西的新线性布局。
And below is what the solution should look like (although, this will only work depending on what's actually written in your activity_main.xml
file): 以下是解决方案的外观(尽管如此,这只会取决于您在activity_main.xml
文件中实际写入的内容):
@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);
// The line below assumes that the layout called main_layout
// found in the res/layout/activity_main.xml is a LinearLayout
LinearLayout mainLayout = (LinearLayout)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);
mainLayout.addView(spinner);
}
After you correct this mistake, I'd suggest you study a little bit Java and do some Java exercises for at least twenty minutes a day during three or four weeks. 在你纠正了这个错误之后,我建议你学习一点Java并在三到四周内每天至少做二十分钟的Java练习。 Once you know a little bit more of Java, you'll be able to correct mistakes such as these very easily. 一旦你了解了一点Java,你就能够很容易地纠正这些错误。
please have a look at yours code 请看看你的代码
setContentView(R.layout.activity_main);
but later 但后来
setContentView(linearlayout);
handle all these things carefully 仔细处理所有这些事情
please have a seperate linear layout at yours main xml layout and use following 请在您的主xml布局中使用单独的线性布局并使用以下内容
final LinearLayout lm = (LinearLayout) findViewById(R.id.linearMain);
instead of LinearLayout linearlayout = new LinearLayout(this);
而不是LinearLayout linearlayout = new LinearLayout(this);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.