2012-03-11 5 views
0

Androidビューの一部をプログラムで構築しようとしていますが、残念ながらRelativeLayoutに問題があります。これは、それがどのように見えるかですRelativeLayout要素が重複しています

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.insertnew_layout); 
    LinearLayout ll = (LinearLayout) findViewById(R.id.container); 

    ll.setOrientation(LinearLayout.VERTICAL); 
    TableLayout tl = new TableLayout(this); 
    tl.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 


     // fill content 
     for (int i = 0; i < 3; i++) { 

     TableRow tr = new TableRow(this); 
     tr.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 

     RelativeLayout rl = new RelativeLayout(this); 
     rl.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 

     TextView score = new TextView(this); 
     score.setText(""+i); 
     RelativeLayout.LayoutParams lpScore = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.FILL_PARENT); 
     lpScore.addRule(RelativeLayout.ALIGN_PARENT_LEFT); 
     score.setLayoutParams(lpScore); 

     TextView description = new TextView(this); 
     description.setText("this is my description"); 
     RelativeLayout.LayoutParams lpDescription = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.FILL_PARENT); 
     lpDescription.addRule(RelativeLayout.RIGHT_OF, score.getId()); 

     description.setLayoutParams(lpDescription); 

     CheckBox checkbox = new CheckBox(this); 
     RelativeLayout.LayoutParams lpCheckbox = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.FILL_PARENT); 
     lpCheckbox.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); 
     checkbox.setLayoutParams(lpCheckbox); 

     rl.addView(score); 
     rl.addView(description); 
     rl.addView(checkbox); 
     tl.addView(rl); 

    } 

    ll.addView(tl); 

:あなたが見ることができるように

output in javacode

、「説明は」上で、それはお互い

の私Elementsオーバーレイは、これは私のコードで作ります「スコア」のトップここで

は、XMLで同じコードです:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:id="@+id/tableLayoutTextEntry" > 

<TableRow 
    android:id="@+id/tableRowTextEntry" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" > 

    <RelativeLayout 
     android:id="@+id/relativeLayout1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" > 

     <TextView 
      android:id="@+id/score" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:text="score" /> 

     <TextView 
      android:id="@+id/description" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_toRightOf="@+id/score" 
      android:text="description" /> 

     <CheckBox 
      android:id="@+id/checkBox" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentRight="true" 
      android:text="select" /> 

    </RelativeLayout> 

</TableRow> 

</TableLayout> 

そして、ここではそれがXMLで次のようになります。

output in xml

あなたは、XMLでは、toRightOf-コマンドを見ることができるように説明は、スコアの右側にあります。

これはどのように可能ですか?ライン

lpDescription.addRule(RelativeLayout.RIGHT_OF, score.getId());

同じことを行うのはずの?

+0

レイアウトをコードで作成する必要がありますか? – Egor

+0

@Egorはい、まさに彼はListView用の**カスタムアダプタを定義することでこれを簡単に実装できます** –

答えて

1

score.getId()は、まだレイアウト全体が処理された状態で画面に追加されていないため、-1(無効)を返します。 getId()を呼び出す前にidを手動でsetId()に設定する必要があります。すべて正常に動作するはずです。