本文介绍了在RelativeLayout的滚动型编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以我想换一个滚动型编程一个RelativeLayout的,但我不断收到,告诉我,RelativeLayout的已经有一个父。错误
So I was trying to wrap a RelativeLayout in a ScrollView programmatically, but I keep getting an error that tells me that the RelativeLayout already has a parent.
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);
}
}
为什么它不断给我一个问题吗?
Why does it keep on giving me a problem?
推荐答案
您正在调用的setContentView 2倍。只是删除它应该工作第一的setContentView。
其numberpicker右上图
You are calling setContentView 2 times. Just remove the first setContentView it should work.Its right above the numberpicker
setContentView(rl);
这篇关于在RelativeLayout的滚动型编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!