반응형
AppCompat 위젯을 사용하여 버튼을 생성하고 클릭시 색상이 바뀌는 이벤트를 어떻게 주는지 아래 예제를 통해 살펴보자.
-AppCompatButton 사용 예제-
-코드-
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:paddingTop="55dp"
android:paddingLeft="25dp"
android:paddingRight="25dp"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.AppCompatButton
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/white_rounded_button"
android:text="Login"/>
</LinearLayout>
</RelativeLayout>
white_rounded_button.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape>
<solid android:color="@android:color/holo_blue_light"/>
<stroke android:color="@android:color/black" android:width="1dp"/>
<corners android:radius="50dp"/>
</shape>
</item>
<item>
<shape>
<solid android:color="@android:color/white"/>
<stroke android:color="@android:color/darker_gray"
android:width="1dp"/>
<corners android:radius="50dp"/>
</shape>
</item>
</selector>
반응형
'Android' 카테고리의 다른 글
Android / 버튼 클릭시 숫자가 증감하는 로직 (2) | 2020.07.22 |
---|---|
Android/ ProgressBar (0) | 2020.07.22 |
Android / ScrollView (0) | 2020.07.21 |
Android / 액션바 삭제하기 (0) | 2020.07.21 |
Android / 원형 이미지 구현하기 (0) | 2020.07.21 |