program story

Android에서 마이너스 마진을 사용하는 것이 나쁜 습관입니까?

inputbox 2020. 8. 9. 10:25
반응형

Android에서 마이너스 마진을 사용하는 것이 나쁜 습관입니까?


마이너스 마진 데모 :

                         여기에 이미지 설명 입력

시나리오

다른 뷰의 경계 상자를 침범하도록 뷰 중 하나에 음수 여백을 설정하여 뷰를 겹칩니다.

생각

레이아웃이 겹치는 경우 예상대로 작동하는 것 같습니다. 하지만 무의식적으로 일을 제대로하지 않는다는 이유로 더 큰 문제에 부딪 히고 싶지 않습니다. 에뮬레이터, 물리적 장치, 네거티브 여백을 사용하면 모든 것이 올바르게 작동하는 것처럼 보이며 한 뷰가 다른 뷰 경계 상자를 침범하고 레이아웃에서 선언 된 방식에 따라 다른 뷰의 위 또는 아래에 있습니다.

나는 또한 API (21) 이후 우리가를 설정할 수 있습니다 알고 있어요 translationZelevation보기 위 또는 다른 뷰 아래에 표시 할 속성을하지만 내 관심사는 기본적으로는 사실에서 오는 문서에 에 대한 layout_margin특성이 명확하게 지정된 것 여백 값이 양수이어야한다 ,하자 나 인용 :

발췌 :
이보기의 왼쪽, 위쪽, 오른쪽 및 아래쪽에 추가 공간을 지정합니다. 이 공간은이 뷰의 범위 밖에 있습니다. 여백 값은 양수 여야합니다 . "14.5sp"와 같은 단위가 추가 된 부동 소수점 숫자 인 차원 값이어야합니다. 사용 가능한 단위 : px (픽셀), dp (밀도 독립 픽셀), sp (기본 글꼴 크기에 따라 조정 된 픽셀), in (인치), mm (밀리미터) ...

원래이 질문을 한 후 몇 년 동안 나는 마이너스 마진에 대한 문제가 없었고 가능한 한 많이 사용하지 않으려 고 노력했지만 문제가 발생 하지 않았 으므로 문서에 나와 있지만 나도 그렇지 않습니다. 그것에 대해 걱정했습니다.


2010 년 @RomainGuy (핵심 Android 엔지니어)는 마이너스 마진이 지정되지 않은 동작을 가졌다 고 말했습니다 .

2011 년 @RomainGuy는 및에서 마이너스 여백을 사용할 수LinearLayoutRelativeLayout 있다고 말했습니다 .

2016 년 @RomainGuy는 공식적으로 지원 한 적이 없으며ConstraintLayout .

하지만이 제한을 해결하는 것은 쉽습니다.

기본보기 하단에 도우미보기 (높이 0dp, 부모에 제한되는 너비)를 추가하고 하단에 원하는 여백을 추가합니다.
그런 다음 뷰를이 뷰 아래에 배치하여 지원되지 않는 음수 값을 사용할 필요없이 "음수"여백을 효과적으로 허용합니다.


음수 여백을 사용하려면 컨테이너와 clipToPadding대해 충분한 패딩 을 false로 설정하고 하위 여백을 음수 여백으로 설정하여 자식보기를 자르지 않도록합니다!


이것이 누군가를 도울 수 있기를 바랍니다. 다음은 ConstraintLayout@CommonsWare의 답변 기반으로 사용하는 샘플 코드입니다 .

기본보기 하단에 도우미보기 (높이 0dp, 부모에 제한되는 너비)를 추가하고 하단에 원하는 여백을 추가합니다. 그런 다음 뷰를이 뷰 아래에 배치하여 지원되지 않는 음수 값을 사용할 필요없이 "음수"여백을 효과적으로 허용합니다.

샘플 코드 :

<TextView
    android:id="@+id/below"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#F1B36D"
    android:padding="30dp"
    android:text="I'm below"
    android:textColor="#ffffff"
    android:textSize="48sp"
    android:textAlignment="center"
    tools:layout_editor_absoluteX="129dp"
    tools:layout_editor_absoluteY="0dp" />

<android.support.v4.widget.Space
    android:id="@+id/space"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginBottom="32dp"
    app:layout_constraintBottom_toBottomOf="@+id/below"
    app:layout_constraintLeft_toLeftOf="@id/below"
    app:layout_constraintRight_toRightOf="@id/below" />

<TextView
    android:id="@+id/top"
    android:layout_width="100dp"
    android:layout_height="60dp"
    android:textAlignment="center"
    android:textColor="#ffffff"
    android:text="I'M ON TOP!"
    android:background="#676563"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/space" />

산출:

여기에 이미지 설명 입력


It might have been bad practice in the past but with Material Design and its floating action buttons, it seems to be inevitable and required in many cases now. Basically, when you have two separate layouts that you can't put into a single RelativeLayout because they need distinctly separate handling (think header and contents, for instance), the only way to overlap the FAB is to make it stick out of one those layouts using negative margins. And this creates additional problems with clickable areas.


For me, and regarding setting a negative margin on a TextView (I realize the OP is referring to a ViewGroup, but I was looking for issues with setting negative margins and I landed here)... I found a problem with 4.0.3 (API 15) ONLY and the setting of android:layout_marginTop or android:layout_marginBottom to a negative value such as -2dp.

For some reason the TextView does not display at all. It appears to be "gone" from the view (not just invisible).

When I tried this with the other 3 versions of layout_margin, I didn't see the issue.

Note that I haven't tried this on a real device, this is using a 4.0.3 emulator. This is the 2nd odd thing I've found that only affected 4.0.3, so my new rule is to always test with a 4.0.3 emulator :)

I have success with reducing the bottom margin of a TextView by using android:lineSpacingExtra="-2dp" which works even though I happen to have android:singleLine="true" (and so I wouldn't have thought that line spacing would be a factor).


아니요, negative margin. 대신 translate. 언젠가 음수 여백이 작동하더라도 프로그래밍 방식으로 레이아웃을 변경하면 번역이 도움이 될 것입니다. 그리고 여백을 사용하면 화면이 넘칠 수 없습니다.


나는 그것이 짧은 시간 동안 만 가능하다는 것을 알고있었습니다. 그러나 나는 그것에 문제가 없다고 본다. 화면 크기 등에 유의하여 화면에 겹쳐서 표시되지 않아야하는 항목을 실수로 만들지 않도록하십시오. (즉, 텍스트 위에있는 텍스트는 나쁜 생각 일 수 있습니다.)

참고 URL : https://stackoverflow.com/questions/10673503/is-it-a-bad-practice-to-use-negative-margins-in-android

반응형