I am adding 7 linear layouts to a vertical linear layout, and in each one, I am looping through a cursor and adding a view that has two textviews vertically, for each row of the cursor.
Everything is working fine, except the linear layout is not wrapping to the content of the child view.
I had a feeling it's because I was setting wrap_content in the layoutparams before adding the child views, but I updated the linearlayouts layoutparams just before adding it to the main view and it still doesn't wrap.
Dynamically adding code
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();
Fragment View
<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>
Child view
<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>
I was being an idiot.
My child view wrapped to parent, and the parent wrapped to the child, so it still remained the original size.
Changed the child to wrap_content on the height and it all worked.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.