2016-02-05 112 views
0

我的程序工作的方式是我接受3個項目的用戶輸入,用這三個項目以編程方式創建一個水平的LinearLayout。然後將LinearLayout放入以XML定義的RelativeLayout內。Android中的佈局權重問題LinearLayout

用戶可以不斷將這3個項目的水平LL添加到RelativeLayout的底部,在這種情況下,它充當列表。

我在setLayoutParams方法指定的權重爲每個TextView對象

TextView quantityText = new TextView(MainActivity.this); 
    quantityText.setLayoutParams(new LinearLayout.LayoutParams(
      LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1f)); 
    quantityText.setText("" + currentIngredientMeasurementQuantity); 


    TextView typeText = new TextView(MainActivity.this); 
    typeText.setLayoutParams(new LinearLayout.LayoutParams(
      LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1f)); 
    typeText.setText(currentIngredientMeasurementType); 


    TextView nameText = new TextView(MainActivity.this); 
    nameText.setLayoutParams(new LinearLayout.LayoutParams(
      LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, 4f)); 
    nameText.setText(currentIngredientName); 


    LinearLayout ll = new LinearLayout(MainActivity.this); 
    ll.setId(View.generateViewId()); 

    RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.ingredient_list_relativelayout); 

    RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(
      RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 

    //Find last member of RelativeLayout 
    View lastIngredient = (View) relativeLayout.getChildAt(totalIngredientQuantity - 1); 

    relativeParams.addRule(RelativeLayout.BELOW, lastIngredient.getId()); 
    relativeParams.setMargins(0, 0, 0, 0); 


    ll.addView(quantityText); 
    ll.addView(typeText); 
    ll.addView(nameText); 

    relativeLayout.addView(ll, relativeParams); 

的問題是,所述TextViews沒有被顯示的I希望他們的方式。我希望三個項目中的每一個垂直排列,就像一個正確的列表。我得到something like this。您可以看到,字符串的長度以某種方式影響了佈局的存儲/顯示方式。

我知道這是一大塊代碼,但任何幫助將不勝感激!

+0

添加布局的屏幕截圖。會有幫助。 –

+0

你爲什麼不試試Weight –

+0

@KNeerajLal我在帖子 –

回答

0

你必須將它們添加到LinearLayout之前,您TextViews設置Gravity到左(或啓動),就像這樣:

LinearLayout.LayoutParams name_lp = new LinearLayout.LayoutParams(
    LinearLayout.LayoutParams.WRAP_CONTENT, 
    LinearLayout.LayoutParams.WRAP_CONTENT 
); 
name_lp.gravity = Gravity.LEFT; 
nameText.setLayoutParams(name_lp); 
-1

試圖告訴你你的Textview s到使用所有可用空間他們可以得到。所以他們會填寫整個LinearLayout

代替WRAP_CONTENT使用MATCH_PARENTFILL_PARENT

在XML中,你可以使用0dip喜歡這裏描述:In android layouts what is the effect/meaning of layout_height="0dip"

另一個想法是使用TableLayout或網格佈局http://developer.android.com/reference/android/widget/GridLayout.html http://developer.android.com/reference/android/widget/TableLayout.html

樂趣

0

試試這個: -

與其使用的動態創建控件創建一個自定義佈局

custom.xml

<LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:weightSum="90"> 
     <TextView 
      android:id="@+id/textview1" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="30" 
      android:gravity="center" 
      android:text="text1"/> 
     <TextView 
      android:id="@+id/textview2" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:gravity="center" 
      android:layout_weight="30" 
      android:text="text2"/> 
     <TextView 
      android:id="@+id/textview3" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:gravity="center" 
      android:layout_weight="30" 
      android:text="text3"/> 
    </LinearLayout> 

加入這一觀點在RelativeLayout的

View view = factory.inflate(R.layout.custom, null); 
relativeLayout.addView(view); 
0

您需要設置layout_width屬性0layout weight財產好好工作。我做了一對夫婦來改變你的代碼,以使它適合我。

試試這個,

private void addNewRow(float currentIngredientMeasurementQuantity, String currentIngredientMeasurementType, String currentIngredientName) { 

    TextView quantityText = new TextView(MainActivity.this); 
    quantityText.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1f)); 
    quantityText.setText("" + currentIngredientMeasurementQuantity); 

    TextView typeText = new TextView(MainActivity.this); 
    typeText.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1f)); 
    typeText.setText(currentIngredientMeasurementType); 

    TextView nameText = new TextView(MainActivity.this); 
    nameText.setLayoutParams(new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 4f)); 
    nameText.setText(currentIngredientName); 

    LinearLayout ll = new LinearLayout(MainActivity.this); 
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN) { 
     ll.setId(View.generateViewId()); 
    } else { 
     ll.setId(totalIngredientQuantity); 
    } 

    RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.ingredient_list_relativelayout); 

    RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 

    //Find last member of RelativeLayout 
    View lastIngredient = (View) relativeLayout.getChildAt(totalIngredientQuantity - 1); 

    if (lastIngredient != null) { 
     relativeParams.addRule(RelativeLayout.BELOW, lastIngredient.getId()); 
     relativeParams.setMargins(0, 0, 0, 0); 
    } 

    ll.addView(quantityText); 
    ll.addView(typeText); 
    ll.addView(nameText); 

    relativeLayout.addView(ll, relativeParams); 
}