ShapeDrawable xml에서 사용하기

Posted by ITPangPang
2016. 5. 15. 17:47 안드로이드(android)/xml


ShapeDrawable 기초



안드로이드에서는 xml로 기본적인 도형을 만들 수 있는

   ShapeDrawable이라는 것이 있습니다. 

   ShapeDrawable은 Drawable Resources의 한 종류입니다.


   이번글에서는 아주 기초적으로 Rectangle, Line, Oval도형

   사용하는 방법을 알아보겠습니다


   기초는 쉽지만 응용하기는 정말 어려운거 같습니다.

   이번글은 한가지 적용하는 방법만 간단히 알아보지만 

   여러도형들도 복합적으로 사용하는 방법이 있으니 이와 관련해서는 다음번에 써보겠습니다




먼저

[res]-[drawable]

에다가 xml을

하나 만듭니다


그 다음

아래와 같이 shape을

써준후

원하는 도형을

선택해줍니다


그 다음 

도형마다

설정해줘야하는

속성이 있습니다.

(뭐 거의 비슷합니다)


예를 들어서

rectangle을

하나 만들어 보겠습니다


이렇게 만들어 

준 후에


rectangle 디자인을

넣을곳에 

src나 background로 

붙여주면 됩니다

(보통 버튼, dialog나 ImageView, TextView)


이런식으로

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
>
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/shape"/>

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/shape"/>
</LinearLayout>


다음은 

Oval을 하나

만들어 보겠습니다.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
>

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/shape"/>
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/shape"/>
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@drawable/shape"/>
<ImageView
android:layout_width="200dp"
android:layout_height="200dp"
android:src="@drawable/shape"/>
</LinearLayout>

이와 같이 적어주면


이런식으로 만들수 있습니다.