繁体   English   中英

Android-动态添加的linearlayout不会自动调整到后来添加的子代的高度

[英]Android - Dynamically added linearlayout not wrapping to height of children added later, dynamically

我将7个线性布局添加到垂直线性布局中,并且在每个线性布局中,我遍历游标并为游标的每一行添加一个垂直具有两个textview的视图。

一切工作正常,除了线性布局未包装到子视图的内容。

我有一种感觉是因为我在添加子视图之前在layoutparams中设置了wrap_content,但是我只是在将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>

我是个白痴。

我的孩子视图包装到父级,而父视图包装到孩子,所以它仍然保持原始大小。

在高度上将子级更改为wrap_content,并且一切正常。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM