why marginBottom not working?

后端 未结 8 1532
萌比男神i
萌比男神i 2020-12-23 19:47



        
相关标签:
8条回答
  • 2020-12-23 20:31

    I am not sure in which revision of android you are experiencing this issue. I looked at RelativeLayout in Android 4.2.2 (https://android.googlesource.com/platform/frameworks/base/+/android-4.2.2_r1.1/core/java/android/widget/RelativeLayout.java) and I believe there is a bug in the onMeasure.

    In lines 486 to 488, we have following code:

    if (isWrapContentHeight) {
        height = Math.max(height, params.mBottom);
    }
    

    In my opinion, the above should read:

    if (isWrapContentHeight) {
        height = Math.max(height, params.mBottom + params.bottomMargin);
    }
    

    With android:layout_height="wrap_content", RelativeLayout does not appear take into account the bottomMargin of the last vertically laid out view.

    I suggest you try one of the following:

    *) Add a dummy view that will be the very last vertically laid out view, and ensure that it has the android:layout_below attribute. Note that the dummy view does not have a bottom margin, it is there so that RelativeLayout considers the bottom margin of views laid out above it. For your case, the dummy view would look like this:

    <View
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_below="@id/messageSender"
    />
    

    *) As others have mentioned, achieve the same effect in other ways, such as Padding.

    *) A not-so-trivial solution is bring in RelativeLayout, its platform style definitions and Pool management classes into your project, perform the bug fix I mentioned above and use this anywhere you would normally use RelativeLayout.

    *) File a bug with Google

    Hope this helps.

    0 讨论(0)
  • 2020-12-23 20:32

    marginBottom has no effect if you set android:layout_height="wrap_content" for <RelativeLayout>, instead set it as match_parent and check.

    0 讨论(0)
提交回复
热议问题