Custom Draw Line Between Two Views Not Working Properly

萝らか妹 提交于 2019-12-02 03:17:33

Currently, you are adding MatchTheColumnDrawViews programmatically to MatchTheFollowingAttempted without setting LayoutParams. There are default values for LayoutParams, so with just one instance of MatchTheFollowingAttempted you were simply lucky.

So to fix the problem you have to set LayoutParams to the MatchTheColumnDrawViews, for example like this:

for (int i = 0; i < numberOfOneSideButtons; i++) {

    MatchTheColumnDrawView matchTheColumnDrawView = new MatchTheColumnDrawView(
                       mContext, leftSideButtons.get(i), 
                       rightSideButtons.get(numberOfOneSideButtons - 1 - i),
                       null, null,
                       2.0f, MatchTheColumnDrawView.LEFT_TO_RIGHT);

    RelativeLayout.LayoutParams layoutParams = new LayoutParams(
                                      ViewGroup.LayoutParams.MATCH_PARENT, 
                                      ViewGroup.LayoutParams.MATCH_PARENT);
    layoutParams.addRule(ALIGN_TOP, 
                  leftSideButtons.get(0).getId());
    layoutParams.addRule(ALIGN_BOTTOM, 
                  leftSideButtons.get(numberOfOneSideButtons - 1).getId());
    matchTheColumnDrawView.setLayoutParams(layoutParams);

    matchTheColumnDrawViewArrayList.add(matchTheColumnDrawView);

    addView(matchTheColumnDrawView);
}

If your left side and right side Buttons have very varying heights then you will have to find a better formula but the idea is to request as much space as possible but not the whole screen (else the next custom View will not be shown at all), so try to align the MatchTheColumnDrawView bottom to the lowest Button bottom.

P.S. How did I find out?

Using Android Studio's Layout Inspector on your View (BTW thanks for posting the full code!) showed that the "missing" Views actually had a height of 0.

Overriding onMeasure() in MatchTheColumnDrawView to log the measured width and height confirmed this: RelativeLayout does two layout passes: having a height of 0 after the first pass may happen, but after the second pass the View should have some height > 0 or it will not be shown.

EDIT:

In MatchTheFollowingAttempted, you set a padding to each of the Buttons The green circles will be drawn exactly in the middle of the View's edge if you subtract the padding when calculating the mid height values, e.g.

float endViewMidHeightY = endView.getY() + endView.getHeight() / 2 - 10;

Because a Button has its own padding (it looks smaller than it actually is), the circles still do not overlap the visible Button edge. To achieve this, either use your own Button background or (kind of hacky because the padding may change in future Android versions) subtract the padding from the x coordinate (left side) respectively add the padding to the x coordinate (right side) .

The screenshot below shows an example with some custom Views and some Buttons:

Hi try the following code

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="50dp">

 <Button
    android:layout_weight="1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<View
    android:layout_weight="5"
    android:layout_width="match_parent"
    android:background="#313131"
    android:layout_gravity="center_vertical"
    android:layout_height="3dp"/>

<Button
    android:layout_weight="1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" /></LinearLayout>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!