Reputation: 1127
I have a custom view which I add child views to dynamically.
When each child is added, I need to recalculate the size of the views so they can fit the parent's width
For example: If I have a TextView:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:gravity="center"
android:padding="8dp"
android:clickable="true"
android:textColor="#666666"
android:visibility="visible"/>
If I add three of these to a horizontal LinearLayout within the xml it correctly sizes the three views so the total widths match the parent.
But I want to inflate them one at a time and add them so can reuse elsewhere in the code if we want a different number of TextViews in a layout.
When I do this, the total width is less than its parent, and I can't get the views to resize when a new one is added.
Here's where I inflate the view:
layout = (LinearLayout) inflater.inflate(R.layout.segmented_control, this);
TextView item = (TextView) inflater.inflate(R.layout.segmented_control_item, null);
Upvotes: 0
Views: 2496
Reputation: 10977
Change the width to match_parent
, i think you need to keep the weight
. Pass the parent layout to the inflater.
layout = (LinearLayout) inflater.inflate(R.layout.segmented_control, this);
TextView item = (TextView) inflater.inflate(R.layout.segmented_control_item, layout, false);
layout.addChild(item);
I'm not sure if that works right away, you might need to experiment a little with the layout paramters.
Upvotes: 0
Reputation: 782
Change your TextView layout width to fill_parent and remove the weight. I think that it should work.
Upvotes: 0