以编程方式创建UI的最佳实践

时间:2018-01-17 20:51:28

标签: android mobile view

最近我总是想要动态创建我的UI。 我找到了一种方法,但我真的不喜欢它。

例如,我想创建一个类似Dayview的GoogleCalender,最后这样做:(不要详细阅读代码,只是看到它很糟糕)

while (window.pollEvent(event))

它丑陋且不具备表现力。

所以我现在要创建的是匹配历史记录:

enter image description here

我会有一个视图,其中包含每个团队名称的2个textViews和1个用于分数的textView。这些箭头应该显示它们将如何放置在约束布局中。

我的猜测应该是如何为MatchView创建一个layout.xml,并且在xml中我有一个占位符用于其他textViews,我可以通过编程方式进行更改。然后我的MatchView类将使用此layout.xml,我的MatchView类的对象是一个MatchView。

例如:

constraintSet = new ConstraintSet();

        View nextHolder = new View(this);
        View nextMarker = new View(this);
        TextView nextTime = new TextView(this);

        holders[i] = nextHolder;
        times[i] = nextTime;
        markers[i] = nextMarker;

        int calcHour = hour + i;

        if (calcHour < 10) {
            nextTime.setText("0" + Integer.toString(calcHour) + ":00");
        } else {
            nextTime.setText(Integer.toString(calcHour) + ":00");
        }
        nextTime.setTextColor(0xFF000000);
        nextMarker.setBackgroundColor(ResourcesCompat.getColor(getResources(), R.color.grey, null));

        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
            holders[i].setId(Utils.generateViewId());
            markers[i].setId(Utils.generateViewId());
            times[i].setId(Utils.generateViewId());
        } else {
            holders[i].setId(View.generateViewId());
            markers[i].setId(View.generateViewId());
            times[i].setId(View.generateViewId());
        }

        int nextHolderID = holders[i].getId();
        int nextMarkerID = markers[i].getId();
        int nextTimeID = nextTime.getId();

        constraintLayout.addView(holders[i]);
        constraintLayout.addView(markers[i]);
        constraintLayout.addView(times[i]);

        constraintSet.clone(constraintLayout);

        //Constraint the Time-Stamp-Holders
        constraintSet.constrainHeight(nextHolderID, Utils.convertDpToPixel(40, context));
        constraintSet.constrainWidth(nextHolderID, ConstraintSet.MATCH_CONSTRAINT);
        constraintSet.connect(nextHolderID, ConstraintSet.START, ConstraintSet.PARENT_ID,
                ConstraintSet.START);
        constraintSet.connect(nextHolderID, ConstraintSet.END, ConstraintSet.PARENT_ID,
                ConstraintSet.END);
        if (i == 1) {
            constraintSet.connect(nextHolderID, ConstraintSet.TOP, startHolder.getId(),
                    ConstraintSet.BOTTOM, Utils.convertDpToPixel(8, context));
        } else {
            constraintSet.connect(nextHolderID, ConstraintSet.TOP, holders[i - 1].getId(),
                    ConstraintSet.BOTTOM, Utils.convertDpToPixel(8, context));
        }

        //Constraint the Time TextViews
        constraintSet.constrainHeight(nextTimeID, ConstraintSet.WRAP_CONTENT);
        constraintSet.constrainWidth(nextTimeID, ConstraintSet.WRAP_CONTENT);
        constraintSet.connect(nextTimeID, ConstraintSet.BOTTOM, nextHolderID,
                ConstraintSet.BOTTOM, Utils.convertDpToPixel(8, context));
        constraintSet.connect(nextTimeID, ConstraintSet.TOP, nextHolderID,
                ConstraintSet.TOP, Utils.convertDpToPixel(8, context));
        constraintSet.connect(nextTimeID, ConstraintSet.START, nextHolderID,
                ConstraintSet.START, Utils.convertDpToPixel(16, context));

        //Constraint the timeline Views
        constraintSet.constrainHeight(nextMarkerID, ConstraintSet.MATCH_CONSTRAINT);
        constraintSet.constrainWidth(nextMarkerID, ConstraintSet.MATCH_CONSTRAINT);

        constraintSet.connect(nextMarkerID, ConstraintSet.BOTTOM, nextTimeID,
                ConstraintSet.BOTTOM, Utils.convertDpToPixel(8, context));
        constraintSet.connect(nextMarkerID, ConstraintSet.TOP, nextTimeID,
                ConstraintSet.TOP, Utils.convertDpToPixel(8, context));
        constraintSet.connect(nextMarkerID, ConstraintSet.START, nextTimeID,
                ConstraintSet.END, Utils.convertDpToPixel(16, context));
        constraintSet.connect(nextMarkerID, ConstraintSet.END, nextHolderID,
                ConstraintSet.END, Utils.convertDpToPixel(16, context));

        constraintSet.applyTo(constraintLayout);

MatchView类看起来像这样:

MatchView matchView = new MatchView("Team1", "Team2", "score")

这是它的完成方式吗?有更方便的方法吗? 如果我猜对了,你有一个教程,他们会告诉我你是如何做到的吗? 我搜索了很多天但我找不到我真正想要的东西。 我希望我不要问太愚蠢的事情。

提前致谢和问候

BOLLE

0 个答案:

没有答案