タイトルは基本的にはそうです。私はLinearLayout
を拡張するクラスを持っていて、そのLinearLayoutに子を持つようにします。TextView
問題は、TextViewが表示されないようです。LinearLayoutにプログラムで追加するとTextViewが表示されません
これまでのところ、私は間違っていますか?
更新:私は君たちが示唆されているとして、次のように私のコードを変更し、そして私のTextView
それでも表示されない....
public class CalendarCourseView extends LinearLayout {
private int height;
private int topMargin;
private Course course;
public CalendarCourseView(Context context, Course course, int topMargin,
int height) {
super(context);
final DisplayMetrics displayMetrics = getContext().getResources().getDisplayMetrics();
this.topMargin = (int) TypedValue
.applyDimension(TypedValue.COMPLEX_UNIT_DIP, topMargin, displayMetrics);
this.height = (int) TypedValue
.applyDimension(TypedValue.COMPLEX_UNIT_DIP, height, displayMetrics);
this.course = course;
this.setBackgroundColor(course.getColor());
setTextView();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec), this.height);
((MarginLayoutParams) getLayoutParams()).topMargin = topMargin;
}
private void setTextView() {
TextView textView = new TextView(this.getContext());
LayoutParams params = new LayoutParams(new LinearLayout.LayoutParams(ViewGroup
.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
textView.setLayoutParams(params);
textView.setText(course.getName());
this.addView(textView);
}
}
アップデート: I、ピンポイントの問題。それはonMeasureと一緒です。 TextViewがheightとtopMarginの変更後にLinearLayoutと同じ位置に配置されていないと思います。
更新: onMeasureでsuper.onMeasureを呼び出すだけで簡単に修正されました。
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, this.height);
((MarginLayoutParams) getLayoutParams()).topMargin = topMargin;
}
私はsetMeasuredDimension(int, int)
のみを表示していない子供の寸法が変化するため、それがあると信じています。私はonLayout
を上書きしなければならなかったでしょう。 super.onMeasure
を呼び出すと、子供と簡略化されたものも変更されます。
: http://stackoverflow.com/questions/13394181/inflated-children-of-custom-linearlayout-dont-show-when-overriding-onmeasure –
'onMeasure'は通常2回以上呼び出されます。そこにレイアウトのものを追加するのは、通常はバッドアイデアです。そして、あなたは 'MeasureSpec 'を使うべきです。getSize(widthMeasureSpec) 'を呼び出すだけではなく、' widthMeasureSpec'を渡すだけではありません。 –
@FlorentSpahiu私はまだ、TextViewがどのように表示されないのかは分かりません。 LinearLayoutはうまく膨らんでいます... – Pants