패딩은 XML 레이아웃의 <shape>에 영향을주지 않습니다.


92

<shape>XML 파일 / 레이아웃 내 에서 선언 된 패딩을 설정하려고 합니다. 그러나 내가 설정 한 것이 무엇이든 패딩과 관련된 변경 사항은 없습니다. 다른 속성을 수정하면 UI에 효과가 나타납니다. 그러나 패딩에서는 작동하지 않습니다. 이것이 발생할 수있는 가능한 이유에 대해 조언 해 주시겠습니까?

스타일을 지정하려는 XML 셰이프는 다음과 같습니다.

 <shape xmlns:android="http://schemas.android.com/apk/res/android" 
      android:shape="rectangle">

    <stroke android:width="1dp" 
            android:color="#ffffff" 
            android:dashWidth="2dp"/>

    <solid android:color="@color/button_white_cover"/>

    <corners android:radius="11dp"/>

    <padding android:left="1dp" 
             android:top="20dp"
             android:right="20dp" 
             android:bottom="2dp"/>
 </shape>

답변:


113

마침내 패딩 문제를 해결했습니다.

따라서 여기의 패딩은 모양에 영향을주지 않습니다. 대신, 그것을 사용할 다른 드로어 블에 패딩을 적용해야합니다. 그래서 모양을 사용하는 드로어 블이 있고 거기에서 다음을 수행합니다.

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:drawable="@drawable/shape_below"/>
   <item android:top="10px" android:right="10px" android:drawable="@drawable/shape_cover"/>
</layer-list>

따라서 두 번째 <item>요소 에서 볼 수 있듯이 패딩 (위쪽과 오른쪽)을 설정했습니다. 그리고 그 후에 모든 것이 작동합니다.

감사.


안녕하세요,이 솔루션을 문제에 적용하는 방법을 이해하지 못합니다. 모양을 사용하는 버튼이 있고 adnroid : top 및 android : right 설정은 효과가 없습니다. 어떻게 해결할까요? 감사.
아이스크림

2
Lyubomyr가 말하는 것은 저에게 효과가있는 이와 같은 작업을하는 것이라고 생각합니다.하지만 모서리는 항상 불투명 한 흰색 배경으로 표시되어 표시 문제를 유발합니다 (댓글 형식에 대해 죄송합니다!). <layer-list xmlns : android = " schemas.android.com/apk/res/android "> <item> <shape android : shape ="rectangle "> <size android : width ="0dp "android : height ="45dp "android : color ="@ android : color / transparent "/> </ shape> </ item> <item android : top ="0dp "android : right ="0dp "android : bottom ="0dp "android : left ="0dp "android : drawable ="@ drawable / button_blue_shape "/> </ layer-list>
DustinB

또한 주제에서 벗어 났지만 유용 할 수 있습니다. 사용 된 드로어 블이 색상 인 경우 android : drawable = "@ color / divider"와 같이 작성할 수도 있습니다. 그러나 이것은 오래된 장치에서 중단됩니다. 2.2에서 테스트했는데 고장났습니다. 어느 수준에서 수용 할 수 있는지 확실하지 않습니다.
Pijusn

실제로 <item>자식 드로어 블을 허용합니다 (명시 적으로 문서화되지는 않았지만). 이것이 단일 xml 내에서이 동작을 달성 할 수있는 방법입니다.
Alex Cohn

3
이 대답은 7 년 후에도 정확한 것 같습니다. 도대체입니다 <shape><padding> 위해 ?
David Lord

75

코멘트를 한 부부가 언급했듯이, 가장 좋은 방법은을 래핑하는 것입니다 <shape><item>요소 대신에 거기에 패딩을 설정합니다. 그러나 아래 링크에 자세히 설명 된 다른 옵션이 있습니다. 예 :

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:left="8dp"
        android:right="8dp"
        android:top="8dp"
        android:bottom="8dp">

        <shape android:shape="rectangle">
            ...
        </shape>
    </item>
</layer-list>

필요에 따라 레이어 목록을 사용하는 대신 다음 드로어 블 유형 중 하나를 사용할 수 있습니다.

  • 레이어 목록
  • 선택자
  • 레벨 목록
  • 전이

사용 가능한 드로어 블 유형에 대한 자세한 내용은 이 Android 문서를 참조 하세요.

출처 :


그래, 당신은 언급하지 않았다<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
user924

@ user924 당신의 요점이 무엇인지 잘 모르겠습니다. 목적에
부합

3
에디션없이 복사하고 실행할 수있는 예제를 제공하는 것이 더 낫다는 것을 의미합니다. 그러나이 경우 <item>은 해결되지 않을 것입니다 (<layer-list> 안에 있어야하기 때문)
user924

2
@ user924 지금 무슨 뜻인지 알겠습니다. 문제를 지적하고 답변을 개선하는 데 도움을 주셔서 감사합니다. 코드 샘플을 업데이트하고 사용 가능한 드로어 블 유형에 대한 주석을 추가했습니다
TheIT

22

삽입을 시도 할 수 있습니다.

<?xml version="1.0" encoding="UTF-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
    android:insetBottom="2dp"
    android:insetLeft="1dp"
    android:insetRight="20dp"
    android:insetTop="20dp">

    <shape android:shape="rectangle">

        <stroke
            android:width="1dp"
            android:color="#ffffff"
            android:dashWidth="2dp" />

        <solid android:color="@color/button_white_cover" />

        <corners android:radius="11dp" />
    </shape>

</inset>

22

Lyobomyr의 대답은 작동하지만 여기에는 동일한 솔루션이 적용되지만 새로운 드로어 블을 만드는 오버 헤드가 없으므로 파일 혼란을 최소화합니다.

https://stackoverflow.com/a/3588976/578746

위의 SO 답변에 anon으로 anwser 복사 / 붙여 넣기 :

<item android:left="6dp"
      android:right="6dp">

    <shape android:shape="rectangle">
        <gradient android:startColor="#ccd0d3"
                  android:centerColor="#b6babd"
                  android:endColor="#ccd0d3"
                  android:height="1px"
                  android:angle="0"/>
    </shape>
</item>


1
이 답변 Yahel에 감사하지만 링크 전용 답변을 게시하지 마십시오. 링크 내용이 변경되거나 무효화 될 수 있으므로 항상 답변에 필요한 모든 정보를 제공하십시오.
TheIT

답 편집
야헬

9

이 문제를 다음과 같이 해결했습니다.

<shape android:shape="oval"  >
    <stroke android:width="4dp" android:color="@android:color/transparent" />
    <solid android:color="@color/colorAccent" />
</shape>

투명한 획을 추가했습니다.


영리하지만 나는 이것을 해킹이라고 생각합니다 :-/
dell116
당사 사이트를 사용함과 동시에 당사의 쿠키 정책개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.