๐Ÿผ 01 Set up the app bar

YeoulLeeยท2020๋…„ 12์›” 2์ผ
0

TFLite-imageclassification

๋ชฉ๋ก ๋ณด๊ธฐ
2/3

App Bar๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์—ฌ๋Ÿฌ ๋ฐฉ๋ฒ•์ด ์žˆ์ง€๋งŒ, ActionBar ๋Œ€์‹  ToolBar๋ฅผ ์‚ฌ์šฉํ•˜์ž.

  • backward compatibility (work on the widest range of devices)
  • customize app bar

1. AppCompat ์˜์กด์„ฑ ์ถ”๊ฐ€

build.gradle์—

implementation "androidx.appcompat:appcompat:$appcompat_version"

์„ ์ถ”๊ฐ€ํ•ด์•ผ ํ•œ๋‹ค. (์ถ”๊ฐ€๋˜์–ด ์žˆ์Œ)

2. AppCompatActivity

Activity๊ฐ€ AppCompatActivity๋ฅผ ์ƒ์†ํ•ด์•ผ ํ•œ๋‹ค. (์ƒ์†๋˜์–ด ์žˆ์Œ)

  • KOTLIN
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity()
  • JAVA
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity 

3. NoActionBar

ActionBar ์ œ๊ฑฐ๋ฅผ ์œ„ํ•ด res/values/themes.xml์„ ์ˆ˜์ •ํ•œ๋‹ค.

  • ๊ธฐ์กด
<style name="Theme.Image_classification" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
  • ๋ณ€๊ฒฝ ํ›„
<style name="Theme.Image_classification" parent="Theme.MaterialComponents.DayNight.NoActionBar">

CoordinatorLayout

CoordinatorLayout ์˜์กด์„ฑ ์ถ”๊ฐ€

build.gradle์—

implementation "androidx.coordinatorlayout:coordinatorlayout:1.1.0"

์„ ์ถ”๊ฐ€ํ•ด์•ผ ํ•œ๋‹ค. (์ถ”๊ฐ€ํ•˜์ง€ ์•Š์•„๋„ ๋จ)

Layout ๋ณ€๊ฒฝ

layout/activity_main.xml์˜ ์ตœ์ƒ์œ„ ์š”์†Œ๋ฅผ CoordinatorLayout์œผ๋กœ ์ง€์ •ํ–ˆ๋‹ค.

4. Toolbar

CoordinatorLayout ์•ˆ์— Toolbar๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค. Toolbar ์•ˆ์— ImageView๋ฅผ ์œ„์น˜์‹œ์ผฐ๋‹ค. ์ด๋ฏธ์ง€๋Š” ๋‹ค์šด๋กœ๋“œ ํ›„ res/drawable์— ์œ„์น˜์‹œํ‚จ๋‹ค.

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@android:color/black">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/tfl2_logo" />
    </androidx.appcompat.widget.Toolbar>
    
</androidx.coordinatorlayout.widget.CoordinatorLayout>

5. setSupportActionBar()

activity์˜ onCreate() method์—์„œ setSupportActionBar() method๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค. (์ƒ๋žต๊ฐ€๋Šฅ)

  • KOTLIN
import androidx.appcompat.widget.Toolbar

val toolbar: Toolbar = findViewById(R.id.toolbar)
setSupportActionBar(toolbar)
  • JAVA
import androidx.appcompat.widget.Toolbar;

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

https://developer.android.com/training/appbar/setting-up

profile
AI + Android

0๊ฐœ์˜ ๋Œ“๊ธ€