android toolbar의 정의 위치

조회수 1790회

하나의 activity에 여러개의 fragment를 사용할 때 fragment마다 각각 다른 toolbar를 사용하고 싶을 때가 있습니다. 여기서 toolbar를 activity에 한개만 정의하고 fragment에서 각각 커스텀하여 돌려쓰는 방법과 fragment마다 toolbar를 정의하여 ((AppcompatActivity)context).setSupportActionbar(toolbar); 하는 방법 중 어느것이 일반적인가요? 아니면 다른 방법이 있나요?

  • (•́ ✖ •̀)
    알 수 없는 사용자

1 답변

  • 음.. 그렇게 프래그먼트 돌려쓰는 방법도 있는데... 저같은경우는 그냥 Activity에서 툴바를 하나 만들어서 해요 가끔 툴바에 내용이 달라야 할 때도 있고, 기타등등 (솔직히 내용이 다르게 하는법은 몰라서 그랬어요..<<) 큼..

    그래서 저는 이렇게 했습니다

        <android.support.v7.widget.Toolbar
            android:background="@color/colorPrimary"
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <TextView
                android:layout_gravity="center"
                android:textStyle="bold"
                android:text="열차 조회"
                android:textSize="28dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
    
            <ImageButton
                android:layout_gravity="left"
                android:background="@drawable/ic_menu_white_24dp"
                android:layout_width="48dp"
                android:layout_height="48dp"/>
    
        </android.support.v7.widget.Toolbar>
    

    이렇게 하면 밑의 사진처럼 나옵니다 이미지

    보시는 바와 같이 툴바가 만들어 집니다... 내용 수정은 어느 정도 가능하리라 믿고,, 그럼 이만...<

    • (•́ ✖ •̀)
      알 수 없는 사용자

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)