2012-05-25 71 views
-1

可能重複:
OpenGL and Android relative layout相對佈局編碼

海蘭,我不知道如何將這個XML,因爲我需要它的代碼代碼

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

放置就是這樣。 我的一切至今

gi = new RelativeLayout(this); 
     gi.setBackgroundResource(R.raw.gi); 

     img1= new ImageView(this); 
     img1.setImageResource(R.drawable.b1); 
     img1.setLayoutParams(); 
     img2= new ImageView(this); 
     img2.setImageResource(R.drawable.b2); 
     img3= new ImageView(this); 
     img3.setImageResource(R.drawable.b3); 

     gi.addView(img1,64,64); 
     gi.addView(img2,64,64); 
     gi.addView(img3,64,64); 

我顯然遇到一些麻煩,我不能在文檔中找到alignparents。同樣是這些像素,DPI的或者在addview東西

回答

0

檢查RelativeLayout.LayoutParams

它應該有你需要的一切。

+0

謝謝,但我仍然無法找到這個layout_to_right_of沒有使用imageview r.id,如果我將它們全部添加像這樣,他們堆疊在另一個之上 – user1398593

+0

好的,這裏也許是一個更好的鏈接:http://stackoverflow.com/questions/5191099/how-to-set-relativelayout-layout-params-in-code-not-in-xml。請注意,我認爲它只是需要id,以便知道如何在視圖之間建立關係,因爲這就是relativeLayout的工作原理。當然,我可能是錯的,但根據API看起來這樣。 –

+0

也不是真的有幫助,我之所以想這樣做的原因是將它添加到glsurface視圖。我需要添加到XML以使其工作以及如何啓動我的GLSurfaceView? – user1398593

0

一個RelativeLayout可以在大多數情況下是有用的,但在這個特殊的一個,你可以更好地通過以下服務:

<LinearLayout android:orientation="horizontal" android:layout_width="match_parent"> 
    <ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="64px" 
     android:layout_height="64px" 
     android:src="@drawable/b1" /> 

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

    <ImageView 
     android:id="@+id/imageView3" 
     android:layout_width="64px" 
     android:layout_height="64px" 
     android:src="@drawable/b3" /> 
</LinearLayout> 

要回答你原來的問題,這些都顯示不正常的原因是因爲其中:

android:layout_toRightOf="@+id/imageView1" 

查看@+id?這就是創建這個視圖的新實例,它混淆了XML id

什麼,你應該使用這就是:

android:layout_toRightOf="@id/imageView1" 

希望這有助於!

+0

感謝,但我希望有一個程序化的方法,XML文件的工作,但現在我還是想去做這樣 – user1398593

+0

我們強烈鼓勵儘可能使用XML佈局與ViewStubs的動態元素。維護更容易,特別是對於不同的屏幕尺寸。 – Codeman

+0

我真的很抱歉,但現在xml文件顯示沒有資源匹配'id'給定名稱與....現在什麼 – user1398593