所以我试图以编程方式在ScrollView中包装RelativeLayout,但是我一直收到一条错误,告诉我RelativeLayout已经有了父级。
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.os.Bundle;
import android.util.Log;
import android.view.ViewGroup;
import android.widget.DatePicker;
import android.widget.NumberPicker;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.TimePicker;
/*
* Ids:
* 1-first Text box
* 2-DatePicker
* 3-TimePicker
*/
public class TwoActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//scrollView
ScrollView sv = new ScrollView(this);
Log.v("Ro", "Starts app");
//create Relative Layout
RelativeLayout rl = new RelativeLayout(this);
Log.v("Ro", "Relative Layout: "+rl);
//From Text View
RelativeLayout.LayoutParams lptv1 = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
lptv1.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
TextView tv1 = new TextView(this);
tv1.setText("From: 4-24-12 11:59pm");
tv1.setId(1);
rl.addView(tv1, lptv1);
//To Text View
RelativeLayout.LayoutParams lptv2 = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
lptv2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
TextView tv2 = new TextView(this);
tv2.setText("To: 4-25-12 12:00am");
rl.addView(tv2, lptv2);
//DatePicker
DatePicker startDate = new DatePicker(this);
startDate.setId(2);
RelativeLayout.LayoutParams lptv3 = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
lptv3.addRule(RelativeLayout.BELOW,1);
rl.addView(startDate, lptv3);
//TimePicker
TimePicker startTime = new TimePicker(this);
startTime.setId(3);
RelativeLayout.LayoutParams lptv4 = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
lptv4.addRule(RelativeLayout.BELOW,2);
rl.addView(startTime, lptv4);
setContentView(rl);
//NumberPicker
NumberPicker duration = new NumberPicker(this);
RelativeLayout.LayoutParams lptv5 = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
lptv5.addRule(RelativeLayout.BELOW,3);
rl.addView(duration, lptv5);
sv.addView(rl);
//setContentView(rl);
setContentView(sv);
//setContentView(R.layout.main);
}
}
为什么它继续给我一个问题?
答案 0 :(得分:1)
您正在调用setContentView 2次。只需删除它应该工作的第一个setContentView。 它位于数字选择器正上方
setContentView(rl);