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 に答える