我的程序工作的方式是我接受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。您可以看到,字符串的長度以某種方式影響了佈局的存儲/顯示方式。
我知道這是一大塊代碼,但任何幫助將不勝感激!
添加布局的屏幕截圖。會有幫助。 –
你爲什麼不試試Weight –
@KNeerajLal我在帖子 –