我的程序的工作方式是我为3个项目输入用户输入,使用这三个项目以编程方式创建水平LinearLayout
。然后将LinearLayout
放在XML中定义的RelativeLayout
内。
用户可以继续将这些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
没有按照我希望的方式显示。我希望这三个项目中的每一个都垂直排列,就像一个正确的列表。
相反,我得到something like this。您可以看到字符串的长度以某种方式影响布局的存储/显示方式。
我知道这是一大堆代码,但是任何帮助都会受到赞赏!
答案 0 :(得分:0)
您需要在Gravity
上将TextViews
设置为左(或开始),然后再将其添加到LinearLayout
,就像这样:
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 :(得分: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);
答案 2 :(得分:0)
您需要将layout_width
属性设置为0
才能使layout 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);
}
答案 3 :(得分:-1)
尝试告诉您的Textview
使用他们可以获得的所有可用空间。所以他们将填满整个LinearLayout
。
而不是WRAP_CONTENT
使用MATCH_PARENT
或FILL_PARENT
在XML中,你可以像这里描述的那样使用0dip:In android layouts what is the effect/meaning of layout_height="0dip"
另一个想法是使用TableLayout或GridLayout http://developer.android.com/reference/android/widget/GridLayout.html http://developer.android.com/reference/android/widget/TableLayout.html
玩得开心