2012-05-26 154 views
0

我已經試過在Relative layout Coding處詢問這個問題,但是第一次嘗試並不是很清楚。OpenGL和Android相對佈局

我想在GLSurfaceView創建一個相對佈局,但佈局必須是這樣的:

<RelativeLayout 
     android:id="@+id/View1" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:background="@raw/gi" 
     android:orientation="vertical" > 

     <ImageView 
       android:id="@+id/imageView1" 
       android:layout_width="64px" 
       android:layout_height="64px" 
       android:layout_alignParentLeft="true" 
       android:layout_alignParentTop="true" 
       android:src="@drawable/b1" /> 

     <ImageView 
       android:id="@+id/imageView2" 
       android:layout_width="64px" 
       android:layout_height="64px" 
       android:layout_alignParentTop="true" 
       android:layout_toRightOf="@+id/imageView1" 
       android:src="@drawable/b2" /> 

     <ImageView 
       android:id="@+id/imageView3" 
       android:layout_width="64px" 
       android:layout_height="64px" 
       android:layout_alignParentTop="true" 
       android:layout_toRightOf="@+id/imageView2" 
       android:src="@drawable/b3" /> 
</RelativeLayout> 

但是,如果我的代碼它,就像他們在它的工作原理回答說,但圖片尚未提上彼此頂部。 (我用getId函數進行addrules)

所以我正在考慮添加整個XML文件並以這種方式工作,但任何時候我加載一個XML應用程序停止。這裏的大部分代碼:

public class HelloWorld extends Activity { 
... 
protected void onCreate(Bundle savedInstanceState) { 
    .... 
    opengl = new GLSurfaceView(getApplication()); 
    opengl.setEGLConfigChooser(new GLSurfaceView.EGLConfigChooser() { 
     public EGLConfig chooseConfig(EGL10 egl, EGLDisplay display) { 
       int[] attributes = new int[] { EGL10.EGL_DEPTH_SIZE, 16, EGL10.EGL_NONE}; 
       EGLConfig[] configs = new EGLConfig[1]; 
       int[] result = new int[1]; 
       egl.eglChooseConfig(display, attributes, configs, 1, result); 
       return configs[0]; 
     } 
    }); 

    renderer = new MyRenderer(); 
    mGLView.setRenderer(renderer); 
    setContentView(opengl); 
    addContentView(findViewById(R.id.View1), new RelativeLayout.LayoutParams(
       RelativeLayout.LayoutParams.FILL_PARENT, 
       RelativeLayout.LayoutParams.FILL_PARENT)); 

記住,沒有最後調用addContentView應用作品。

+0

看起來我要回答我自己的問題。 代替addcontentview添加以下 GI =新的RelativeLayout(本); 查看查看; LayoutInflater inflater =(LayoutInflater)getApplicationContext()。getSystemService(Context.LAYOUT_INFLATER_SERVICE); view = inflater.inflate(R.layout.gi,null); hud.addView(view); \t \t addContentView(gi,new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.FILL_PARENT)); – user1398593

+0

因此我成功地在glsurface視圖上添加了一個完整的xml文件。 但我現在想知道如何獲得r.layout.gi中每個孩子的ID。假設我想改變一張R.id.imageView1的圖片?????????????? (特別感謝http://stackoverflow.com/questions/2335813/how-to-inflate-one-view-with-an-layout) – user1398593

+1

評論並非答案。如果你想回答自己的問題,實際上*通過使用頁面底部的「發佈你的答案」按鈕來回答它。 –

回答

0

看起來我要回答我自己的問題。

代替addcontentview添加以下

gi=new RelativeLayout(this); 
       View view; 
     LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     view = inflater.inflate(R.layout.gi, null); 
     hud.addView(view); 
     addContentView(gi, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT)); 

,因此我在添加glsurface視圖上方整體xml文件succeded。

但我現在想知道如何獲得r.layout.gi中每個孩子的ID。假設我想改變一張R.id.imageView1的圖片??????????????

有沒有人知道如何用當前的代碼做到這一點?

0

我知道這與我正常的FrameLayout工作。

TextView counter = new TextView(getApplicationContext()); 
    counter.setBackgroundResource(R.drawable.counter); 
    counter.setTextSize(50); 
    counter.setTypeface(null, Typeface.BOLD); 
    counter.setText(" 000"); 
    addContentView(counter, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));