0

I have the following code where MyClass basically extends View. I was wondering if I need to use both setContentView(R.layout.activity_mainlcass_app) and setContentView(myDrawing) to show the 2D graphics that I draw in MyClass.

public class MainClass extends Activity {
    MyClass myDrawing;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mainlcass_app);

        myDrawing = new myDrawing(this);
            setContentView(myDrawing);
            myDrawing.requestFocus();
    }
}
4

4 回答 4

2

不,你不能那样做。第二个布局将覆盖父视图。

于 2013-01-25T07:12:21.093 回答
2

在您的主布局(activity_mainlcass_app)中只需添加MyClass

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <com.example.MyClass
        android:id="@+id/myclass"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>
于 2013-01-25T07:14:15.143 回答
0

Try like this

<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">

<com.example.firstactivity
android:layout_width="fill_parent"
  android:layout_height="fill_parent"/>


<com.example.secondactivity
android:layout_width="fill_parent"
  android:layout_height="fill_parent"/>

</LinearLayout>

For more information check this link and this example

于 2013-01-25T07:18:40.223 回答
0

如果您想在其他(可能具有不同的布局大小)之上新绘图,请使用 FrameLayout 进行原始布局并使用重力来定位新绘图并使用 addView 活动方法。

于 2013-01-25T09:37:55.790 回答