완성본
액션바 외 다른 코드는 밑 링크 참고
https://dkan9634.tistory.com/97
사용법
1. 타이틀바로 사용할 xml을 그려주자. => height : 60dp
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="My Action Bar"
android:textColor="@color/white"
android:textStyle="bold"
android:textSize="20sp" />
2. 액티비티에서 -> 기본 액션바를 불러내서 => 커스텀 액션바로 설정하자.
// 1. 액티비티에서 -> 기본 액션바를 불러내서 => custom 액션바로 수정
//기본액션바를 불러내자
val defaultActionBar = supportActionBar!!
// 모드를 커스텀 지원으로 설정
defaultActionBar.displayOptions = ActionBar.DISPLAY_SHOW_CUSTOM
// 실제로 어떤 커스텀뷰를 사용?
defaultActionBar.setCustomView(R.layout.my_custom)
3. 양 옆의 여백 제거. => 커스텀 액션바를 들고있는 toolbar 를 찾아내서 여백 제거.
// 양 옆 여백 제거 필요 => 커스텀 액션바를 들고 있는 toolbar를 찾아내서 여백 제거
// tool bar 변수에 저장
val toolbar = defaultActionBar.customView.parent as Toolbar
toolbar.setContentInsetsAbsolute(0, 0) // 왼오 여백
4. 커스텀 액션바를 꾸며서 => 요소들을 id를 붙여서 코틀린에서 활용.
=> 액션바.커스텀뷰.findViewById(아이디입력) 를 이용해서 뷰를 찾아 오자.
=> 멤버변수로 생성, 찾는건 onCreate 이후에. => lateinit var 활용. (멤버변수 필요!!)
나는 액션바에 버튼을 놓고 버튼을 누르면 토스트가 뜨게 했다.
액션바 xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="horizontal"
android:background="@color/black"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="My Action Bar"
android:textColor="@color/white"
android:textStyle="bold"
android:textSize="20sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"/>
<Button
android:id="@+id/toastBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="토스트 띄우기"/>
</LinearLayout>
MainActivity Code
package com.eunjeong.drawablexml
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.Toast
import androidx.appcompat.widget.Toolbar
import androidx.appcompat.app.ActionBar
class MainActivity : AppCompatActivity() {
// 액션바에 있는 UI를 담아줄 변수
lateinit var toastBtn : Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// 1. 액티비티에서 -> 기본 액션바를 불러내서 => custom 액션바로 수정
//기본액션바를 불러내자
val defaultActionBar = supportActionBar!!
// 모드를 커스텀 지원으로 설정
defaultActionBar.displayOptions = ActionBar.DISPLAY_SHOW_CUSTOM
// 실제로 어떤 커스텀뷰를 사용?
defaultActionBar.setCustomView(R.layout.my_custom)
// 양 옆 여백 제거 필요 => 커스텀 액션바를 들고 있는 toolbar를 찾아내서 여백 제거
// tool bar 변수에 저장
val toolbar = defaultActionBar.customView.parent as Toolbar
toolbar.setContentInsetsAbsolute(0, 0) // 왼오 여백
//커스텀 액션바에 담아둔 버튼 찾아오기
toastBtn = defaultActionBar.customView.findViewById(R.id.toastBtn)
toastBtn.setOnClickListener {
Toast.makeText(this, "액션바를 통한 토스트", Toast.LENGTH_SHORT).show()
}
}
}
https://github.com/Eundding/android_study/tree/master/DrawableXml
GitHub - Eundding/android_study
Contribute to Eundding/android_study development by creating an account on GitHub.
github.com
참고 : class101+ 코딩티처 지니 강의
'Android' 카테고리의 다른 글
[Android] 원형 이미지뷰 / CircleImageView 만들기 (0) | 2023.01.03 |
---|---|
[Android] 이미지 확대/축소 하기 (0) | 2023.01.03 |
[Android] TextView background를 Xml로 지정 (0) | 2023.01.02 |
[Android] TabLayout text color, indicator color 변경 (2) | 2023.01.02 |
[Android] Fragments (0) | 2023.01.02 |