Android - 动态地添加了线性布局,而不是包装到以后添加的子项的高度

时间:2014-03-26 11:00:14

标签: android dynamic android-linearlayout

我将7个线性布局添加到垂直线性布局中,并且在每个布局中,我循环遍历光标并为光标的每一行添加垂直两个文本视图的视图。

除了线性布局没有包装到子视图的内容之外,一切正常。

我有一种感觉,因为我在添加子视图之前在layoutparams中设置了wrap_content,但我在将layoutlay添加到主视图之前更新了linearlayouts layoutparams并且它仍然没有换行。

动态添加代码

while(c.moveToNext()) {
    if(!(c.getString(c.getColumnIndex("date")).equals(startDate))) {
        if(thisLin != null) {
            LayoutParams thisLinParams = new LayoutParams(
                LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
            thisLin.setLayoutParams(thisLinParams);
            seven_wrapper.addView(thisLin);
        }
        thisLin = new LinearLayout(getActivity());

        // originally I was setting layoutparams here but I removed this
        // when I thought it wasn't wrapping properly as there were no child views
        // at this stage
        //LayoutParams thisLinParams = new LayoutParams(
        //  LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        //thisLin.setLayoutParams(thisLinParams);

        TextView t = new TextView(getActivity());
        t.setText(c.getString(c.getColumnIndex("date")));
        LayoutParams lparams = new LayoutParams(
        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        t.setLayoutParams(lparams);
        thisLin.addView(t);
    }

    sevenItem = inflater.inflate(R.layout.seven_item, container, false);

    TextView time = (TextView) sevenItem.findViewById(R.id.seven_item_time);
    TextView name = (TextView) sevenItem.findViewById(R.id.seven_item_name);
    time.setText(c.getString(c.getColumnIndex("time")));
    name.setText(c.getString(c.getColumnIndex("name")));
    thisLin.addView(sevenItem);

}
seven_wrapper.addView(thisLin);

c.close();

片段视图

<HorizontalScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
    android:layout_height="match_parent">

<LinearLayout
android:id="@+id/seven_day_rows_wrapper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"/>
</HorizontalScrollView>

儿童观点

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

<TextView android:id="@+id/seven_item_name"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@id/seven_item_time"/>
</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

我是个白痴。

我的子视图已包装到父级,父级包装到子级,因此它仍然保持原始大小。

将孩子改为高度的wrap_content,这一切都有效。