* 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">
<com.example.view.MyButton
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="확인"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
* MyButton.java
public class MyButton extends AppCompatButton {
public MyButton(Context context) {
super(context);
init(context);
}
public MyButton(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public void init(Context context) {
setBackgroundColor(Color.CYAN);
setTextColor(Color.BLACK);
float textSize = getResources().getDimension(R.dimen.text_size); // pixel이 기본값이라 sp로 변환
setTextSize(textSize);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Log.d("MyButton", "onDraw 호출됨");
}
@Override
public boolean onTouchEvent(MotionEvent event) {
Log.d("MyButton", "onTouchEvent 호출됨");
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
setBackgroundColor(Color.BLUE);
setTextColor(Color.RED);
break;
case MotionEvent.ACTION_OUTSIDE:
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
setBackgroundColor(Color.CYAN);
setTextSize(Color.BLACK);
break;
}
invalidate();
return true;
}
}
* dimens.xml(res > values)
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="text_size">30sp</dimen>
</resources>
'android' 카테고리의 다른 글
RecyclerView 리스트(문자목록, 통화목록 ..) (0) | 2020.02.03 |
---|---|
레이아웃 사용자정의 사용 (0) | 2020.02.03 |
NicePatch(png 이미지 확대시) (0) | 2020.02.03 |
권한 요청 (0) | 2020.02.03 |
SMS Receiver (0) | 2020.02.03 |