Reputation: 1208
I'm having problems when adding views programmatically to a horizontal scroll view. I have a horizontal scroll view with a dotted background and have given the linear layouts around it clear colour backgrounds to aid with understanding.
This is what the view looks like prior
Then I add a view to the scroll view after a drag and drop event:
MemoNodeView mn = new MemoNodeView(this);
mn.setMemo(m);
int time = (int) Math.round(pixelsPerMilliSecond()*m.getTimeLength());
mn.setMemoSize(time, timelineHeight);
timeline.addView(mn, new ViewGroup.MarginLayoutParams(time, timelineHeight));
This leads to the view expanding in size vertically:
I attempted to fix this by re-setting the layout params of the linear layout inside the scroll view based on the height it was supposed to have:
ViewGroup.LayoutParams p = timeline.getLayoutParams();
p.height = timelineHeight;
timeline.setLayoutParams(p);
Leading to this situation where the scroll view is still expanded but the content is the right size:
I then tried to resize the scroll view itself so that the size matched what it was supposed to:
ViewGroup.LayoutParams p2 = timelineScrollView.getLayoutParams();
p2.height = timelineHeight;
timelineScrollView.setLayoutParams(p2);
which gave:
Leaving the size of the scroll view correct, but the size of the other views shrunken and slightly in the wrong position. What I want is for the scroll view to remain the same as in the first image and for nothing to move around. It may be something basic, but I cant for the life of me figure out what I'm doing wrong to give this behaviour.
The xml for the area is as follows (note that the colours/ text views are placeholders used to try and help make the issue stand out visually):
`<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/black_frame"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#ffffff" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Small Text" />
</LinearLayout>
<LinearLayout
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="3"
android:orientation="vertical"
android:paddingLeft="@dimen/toggle_draw_padding"
android:paddingRight="@dimen/toggle_draw_padding" >
<HorizontalScrollView
android:id="@+id/horizontal_scroll_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/timeline_dots_bitmap"
android:fillViewport="true">
<LinearLayout
android:id="@+id/timeline_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="#F0AA4444" >
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#ffffff" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Small Text" />
</LinearLayout>
</LinearLayout>`
Any ideas as to what I am doing wrong here?
Upvotes: 0
Views: 1028
Reputation: 1208
I found out what was causing the issue. I'll post here in case anyone else has a similar issue and it is also driving them mental...
The linear layouts that had the white backgrounds had the layout_height="match_parent"
which gave the weird behaviour when other views were added. Changing the two layouts to layout_height="0dip"
solves the issue. As shown below:
`
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:background="#ffffff" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Small Text" />
</LinearLayout>
<LinearLayout
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="3"
android:orientation="vertical"
android:paddingLeft="@dimen/toggle_draw_padding"
android:paddingRight="@dimen/toggle_draw_padding"
>
<HorizontalScrollView
android:id="@+id/horizontal_scroll_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/timeline_dots_bitmap"
android:fillViewport="true"
android:layout_margin="0dip"
android:padding="0dip">
<LinearLayout
android:id="@+id/timeline_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="#F0AA4444"
android:layout_margin="0dip"
android:padding="0dip">
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:background="#ffffff" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Small Text" />
</LinearLayout>
</LinearLayout>`
I hope that this can help anyone who somehow makes the same mistake I did.
Upvotes: 1