[안드로이드 스튜디오] 나만의 툴바 만들기
# App/Android(JAVA)

[안드로이드 스튜디오] 나만의 툴바 만들기

ToolBar 




상태 표시줄 밑에 있는 저 막대기 모양를 툴바라고 합니다.

뒤로 가기, 장바구니 보기, 검색 등의 아이콘을 배치하거나 어플의 로고를 가운데 넣는 등 

예쁘게 꾸미고 사용성을 높이는 데 사용됩니다.







To Do





1. Action Bar 제거


style.xml에 들어가서 액션 바를 제거합니다.


참고 : http://kingpiggylab.tistory.com/67?category=671653




2.ToolBar 세팅



2.1 액티비티.java 에 들어가서 Toolbar 등록해주기(import android.support.v7.widget.Toolbar;)



1
2
3
4
Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
 
cs



2.2 res 우클릭 -> new -> Android Resource Directory


menu 폴더 생성, 안에 menu.xml 생성하기


1
2
3
4
5
6
7
8
9
10
11
12
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    >
 
    <item
        android:id="@+id/action_info"
        android:title="정보"
        android:icon="@drawable/information"
        app:showAsAction="always"/>
 
</menu>
cs




2.3 액티비티에 코드 추가

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.menu, menu);
        return true;
    }
 
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_info:
//툴바의 아이콘이 할 기능 정의할 것
                Intent intent = new Intent(getApplicationContext(), InfoActivity.class);
                startActivity(intent);
                break;
        }
        return super.onOptionsItemSelected(item);
    }

cs



 

3. 돌아가기 버튼 만들기 ( 필요하면 넣기)



1
2
3
4
5
        myToolbar = (Toolbar) findViewById(R.id.drinks_toolbar);
        setSupportActionBar(myToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeAsUpIndicator(R.drawable.menu_left_arrow);
        getSupportActionBar().setDisplayShowTitleEnabled(false);
cs


drawable 파일은 아이콘 제작 홈페이지에서 제작합니다



기존 메소드에 case 추가하기

1
2
3
4
5
6
7
8
9
10
11
12
13
 @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_shoppingcart:
                Intent intent = new Intent(getApplicationContext(), ShoppingCartActivity.class);
                startActivity(intent);
                break;
            case android.R.id.home:
                finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
cs







유용하셨다면 공감 버튼 ↓ 눌러주세요! 


728x90