2011-07-28 181 views
0

(注意,我是Android编程的初学者)在自定义GLSurfaceView上添加视图

我有一个派生自GLSurfaceView的类。

我想要的是放置一些意见(图像,文字)。 我设法通过使用textView.setPadding(300,0,0,0)来正确定位文本视图;

问题是我无法正确定位图像视图。我试过imageView.layout(), imageView.setPadding()

下面是代码:

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 

它甚至有可能将其正确的使用方法,而不在XML文件中specifing填充位置?

我在sdk dev网站上看到了一个例子,展示了如何在openGL表面视图上创建视图,但问题是我有一个派生类,我不知道我是否可以在XML文件中指定它(我在XML中有0%的经验,到目前为止,Eclipse为我处理所有事情)。

回答

2

以后您将学习如何使用xml布局,从而节省大量的麻烦。指定自定义视图的布局也会让我感到沮丧。这是如何工作的:

<view class="complete.package.name.goes.here.ClassName" 
    android:id="@+id/workspace" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" >   
</view> 

所以,一个非常简单的垂直布局为您的应用程序将是:

<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> 

可以,只要让你的布局文件到任何一个参考有一个ID:

ImageView myImageView = (ImageView) findViewById(R.id.imageView1); 
+0

不错,非常感谢:D – n3XusSLO