以编程方式将其添加到LinearLayout时,TextView不会出现

时间:2017-04-30 21:50:42

标签: java android android-layout

基本上标题是这样的。我有一个扩展LinearLayout的类,我希望LinearLayout内部有一个子TextView。问题是,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);
    }

}

更新:我固定了问题。这是onMeasure。我相信在高度和topMargin更改后,TextView不会与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也会改变孩子和简化事物。

2 个答案:

答案 0 :(得分:2)

检查您的代码以查看是否

  

course.getName()

有一个值。仅出于测试目的,您可以说

  

textView.setText( “MyCourseName”);

例如。

以下示例对我来说是正常的。

public class CustomView extends LinearLayout {

    public CustomView(Context context) {
        super(context);
        Log.d("CusotmView", "on constructor after super(context)");

        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("MyText");
        this.addView(textView);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        Log.d("CustomView", "onMeasure");
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}

答案 1 :(得分:0)

如上所述,从order by order_id方法调用setTextView是一个坏主意。 onMeasure并不总是被称为onMeasureonCreate。最重要的是,我不认为您正在将textView正确添加到主视图中。我会将onStart放在构造函数中,如下所示:

setTextView

我还要补充一点,您并未将public MyView(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; super.setBackgroundColor(course.getColor()); this.setTextView(); } 添加到正确的视图中。您将它添加到TextView类,而不是已创建的视图。您将需要获得对要添加布局的视图的引用。也许它看起来像这样:

super