안녕세계

[안드로이드] Toolbar 본문

[안드로이드] Toolbar

Junhong Kim 2017. 1. 17. 18:17
728x90
반응형

Actionbar 대신에 Toolbar를 사용하여 더 유연한 Actionbar를 구현할 수 있다.


[ Step1 ] 라이브러리

dependencies {
compile 'com.android.support:appcompat-v7:25.0.0'
}

※ 라이브러리가 21.0.0 이상이어야 Toolbar 사용 가능



[ Step2 ] 앱 스타일 변경

[AndroidManifest.xml]

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jnj.kickthebucket">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".Activity.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

AndroidManifest 파일에서 android:theme="@style/AppTheme" 부분을 따라 들어가면,

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

이 있는데 이 부분을 NoActionBar 스타일로 바꿔줍니다 아무거나 상관 없음



[ Step3 ] Toolbar 레이아웃 생성

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

[ Step4 ] 툴바 설정

setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
final ActionBar actionBar = getSupportActionBar();
actionBar.setHomeAsUpIndicator(R.drawable.ic_example);
actionBar.setDisplayHomeAsUpEnabled(true);

setContentView 이후로 지정해야함

아이콘 지정, 허용 설정


※ 참고

[1] https://developer.android.com/reference/android/widget/Toolbar.html

[2] https://developer.android.com/reference/android/support/v7/widget/Toolbar.html

[3] https://android-developers.googleblog.com/2014/10/appcompat-v21-material-design-for-pre.html

[4] http://thdev.net/625 - 툴바 

[5] http://makerj.tistory.com/182 - 코디네이터 레이아웃



728x90
반응형
Comments