프로그래밍/안드로이드

안드로이드 - 액션바 꾸미기

가카리 2015. 10. 25. 14:30
반응형

 

다음 메소드를 활용하여 액션바의 출력 옵션을 변경하거나 꾸밀 수 있다.

 

    void setDisplayOptions(int options [, int mask])

    int getDisplayOptions()

 

지원 가능한 옵션의 종류는 다음과 같다.

 

옵션

설명

DISPALY_USE_LOGO

0x01

앱아이콘 대신에 로고를 표시한다.

DISPLAY_SHOW_HOME

0x02

홈 요소를 보여준다.

DISPLAY_HOME_AS_UP

0x04

왼쪽 화살표 모양의 Up버튼을 보여준다.

DISPLAY_SHOW_TITLE

0x08

타이틀과 서브 타이틀을 보여준다.

DISPLAY_SHOW_CUSTOM

0x10

커스텀 뷰를 보여준다.

 

또는 다음의 옵션을 개별적으로 지정하는 메소드를 호출해도 결과는 동일하다.

 

    void setDisplayUseLogoEnabled(boolean useLogo)

    void setDisplayShowHomeEnabled(boolean showHome)

    void setDisplayHomeAsUpEnabled(boolean showTitle)

    void setDisplayShowCustomEnabled(boolean showCustom)

 

액티비티의 로고 아이콘은 매니페스트에 logo 속성으로 지정한다.

 

    <activity android:name="패키지명.클래스명"

        android:label="DisplayOption"

        android:logo="@drawable/androboy"/>

 

또는 다음 메소드로 로고를 변경 할 수 있다.

    

    void setLogo(int resId)

    void setLogo(Drawable logo)

 

커스텀 뷰는 액션바에 별도의 뷰를 추가로 하나 더 배치하는 것이며 로고 아이콘과 액션 항목 사이에 나타난다.

 

다음 메소드로 커스텀 뷰를 배치한다.

 

    void setCustomView(int resId)

    void setCustomView(View view [, ActionBar.LayoutParams layoutParams])

    View getCustomView()

 

다음 메소드로 서브 타이틀을 하나 더 붙일 수 있다.

 

    void setSubtitle(int resId)

    void setSubtitle(CharSequence subtitle)

 

다음 예제는 실제 옵션 값을 체크 해본다.

 

아래와 같이 파일을 구성한다.

 

displayoption.xml

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="match_parent"

    android:layout_height="match_parent" >

<CheckBox

    android:id="@+id/chkuselogo"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:text="USE LOGO" />

<CheckBox

    android:id="@+id/chkshowhome"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:text="SHOW HOME" />

<CheckBox

    android:id="@+id/chkhomeasup"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:text="HOME AS UP" />

<CheckBox

    android:id="@+id/chkshowtitle"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:text="SHOW TITLE" />

<CheckBox

    android:id="@+id/chkshowcustom"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:text="SHOW CUSTOM" />

</LinearLayout>

 

actionbarmenu.xml

 

<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:title="하나"

        android:icon="@android:drawable/ic_menu_add"

        android:showAsAction="always|withText"

        />

    <item android:title=""

        android:icon="@android:drawable/ic_menu_edit"

        android:showAsAction="ifRoom"

        />

    <item android:title=""

        android:showAsAction="ifRoom"

        />

    <item android:title=""

        android:showAsAction="ifRoom"

        />

    <item android:title="다섯"

        android:showAsAction="ifRoom"

        />

    <item android:title="여섯" />

</menu>

 

DisplayOption.java

 

package com.example.ch21_displayoption;

 

import android.app.ActionBar;

import android.app.Activity;

import android.os.Bundle;

import android.view.Menu;

import android.view.MenuInflater;

import android.widget.Button;

import android.widget.CheckBox;

import android.widget.CompoundButton;

 

 

public class DisplayOption extends Activity {

    CheckBox mChkUseLogo;

    CheckBox mChkShowHome;

    CheckBox mChkHomeAsUp;

    CheckBox mChkShowTitle;

    CheckBox mChkShowCustom;

    ActionBar mActionBar;

    Button mCustom;

    

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.displayoption);

      

        mChkUseLogo = (CheckBox)findViewById(R.id.chkuselogo);

        mChkUseLogo.setOnCheckedChangeListener(mListener);

        mChkShowHome = (CheckBox)findViewById(R.id.chkshowhome);

        mChkShowHome.setOnCheckedChangeListener(mListener);

        mChkHomeAsUp = (CheckBox)findViewById(R.id.chkhomeasup);

        mChkHomeAsUp.setOnCheckedChangeListener(mListener);

        mChkShowTitle = (CheckBox)findViewById(R.id.chkshowtitle);

        mChkShowTitle.setOnCheckedChangeListener(mListener);

        mChkShowCustom = (CheckBox)findViewById(R.id.chkshowcustom);

        mChkShowCustom.setOnCheckedChangeListener(mListener);

        

        mActionBar = getActionBar();

        mCustom = new Button(this);

        mCustom.setText("Custom");

        mActionBar.setCustomView(mCustom);

        

        mActionBar.setSubtitle("subtitle");

        

        int option = mActionBar.getDisplayOptions();

        //개별 옵션 상태를 조사하기위해 다음과같이 비트연산을 해야한다.

        mChkUseLogo.setChecked((option & ActionBar.DISPLAY_USE_LOGO) != 0);

        mChkShowHome.setChecked((option & ActionBar.DISPLAY_SHOW_HOME) != 0);

        mChkHomeAsUp.setChecked((option & ActionBar.DISPLAY_HOME_AS_UP) != 0);

        mChkShowTitle.setChecked((option & ActionBar.DISPLAY_SHOW_TITLE) != 0);

        mChkShowCustom.setChecked((option & ActionBar.DISPLAY_SHOW_CUSTOM) != 0);

        

    }

 

    public boolean onCreateOptionsMenu(Menu menu){

        super.onCreateOptionsMenu(menu);

        MenuInflater inflater = getMenuInflater();

        inflater.inflate(R.menu.actionbarmenu, menu);

      

        return true;

    }

 

    //체크박스 리스너

    CheckBox.OnCheckedChangeListener mListener =

            new CheckBox.OnCheckedChangeListener(){

 

                @Override

                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                    // TODO Auto-generated method stub

                    switch(buttonView.getId()){

                    case R.id.chkuselogo:

                        mActionBar.setDisplayUseLogoEnabled(isChecked);

                        break;

                    case R.id.chkshowhome:

                        mActionBar.setDisplayShowHomeEnabled(isChecked);

                        break;

                    case R.id.chkhomeasup:

                        mActionBar.setDisplayHomeAsUpEnabled(isChecked);

                        break;

                    case R.id.chkshowtitle:

                        mActionBar.setDisplayShowTitleEnabled(isChecked);

                        break;

                    case R.id.chkshowcustom:

                        mActionBar.setDisplayShowCustomEnabled(isChecked);

                        break;                        

                    }

                }

            

    };

    

}

 

 

실행 화면

 

 

반응형