在android中使用多视图

在android中使用多视图,android,android-layout,views,Android,Android Layout,Views,所以我有下面的onCreate函数 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mylocation = new MyLocation(this); double distance = distance (mylocation.lat, mylocation.lng, mes

所以我有下面的onCreate函数

        @Override
    protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);

       mylocation = new MyLocation(this);

       double distance = distance (mylocation.lat, mylocation.lng, messagelat, messagelong);
       Log.d("Distance",Double.toString(distance));
       mSensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
       mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION);
       mView = new SampleView(this,Double.toString(distance));
       **this.setContentView(R.layout.activity_clouds);**
       this.button = (Button)this.findViewById(R.id.close);
       this.button.setOnClickListener(new OnClickListener() {
         public void onClick(View v) {
             Intent myIntent = new Intent(Activity1.this, CameraPreview.class);
             Activity1.this.startActivity(myIntent);
         }
       });

       **setContentView(mView);**

   }

我的问题是,我想同时使用activity_cloud.xml中的视图和SampleView,但如果这样做,我只会得到示例视图!我想在SampleView类中实现一个按钮覆盖和覆盖。。。非常感谢

您可以在常规xml布局中使用自定义视图

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:orientation="vertical">
<com.something.something.SampleView
        android:id="@+id/sample_view"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent" />
<Button android:layout_height="0dp"
                                   android:layout_width="fill_parent"
                                   android:layout_weight="1"
                                   android:id="@+id/close"/>


为什么不直接将按钮从SampleView添加到activity_cloud?为什么不在xml中创建一个,然后获取id,然后执行类似myLinear.addView(MVView)的操作?我无法将id添加到LinearLayout?无法想象为什么?无法添加id?你是说这样吗?多谢你解决了我的问题!