2011-12-30 41 views
0

我有兩個相對佈局,當前顯示爲一個在另一個之下。我希望第二個佈局以編程方式顯示在第一個相對佈局的右側。以編程方式在另一個相對佈局旁放置一個相對佈局

以下是我使用的代碼。

main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 
    <RelativeLayout 
     android:layout_width="620dp" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:id="@+id/lay1" >  
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/hello1" /> 
    </RelativeLayout> 
    <RelativeLayout 
     android:layout_width="620dp" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:id="@+id/lay2"> 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/hello2" /> 
    </RelativeLayout> 
</LinearLayout> 

myAct.java

public class myAct extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     LinearLayout layoutContainer = new LinearLayout(this); 
     layoutContainer.setLayoutParams(new  LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 

     // Arguments here: width, height, weight 
     LinearLayout.LayoutParams childLp = new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1); 
     RelativeLayout layoutLeft = new RelativeLayout(this); 

     layoutContainer.addView(layoutLeft, childLp); 
     RelativeLayout layoutRight = new RelativeLayout(this); 
     layoutContainer.addView(layoutRight, childLp); 
    } 
} 

在此先感謝

回答

1

更改您的線性佈局方向與水平。我還將線性佈局的權重總和設置爲2,並給每個相對佈局權重1以確保均勻分割。

+0

謝謝Tony..it工程... :-) – JKV 2011-12-30 07:31:22

+0

@jinsi這似乎是愚蠢的。爲什麼你在標題中添加了** programaticcaly **? :-) – 2011-12-30 07:40:35

+0

現在我有n個相對的佈局,我想在兩排佈局中排列。誰能幫我嗎... – JKV 2011-12-30 08:51:12

0

修改代碼中的這些更改,您將獲得水平方向。

main.xml中

android:layout_width="620dp"android:layout_width="wrap_content"

LinearLayout

添加android:id="@+id/ll"在myAct.java

刪除所有語句setContentView後,並添加以下行

LinearLayout ll = (LinearLayout)findViewById(R.id.ll); 
ll.setOrientation(LinearLayout.HORIZONTAL); 

試試吧,如果有疑問的話問我。

0

對謂語或行佈局代碼一看寫的這篇文章:

Line-breaking widget layout for Android

更改根據您的要求,下面一行。

child.layout(xpos, ypos, xpos + childw, ypos + childh); 

例如,如果你需要在一列只有2個TextView的,這樣做

if((i%2) != 0) 

xpos = screenWidth/2; 

else 

xpos = 0;