Android/XML
<ImageButton> 꾸미기
혀가 길지 않은 개발자
2020. 8. 4. 17:32
values/colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#6200EE</color>
<color name="colorPrimaryDark">#3700B3</color>
<color name="colorAccent">#03DAC5</color>
<integer name="alpha_pressed">50</integer>
</resources>
<integer name="alpha_pressed">50</integer> 추가
drawable/btn_setting.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<bitmap android:alpha="@integer/alpha_pressed" android:src="@drawable/ic_setting" />
</item>
<item android:state_pressed="false" android:drawable="@drawable/ic_setting" />
</selector>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="center"
android:background="#BBBBFF"
tools:context=".MainActivity">
<ImageButton
android:id="@+id/btnSetting"
android:layout_width="80dp"
android:layout_height="80dp"
android:background="@drawable/btn_setting"/>
</LinearLayout>
MainActivity.kt
package com.jwsoft.kotlinproject
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
btnSetting.setOnClickListener {
Toast.makeText(applicationContext, "Setting", Toast.LENGTH_SHORT).show()
}
}
}