Reputation: 227
(note that I'm a total beginner in android programming)
I have a class that derives from GLSurfaceView.
What I would like is to place some views (image,text) on top of it.
I managed to get the text view to position properly by using textView.setPadding(300,0,0,0)
;
The problem is that I cannot get the image view to position properly. I've tried imageView.layout(), imageView.setPadding()
.
Here is the code:
ImageView imageView=new .... // Create and set drawable
// Setting size works as expected
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(200,200);
imageView.setLayoutParams(lp);
surfaceView = new MySurfaceViewDerivedFromOpenGLSurface(...);
setContentView(surfaceView);
addContentView(textView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
addContentView(textView2, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
addContentView(imageView, lp); // Add the image view
Is it even possible to position it properly this way without specifing the stuff in the XML file?
I saw an example in the sdk dev website that shows how to create views on the openGL surface view, but the problem is that I have a derived class and I don't know if I can specify it in the XML file (I have 0% experience in the XML, Eclipse handled everything for me so far).
Upvotes: 0
Views: 3650
Reputation: 17800
You'll save a ton of headaches later by learning how to use xml layout. Specifying a layout for a custom view tripped me up too. This is how it works:
<view class="complete.package.name.goes.here.ClassName"
android:id="@+id/workspace"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</view>
So a really simple vertical layout for your app would be:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView android:id="@+id/textView2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<ImageView android:id="@+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<view class="package.name.to.MySurfaceViewDerivedFromOpenGLSurface"
android:id="@+id/mySurfaceView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
</view>
</LinearLayout>
You can get a reference to anything in your layout files as long as there's an id:
ImageView myImageView = (ImageView ) findViewById(R.id.imageView1);
Upvotes: 2