【问题标题】:Android - Dynamically added linearlayout not wrapping to height of children added later, dynamicallyAndroid - 动态添加的线性布局不包装到稍后添加的孩子的高度,动态
【发布时间】:2014-03-29 00:05:36
【问题描述】:

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

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

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

动态添加代码

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>

【问题讨论】:

    标签: android dynamic android-linearlayout


    【解决方案1】:

    我是个白痴。

    我的子视图包裹到父级,父级包裹到子级,所以它仍然保持原来的大小。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多