0

所以我试图以编程方式将 RelativeLayout 包装在 ScrollView 中,但我不断收到一个错误消息,告诉我 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);
    }
}

为什么它总是给我一个问题?

4

1 回答 1

1

您正在调用 setContentView 2 次。只需删除它应该工作的第一个 setContentView 。它就在数字选择器的正上方

setContentView(rl);
于 2012-04-25T00:59:47.643 回答