안드로이드
안드로이드 원형 버튼 만들기
Moimeme Futur
2021. 11. 8. 18:18
개요
안드로이드에서 버튼의 모양과 색을 커스터마이징할 수 있다.
실행 화면

검정 테두리 원형 버튼 만들기
shape_for_circle_button.xml 생성
아래 이미지와 같이 안드로이드 프로젝트 [app] > [res] > [drawable] 디렉토리에 Drawble Resource File(ex. shape_for_circle_button.xml) 을 생성한다.

shape_for_circle_button.xml 구현
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#4CAF50" />
<stroke
android:width="1dp"
android:color="@color/white" />
</shape>
Button에 적용하기
적용할 버튼이 있는 화면의 Layout 파일로 이동하여 해당 버튼의 속성 값으로 android:background="@drawable/파일이름.xml" 넣어준다.
ex).
[app] > [res] > [layout] > [activity_main.xml]
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.appcompat.widget.AppCompatButton
android:layout_width="50dp"
android:layout_height="50dp"
android:text="Button"
android:background="@drawable/shape_for_circle_button"
android:textSize="10sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
결과

초록색 원형 버튼 만들기
shape_for_circle_green_button.xml 만들기
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#4CAF50" />
<stroke
android:width="1dp"
android:color="@color/white" />
</shape>
버튼에 적용하기
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.appcompat.widget.AppCompatButton
android:layout_width="50dp"
android:layout_height="50dp"
android:text="Button"
android:background="@drawable/shape_for_circle_green_button"
android:textSize="10sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
결과

👉 개발자로 취업을 준비 중이시라면,
제가 직접 정리한 취업용 전략 전자책(PDF)도 참고해보세요.
📘 [개발자로 취업하는 법, 현실적인 모든 이야기]
이 책에는 제가 직접 취업 준비를 하며 겪은 시행착오와,
실제 취업 후 주변 동료들에게 공유했던 실전 꿀팁이 담겨 있어요.
특히, 실무 중심의 내용을 담았습니다.