Reputation: 996
I'm trying to inflate 5 vertical LinearLayout
in one custom horizontal LinearLayout
.
First I covert the xml to a View
using the LayoutInflater
. Then I add the vertical layouts to the horizontal one.
The problem is that in onMeasure
, I try to set the width and the height of the children vertical LinearLayout
but then, when I ask for getWidth
and getHeight
it always return 0.
I really don't understand this behaviour.
Can you please help me. Thank you so much.
XML of the vertical LinearLayout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#000000" >
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:text="Button"
android:layout_weight="1" />
<com.vuric.nativesampler.customviews.BandSeekBar
android:id="@+id/bandSeek"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="8"
android:layout_gravity="center"/>
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="Button" />
</LinearLayout>
Class of the container horizontal LinearLayout:
public class EqualizerCustomLayout extends LinearLayout {
private static final int MARGIN = 4;
private Context context;
public EqualizerCustomLayout(Context context) {
super(context);
this.context = context;
init();
}
public EqualizerCustomLayout(Context context, AttributeSet attrs,
int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.context = context;
init();
}
public EqualizerCustomLayout(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
init();
}
private void init() {
setOrientation(LinearLayout.HORIZONTAL);
setLayoutParams(new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
//getNumberOfBands is 5
for (int i = 0; i < CustomApplicationClass.get().getNumberOfBands(); ++i) {
LinearLayout linearLayout = (LinearLayout) inflate(context,
R.layout.slot_band_layout, null);
addView(linearLayout);
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Log.v("jajaja", "onMeasure children are " + getChildCount());
int width = (CustomApplicationClass.get().getScreenSize().x / 100 * 45)
+ MARGIN * 2;
int height = (CustomApplicationClass.get().getScreenSize().x / 100 * 45)
+ MARGIN * 2;
LayoutParams params = new LinearLayout.LayoutParams(width / 5, width);
for (int i = 0; i < getChildCount(); ++i) {
LinearLayout layout = (LinearLayout) getChildAt(i);
layout.setLayoutParams(new LinearLayout.LayoutParams(width
/ getChildCount(), height));
Log.v("jajaja",
"view" + i + " , w: " + layout.getWidth() + " , h: "
+ layout.getHeight()); // always return 0 for both width and height
}
setMeasuredDimension(width, height);
}
}
Upvotes: 0
Views: 629
Reputation: 3777
You have to wait until the view is actually drawn. There is a workaround that can be found here.
Upvotes: 1