티스토리 뷰

반응형

Stack Overflow에 자주 검색, 등록되는 문제들과 제가 개발 중 찾아 본 문제들 중에서 나중에도 찾아 볼 것 같은 문제들을 정리하고 있습니다.

Stack Overflow에서 가장 먼저 확인하게 되는 가장 높은 점수를 받은 Solution과 현 시점에 도움이 될 수 있는 가장 최근에 업데이트(최소 점수 확보)된 Solution을 각각 정리하였습니다.

 

아래 word cloud를 통해 이번 포스팅의 주요 키워드를 미리 확인하세요.

Is there an easy way to add a border to the top and bottom of an Android View?

Android View의 상단과 하단에 테두리를 쉽게 추가할 수 있는 방법이 있습니까?

 문제 내용 

I have a TextView and I'd like to add a black border along its top and bottom borders. I tried adding android:drawableTop and android:drawableBottom to the TextView, but that only caused the entire view to become black.

TextView가 있는데 상단과 하단 테두리를 따라 검은색 테두리를 추가하고 싶습니다. Android:drawableTop과 Android:drawableBottom을 TextView에 추가하려고 했지만 전체 뷰가 검은색이 되었습니다.
<TextView
    android:background="@android:color/green"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:drawableTop="@android:color/black"
    android:drawableBottom="@android:color/black"
    android:text="la la la" />

 

Is there a way to easily add a top and bottom border to a View (in particular, a TextView) in Android?

Android에서 View(특히 TextView)에 위아래 테두리를 쉽게 추가할 수 있는 방법이 있습니까?

 

 

 

 높은 점수를 받은 Solution 

In android 2.2 you could do the following.

Android 2.2에서는 다음을 수행할 수 있습니다.

 

Create an xml drawable such as /res/drawable/textlines.xml and assign this as a TextView's background property.

/res/drawable/textlines.xml과 같은 xml 드로어블을 생성하고 이를 TextView의 배경 속성으로 할당합니다.
<TextView
android:text="My text with lines above and below"
android:background="@drawable/textlines"
/>

 

/res/drawable/textlines.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
      <shape 
        android:shape="rectangle">
            <stroke android:width="1dp" android:color="#FF000000" />
            <solid android:color="#FFDDDDDD" />

        </shape>
   </item>

   <item android:top="1dp" android:bottom="1dp"> 
      <shape 
        android:shape="rectangle">
            <stroke android:width="1dp" android:color="#FFDDDDDD" />
            <solid android:color="#00000000" />
        </shape>
   </item>

</layer-list>

 

The down side to this is that you have to specify an opaque background colour, as transparencies won't work. (At least i thought they did but i was mistaken). In the above example you can see that the solid colour of the first shape #FFdddddd is copied in the 2nd shapes stroke colour.

이것의 단점은 투명도가 작동하지 않기 때문에 불투명한 배경색을 지정해야 한다는 것입니다. (적어도 저는 그들이 그랬다고 생각했지만 제가 착각했습니다). 위의 예에서 첫 번째 도형 #FFdddddd의 단색이 두 번째 도형 획 색상으로 복사되는 것을 볼 수 있습니다.

 

 

 

 가장 최근 달린 Solution 

You can do this by this code snippet -

당신은 이 코드 조각으로 이것을 할 수 있습니다.

 

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <!--Minus (-) how much dp you gave in the stroke width from left right-->
    <item android:left="-10dp" android:right="-10dp">
        <shape
            android:shape="rectangle">
            <stroke android:width="10dp" android:color="@android:color/holo_red_dark" />
           <!--This is the main background -->
            <solid android:color="#FFDDDDDD" />
        </shape>
    </item>
</layer-list>

 

Preview -

미리보기

enter image description here

 

 

출처 : https://stackoverflow.com/questions/1598119/is-there-an-easy-way-to-add-a-border-to-the-top-and-bottom-of-an-android-view

반응형
댓글
공지사항
최근에 올라온 글