自定义ViewGroup无法显示childView

自定义两个ViewGroup做嵌套ViewGroup A 嵌套ViewGroup B 而B的中的childView(TextView)无法显示,具体看代码:

  1. 父VewGroup
public class ViewGropTest extends ViewGroup{

    public ViewGropTest(Context context) {
        super(context,null);

    }
    public ViewGropTest(Context context, AttributeSet attrs) {
        super(context,attrs);
    }


    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        final int width  = getMeasuredWidth();
        final int height = getMeasuredHeight();
        final int  childLeft   = getPaddingLeft();
        final int  childTop    = getPaddingTop();
        final int  childWidth  = width - getPaddingLeft() - getPaddingRight();
        final int  childHeight = height - getPaddingTop() - getPaddingBottom();
        int childCount = this.getChildCount();
        for (int i = 0; i < childCount; i++) {
            View child = this.getChildAt(i);
            child.layout(childLeft, childTop, childLeft + childWidth, childTop + childHeight);
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        final int count = getChildCount();
        for (int i = 0; i < count; i++) {
            View child = this.getChildAt(i);
            this.measureChild(child, widthMeasureSpec, heightMeasureSpec);
            int cw = child.getMeasuredWidth();
            // int ch = child.getMeasuredHeight();
        }
    }
}
  1. 子ViewGroup
public class NewTextView extends ViewGroup{

    private Animation.AnimationListener mListener;

    public NewTextView(Context context) {
        super(context);
        TextView textView = new TextView(context);
        textView.setText("HAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHA");
        textView.setTextSize(20, TypedValue.COMPLEX_UNIT_SP);
        textView.setTextColor(Color.BLACK);
        textView.setGravity(Gravity.CENTER);
        LinearLayout linearLayout = new LinearLayout(context);
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        linearLayout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        linearLayout.addView(textView);
        addView(linearLayout,0);
        setBackgroundColor(0x567866);
//        setBackgroundResource(R.mipmap.ic_launcher);
    }


    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {


        int count = this.getChildCount();

        for(int i = 0;i < count;i++){
            View child = this.getChildAt(i);
            child.setVisibility(View.VISIBLE);
            int x = l;
            int y = t;
            child.layout(l,t,r,b);
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        final int count = getChildCount();
        for (int i = 0; i < count; i++) {
            View child = this.getChildAt(i);
            child.measure(widthMeasureSpec, heightMeasureSpec);
        }
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint paint = new Paint();
        paint.setColor(Color.BLACK);
        paint.setTextSize(30);
        canvas.drawText("FUCK",50,50,paint);
    }

    public void setAnimationListener(Animation.AnimationListener listener) {
        mListener = listener;
    }

    @Override
    public void onAnimationStart() {
        super.onAnimationStart();
        if (mListener != null) {
            mListener.onAnimationStart(getAnimation());
        }
    }

    @Override
    public void onAnimationEnd() {
        super.onAnimationEnd();
        if (mListener != null) {
            mListener.onAnimationEnd(getAnimation());
        }
    }
}
  1. 主视图
public class ViewGroupFragment extends BaseFragment {
    @BindView(R.id.viewgroup)
    ViewGropTest viewgroup;

    @Override
    protected int getLayout() {
        return R.layout.view_test_viewgroup;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        NewTextView textView = new NewTextView(getContext());
        viewgroup.addView(textView);
    }
}

结果只显示子ViewGroup中onDraw方法绘制的文本F**K。
怎么才能让子ViewGroup中TextView显示?还是对于ViewGroup我使用方式不对?

阅读 5k
1 个回答

自己带自己入坑了,setTextSize有问题 typeValue SP不能显示 具体为什么没做研究。
另外onLayout方法也重写了和父ViewGroup一致,如果子ViewGroup不在父ViewGroup下则也不会显示TextView的内容

public  class NewTextView extends ViewGroup{

    private Animation.AnimationListener mListener;

    public NewTextView(Context context) {
        this(context,null);

//        setBackgroundResource(R.mipmap.ic_launcher);
    }
    public NewTextView(Context context,AttributeSet attr){
        super(context,attr);
        TextView textView = new TextView(context);
        textView.setText("HAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHA");
        textView.setTextSize(20);
        textView.setTextColor(Color.BLACK);
        textView.setGravity(Gravity.CENTER);
        LinearLayout linearLayout = new LinearLayout(context);
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        linearLayout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        linearLayout.addView(textView);
        addView(linearLayout,0);
        setBackgroundColor(0x567866);
    }


    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        final int width  = getMeasuredWidth();
        final int height = getMeasuredHeight();
        final int  childLeft   = getPaddingLeft();
        final int  childTop    = getPaddingTop();
        final int  childWidth  = width - getPaddingLeft() - getPaddingRight();
        final int  childHeight = height - getPaddingTop() - getPaddingBottom();
        int count = this.getChildCount();
        for(int i = 0;i < count;i++){
            View child = this.getChildAt(i);
            child.layout(childLeft, childTop, childLeft + childWidth, childTop + childHeight);
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//        setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
        final int count = getChildCount();
        for (int i = 0; i < count; i++) {
            View child = this.getChildAt(i);
            child.measure(widthMeasureSpec, heightMeasureSpec);
        }
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint paint = new Paint();
        paint.setColor(Color.BLACK);
        paint.setTextSize(30);
        canvas.drawText("FUCK",50,50,paint);
    }

    public void setAnimationListener(Animation.AnimationListener listener) {
        mListener = listener;
    }

    @Override
    public void onAnimationStart() {
        super.onAnimationStart();
        if (mListener != null) {
            mListener.onAnimationStart(getAnimation());
        }
    }

    @Override
    public void onAnimationEnd() {
        super.onAnimationEnd();
        if (mListener != null) {
            mListener.onAnimationEnd(getAnimation());
        }
    }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题