在android中使用多个视图

时间:2012-08-08 01:45:06

标签: 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, 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类中添加一个按钮并覆盖它...非常感谢

1 个答案:

答案 0 :(得分:0)

您可以在常规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"/>