【问题标题】:Custom View not appearing in TableRow dynamically自定义视图未动态出现在 TableRow 中
【发布时间】:2017-10-27 18:08:17
【问题描述】:

在尝试动态创建TableLayout 后,在TableRow 中膨胀我的一个布局时出现问题。文本视图以正确的布局权重按预期显示,但自定义视图(包含不同大小正方形的图形)没有。相反,我看到的是一个空白项目,但布局权重正确。 BWSquares 是我要膨胀的自定义视图。

public class MyFragment extends android.support.v4.app.Fragment {

    private TableLayout tblLayout;

    public MyFragment() {}

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.my_relativelayout, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        View v = getView();
        assert v != null;

        RelativeLayout relativelayout = v.findViewById(myRelativeLayout);

        RelativeLayout.LayoutParams rlptblLayout = new RelativeLayout.LayoutParams(GridLayout.LayoutParams.MATCH_PARENT, GridLayout.LayoutParams.WRAP_CONTENT);


        tblLayout = new TableLayout(getActivity());
        TableRow tr = new TableRow(getActivity());
        tr.setLayoutParams(new TableLayout.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
        tr.setWeightSum(1); //total row weight

        TableRow.LayoutParams params1 = new TableRow.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT, 0.06f);
        TextView tvR1 = new TextView(getActivity());
        tvR1.setText("Lorem ipsum dolor sit amet.");
        tvR1.setLayoutParams(params1);


        TableRow.LayoutParams params2 = new TableRow.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT, 0.79f);
        frameLayoutT1 = new FrameLayout(getActivity());
        BWSquares myDrawing = new BWSquares(getActivity());
        frameLayoutT1.setLayoutParams(params2);
        frameLayoutT1.addView(myDrawing);


        TableRow.LayoutParams params3 = new TableRow.LayoutParams(0, TableRow.LayoutParams.WRAP_CONTENT, 0.15f);
        TextView tvR2 = new TextView(getActivity());
        tvR2.setText("Lorem ipsum dolor sit amet.");
        tvR2.setLayoutParams(params3);

        tr.addView(tvR1);
        tr.addView(frameLayoutT1);
        tr.addView(tvR2);
        tblLayout.addView(tr);

        tblLayout.setId(View.generateViewId());

        tblLayout.setLayoutParams(rlptblLayout);

        relativelayout.addView(tblLayout);

        super.onActivityCreated(savedInstanceState);
    }
}

BWSquares.java

public class BWSquares extends View {
    private final TextPaint mTextPaint;

    public static final int BOXES_COUNT = 7;

    private float oneDp;
    private float windowHeight;

    public BWSquares(Context context) { this(context, null); }

    public BWSquares(Context context, AttributeSet attrs) {
        super(context, attrs);
        oneDp = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1,
                getResources().getDisplayMetrics());
        windowHeight = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10,
                getResources().getDisplayMetrics());
        float textSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 15,
                getResources().getDisplayMetrics());

        Paint mBlackPaint = new Paint();
        mBlackPaint.setAntiAlias(true);
        mBlackPaint.setColor(Color.BLACK);
        mBlackPaint.setStrokeWidth(oneDp);
        mBlackPaint.setStyle(Paint.Style.STROKE);

        mTextPaint = new TextPaint(TextPaint.ANTI_ALIAS_FLAG);
        mTextPaint.setColor(Color.BLACK);
        mTextPaint.setTextAlign(Paint.Align.CENTER);
        mTextPaint.setTextSize(textSize);
    }

    private Paint mWindowPaint;
    private Paint mWindowFilledPaint;

    RectF rect = new RectF();
    RectF rect2 = new RectF();

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        if (getWidth() == 0) return;

        float mSideRectWidth = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 5, getResources().getDisplayMetrics());

        setBackgroundColor(Color.parseColor("#808080"));

        for (int i = 0; i < BOXES_COUNT; i++) {
            float leftPosition = mSideRectWidth
                    + i * oneDp
                    + (getWidth() - mSideRectWidth * 2 - (BOXES_COUNT - 1) * oneDp) * i / BOXES_COUNT;
            float rightPosition = mSideRectWidth
                    + i * oneDp
                    + (getWidth() - mSideRectWidth * 2 - (BOXES_COUNT - 1) * oneDp) * (i + 1)
                    / BOXES_COUNT;
        }

        for (int i = 1; i < BOXES_COUNT; i++) {
            float position = mSideRectWidth + (getWidth() - mSideRectWidth * 2) * i / BOXES_COUNT;
            canvas.drawLine(position, 0, position, getHeight(), mGangwayConnectionPaint);
        }
    }

    private void fillRect(Canvas canvas, float left, float right, String number) {
        rect.set(left + oneDp / 2, 0 + oneDp / 2, right - oneDp / 2, getHeight() - oneDp / 2);
        float xPos = left + ((right - left) / 2);
        float yPos = (canvas.getHeight() / 2) - ((mTextPaint.descent() + mTextPaint.ascent()) / 2);
        canvas.drawText(number, xPos, yPos, mTextPaint);
    }    
}

【问题讨论】:

    标签: java android android-relativelayout android-framelayout


    【解决方案1】:

    尝试将setWillNotDraw(false) 添加到您的构造函数中:

    public BWSquares(Context context, AttributeSet attrs) {
        super(context, attrs);
        setWillNotDraw(false);
        ...
    }
    

    【讨论】:

    • 我发现了问题,FrameLayout 不需要在那里。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-19
    • 1970-01-01
    • 1970-01-01
    • 2021-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多