2014-01-17 67 views
-1

我在android中構建了一個簡單的兩列列表,並且需要動態填充行數。 XML佈局工作正常,但是當我嘗試以編程方式構建佈局時,第二列不顯示。有任何想法嗎?android編程佈局

private void addRow(String text, String value) { 
    RelativeLayout line = new RelativeLayout(this); 

    line.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT)); 

    RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
    rlp.addRule(RelativeLayout.ALIGN_PARENT_LEFT); 
    rlp.addRule(RelativeLayout.CENTER_VERTICAL); 

    line.setPadding(0, 0, 0, 15); 

    TextView caption = new TextView(this); 
    caption.setLayoutParams(rlp); 
    caption.setTextSize(TypedValue.COMPLEX_UNIT_PX, 30); 
    caption.setText(text); 

    rlp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
    rlp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); 
    rlp.addRule(RelativeLayout.CENTER_VERTICAL); 

    TextView val = new TextView(this); 
    val.setLayoutParams(rlp); 
    val.setTextColor(0x05b4e4); 
    val.setTextSize(TypedValue.COMPLEX_UNIT_PX, 30); 
    val.setText(value); 

    line.addView(caption); 
    line.addView(val); 

    mLayout.addView(line); 
} 

可比XML的單行(即作品)這是

<RelativeLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:paddingBottom="15px" > 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textSize="30px" 
      android:text="What" 
      android:layout_alignParentLeft="true" 
      android:layout_centerVertical="true" 
      /> 

     <TextView 
      android:id="@+id/txtClubPath" 
      android:textColor="#05b4e4" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textSize="30px" 
      android:text="stuff" 
      android:layout_alignParentRight="true" 
      android:layout_centerVertical="true" 
      /> 

    </RelativeLayout> 

再次 - 行填充,而第二列是不可見的。下面是它進入:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 

android:id="@+id/scrollViewRoot" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:fillViewport="true" 
android:keepScreenOn="true"> 

<LinearLayout 
    android:id="@+id/viewRoot" 
    android:paddingTop="20px" 
    android:paddingBottom="40px" 
    android:paddingLeft="20px" 
    android:paddingRight="20px" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical"> 

</LinearLayout> 

</ScrollView> 
+0

爲什麼-1等級? – kolosy

回答

0

要添加的類型應該LinearLayout.LayoutParams的,因爲父母將是此類型的視圖的佈局PARAMS。

此行

line.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT)); 

應該成爲

line.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT)); 

不過,我想,這應該引起如果有什麼異常。

+0

你說得對,就是這樣。不幸的是,它沒有解決問題。 – kolosy

0

RelativeLayout子對象具有唯一的非零非常重要。

你可以,在API 17+,使用

myView.setId(View.generateViewId()); 

或者,如果你要支持舊的API級別,你可以使用(每次遞增),一個簡單的靜態的AtomicInteger值。

+0

沒有效果。嘗試將AtomicInteger設置爲'Integer.MAX_VALUE - 1000',但無效。 – kolosy