[토막글] Toolbar의 Text(Title) 정렬

지프치프·2022년 1월 8일
0

Android

목록 보기
37/85
post-thumbnail

“Android 로봇은 Google에서 제작하여 공유한 저작물을 복제하거나 수정한 것으로 Creative Commons 3.0 저작자 표시 라이선스의 약관에 따라 사용되었습니다.”


Toolbar의 title은 기본적으로 정렬을 지원하지 않는다.
그래서 약간의 꼼수를 사용해야한다.

우선 Toolbar의 title을 hide 시켜야한다.

	binding.apply {
            actionBar?.hide()
            setSupportActionBar(toolbar)
            // Title을 hide
            supportActionBar?.setDisplayShowTitleEnabled(false)
            tvToolbarTitle.text = "넣고싶은 문자열"
	}            

그리고나서 xml로 가서 toolbar의 하위에 textview를 추가해준다.

    <androidx.appcompat.widget.Toolbar
        android:id = "@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
        <TextView
            android:id="@+id/tv_toolbar_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity = "center"
            android:textColor="@color/black"
            android:textStyle="bold"
            android:fontFamily="@font/kblz"
            android:textSize="20dp"
            tools:text = "Toolbar" />
    </androidx.appcompat.widget.Toolbar>

필자는 Title에 오늘 날짜를 넣어주었다.
실행결과는 아래와 같다.

profile
지프처럼 거침없는 개발을 하고싶은 개발자

0개의 댓글