안드로이드
안드로이드 원형 버튼 만들기
devGeek
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>
결과
반응형