Notice
Recent Posts
Recent Comments
Link
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Tags more
Archives
Today
Total
관리 메뉴

맨땅에 코딩

[안드로이드]ActionBar, ToolBar, 상태 표시줄 커스텀 본문

앱 개발/Java

[안드로이드]ActionBar, ToolBar, 상태 표시줄 커스텀

맨땅 2022. 7. 26. 15:53

목차

    반응형

     

     

     

     

    모르시겠으면 그대로 쭉 따라하시면 됩니다

     

     

     

    1) ActionBar, ToolBar 커스텀

     

    1. 우선 원래 있던 ActionBar를 지워줍니다

     

    themes.xml에서 

     

    <style name="Theme.ParkingSensor" parent="Theme.AppCompat.Light.NoActionBar">

    로 style을 변경해줍니다

     

    어두운 테마를 원하시면 Light 대신 Dark를 써주시면 됩니다

     

     

     

    2. layout에 toolbar.xml을 만들어줍니다

     

    [toolbar.xml]

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.appcompat.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        xmlns:tools="http://schemas.android.com/tools"
        android:background="@color/Pink"
        android:minHeight="?attr/actionBarSize"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        >
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="주차상태 View"
            android:layout_gravity="center"
            android:id="@+id/toolbar_title"
            android:textSize="22sp"
            />
    
    </androidx.appcompat.widget.Toolbar>

     

    위 소스코드에 대한 Design입니다

    android:background에서 @color에 컨트롤+왼쪽마우스 클릭 하시거나 colors.xml에 들어가주셔서

    원하시는 색상 코드를 입력해줍니다

    <color name="Pink">#FDB9C5</color>

     

     

     

    3. toolbar를 사용하는 layout에 오셔서 include해줍니다

     

    저는 activity_main.xml에 추가해주었습니다

     

    <include
        android:id="@+id/include"
        layout="@layout/toolbar" />

    이 코드를 추가해주시면 됩니다

     

    [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"
        >
    
        <include
            android:id="@+id/include"
            layout="@layout/toolbar" />
    
    
    
        <LinearLayout
            android:layout_width="388dp"
            android:layout_height="573dp"
            android:orientation="vertical"

    이해를 돕기위해 소스코드 일부를 추가합니다

     

     

     

    4.MainActiviy.java에 필요한 소스코드를 추가해줍니다(이제 끝입니다)

     

    [MainActivity.java]

    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

     

    onCreate에 이 소스코드를 추가해줍니다

     

     

    어디에 써야될지 헷갈리신다면 

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    이 하단에 추가해주시면 됩니다

     

     

     

    2) 상대표시줄 커스텀

     

    맨 처음 대표사진의 상태표시줄을 회색으로 바꿔주는 기능입니다

     

    맨 처음 AndroidStudio에서 앱을 런타임 해보시면 상태표시줄이 파란색으로 뜨는걸 확인하실수 있습니다

     

    AndroidManifest.xml에 theme를 아래와 같이 변경해주면 됩니다

     

    [AndroidManifest.xml]

    android:theme="@style/Theme.AppCompat.Light.NoActionBar"

     

     

    반응형