[android] 개방형 Android 스트로크?

특정면에만 획이있는 Android 모양 개체를 만들 수 있습니까?

예 :

<stroke 
 android:width="3dip" 
 android:color="#000000"
    android:dashWidth="10dip" 
    android:dashGap="6dip" />

이 CSS와 유사합니다.

border: 3px dashed black;

한쪽에만 스트로크를 설정하려면 어떻게해야합니까? 이것이 내가 CSS에서하는 방법입니다.

border-left: 3px dashed black;

Android XML에서 어떻게 수행합니까?



답변

나는 이것으로 좋은 해결책을 얻었습니다.

<?xml version="1.0" encoding="utf-8"?>

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- This is the line -->
    <item android:top="-1dp" android:right="-1dp" android:left="-1dp">
      <shape>
            <solid android:color="@android:color/transparent" />
            <stroke android:width="1dp" android:color="#ffffff" />
      </shape>
    </item>

</layer-list>

이것은 투명한 배경 이 필요 하지만 여전히 열린 획 색상 이 필요한 경우에 잘 작동합니다 (제 경우에는 하단 선만 필요했습니다). 배경색이 필요한 경우 Maragues 답변에서와 같이 단색 모양 색상을 추가 할 수 있습니다.

1 편집

때때로 고밀도 장치의 경우 낮은 딥 값을 사용하면 매우 얇거나 보이지 않는 스트로크 또는 거리가 끝날 수 있습니다. 이것은 ListView 디바이더를 설정할 때도 발생할 수 있습니다.

가장 간단한 해결 방법은 1dp 대신 1px의 거리를 사용하는 것입니다. 이렇게하면 모든 밀도에서 선이 항상 표시됩니다. 가장 좋은 해결책은 각 밀도에 대한 차원 리소스를 생성하여 각 장치에 가장 적합한 크기를 얻는 것입니다.

편집 2

재미 있었지만 6 년 후 사용하려고했는데 Lollipop 기기에서 좋은 결과를 얻지 못했습니다.

아마도 현재 해결책은 9- 패치를 사용하는 것입니다. 안드로이드는이 문제에 대한 쉬운 해결책을 만들었어야했다.


답변

이 질문이 오래 전에 게시 된 것을 알고 있으므로이 질문을 게시 한 사람은 답변을 사용하지 않을 것이지만 다른 사람에게는 여전히 도움이 될 수 있습니다.

 <?xml version="1.0" encoding="UTF-8"?>
    <!-- inset is used to remove border from top, it can remove border from any other side-->
    <inset xmlns:android="http://schemas.android.com/apk/res/android"
        android:insetTop="-2dp"
        >
    <shape xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/rectangle">
        <stroke android:width="1dp" android:color="#b7b7b7" />
        <corners android:bottomRightRadius="5dp"  android:bottomLeftRadius="5dp"/>

        <solid android:color="#454444"/>
    </shape>
    </inset>

inset태그를 사용하고 제거하려는 측면 테두리에 음수 값을 지정하십시오.

가능한 값은 다음과 같습니다.

android:insetTop="-1dp"
android:insetBottom="-1dp"
android:insetLeft="-1dp"
android:insetRight="-1dp"


답변

두 개의 모양을 결합한 목록 레이어를 사용하여이 문제를 해결했습니다. 그 중 하나는 높이가 1dp 하단에 배치되었습니다.

optionscreen_bottomrectangle.xml :

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- This is the line -->
<item>
      <shape>
            <solid android:color="#535353" />
      </shape>
</item>
<!-- This is the main color -->
<item android:bottom="1dp">
     <shape>
           <solid android:color="#252525" />
     </shape>
</item>
</layer-list>

그런 다음 layout / main.xml 파일에서

<TextView
    android:id="@+id/bottom_rectangle"
    android:background="@drawable/optionscreen_bottomrectangle"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_below="@id/table_options"
    android:layout_above="@id/exit_bar"/>

table_options와 exit_bar 사이의 간격을 배경으로 채우고 exit_bar가 1dp 줄을 인쇄하기 직전에 있습니다. 이것은 나를 위해 속임수를 썼습니다. 다른 사람에게 도움이되기를 바랍니다.

올바른 순서로 레이어를 배치하기 위해 편집 된 답변. 알렉스!


답변

대신 패딩을 사용하여 다른 응답을 처리합니다. 이 작은 snipplet는 상단과 하단에 테두리를 만듭니다.

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- This is the line -->
    <item>
          <shape>
                <padding android:left="0dp" android:top="1dp" android:right="0dp" android:bottom="1dp"/>
                <solid android:color="#898989" />
          </shape>
    </item>
    <!-- This is the main color -->
    <item>
         <shape>
             <solid android:color="#ffffff" />
         </shape>
    </item>
</layer-list>


답변

레이어 목록 드로어 블이 위에서 아래로 그려 지므로 @Maragues의 대답은 거꾸로입니다 (목록의 마지막 항목이 맨 위에 그려 짐).

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- This is the line -->
<item>
      <shape>
            <solid android:color="#535353" />
      </shape>
</item>
<!-- This is the main color -->
<item android:bottom="1dp">
     <shape>
           <solid android:color="#252525" />
     </shape>
</item>

</layer-list>

이렇게하면 도형을 선 색상으로 효과적으로 채운 다음 그 위에 배경색을 그립니다. 마지막 1dp는 선 색상이 보이도록 남겨 둡니다.


답변

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >


<item>
    <shape android:shape="rectangle" >
        <stroke  android:width="2dp"
                 android:color="#BBBBBB" />
        <solid android:color="@android:color/transparent" />
    </shape>
</item>
<item  android:bottom="2dp" >
    <shape android:shape="rectangle" >
        <stroke  android:width="2dp"
                 android:color="@color/main_background_color" />
        <solid android:color="@android:color/transparent" />
    </shape>
</item>


답변

다음 코드를 사용했습니다.

<?xml version="1.0" encoding="UTF-8"?>
<!-- inset is used to remove border from top, it can remove border from any other side-->
<inset xmlns:android="http://schemas.android.com/apk/res/android"
    android:insetTop="-2dp" android:insetLeft="-2dp" android:insetRight="-2dp"
    >
    <shape xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/rectangle">
        <stroke android:width="1dp" android:color="@color/colorPrimary" />
        <solid android:color="#0000"/>
        <padding android:left="2dp" android:top="2dp" android:bottom="2dp" android:right="2dp"/>
    </shape>
</inset>