0
我試圖在一個定製水平LinearLayout
中對5個垂直LinearLayout
充氣。在水平LinearLayout中填充垂直線性佈局返回寬度和高度0
首先,我使用LayoutInflater
將xml轉換爲View
。然後我將垂直佈局添加到水平佈局。
的問題是,在onMeasure
,我嘗試設置寬度和孩子的垂直LinearLayout
的高度,但是,當我問getWidth
和getHeight
它總是返回0。
我真的不明白這種行爲。
你能幫我嗎。非常感謝。
垂直的LinearLayout的XML:
<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>
類容器水平的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);
}
}
我在想,在OnMeasure()我必須分配兒童的維度......是不是真的? –
指定作品,但我相信你必須等到以後再讀取價值。 –
問題是我看不到我添加的5個LinearLayouts –