본문 바로가기
프로그래밍/Android

Android Actionbar(appcompat_v7) 사용하기

by CaffeLatte 2014. 11. 1.

1) themes.xml 구현하기

<!-- the theme applied to the application or activity -->

    <style name="CustomActionBarTheme" parent="@style/Theme.AppCompat">        

        <item name="android:actionBarStyle">@style/solid_ActionBar</item>

        <item name="android:actionBarTabStyle">@style/MyActionBarTabs</item>

        <item name="android:actionBarTabBarStyle">@style/ActionBarTabBarStyle</item>

        <item name="android:actionBarDivider">@null</item>

        <!-- <item name="android:actionBarSize">80dp</item> --> 


        <!-- Support library compatibility -->

        <item name="actionBarTabStyle">@style/MyActionBarTabs</item>

    </style>

    

    <!-- ActionBar styles -->

    <style name="solid_ActionBar" parent="@style/Widget.AppCompat.ActionBar.Solid">        

        <item name="android:background">@null</item>

        <!-- <item name="android:backgroundStacked">@null</item>

        <item name="android:backgroundSplit">@null</item> -->

    </style>


    <!-- ActionBar tabs styles -->

    <style name="MyActionBarTabs" parent="@style/Widget.AppCompat.ActionBar.TabView">

        

        <!-- tab indicator -->

        <item name="android:background">@null</item>


        <!-- Support library compatibility -->

        <item name="background">@null</item>

    </style>

    

    <!-- ActionBar tab bar styles -->

    <style name="ActionBarTabBarStyle" parent="@style/Widget.AppCompat.ActionBar.TabBar">

        <!-- tab indicator -->

        <item name="android:background">@null</item>


        <!-- Support library compatibility -->

        <item name="background">@null</item>

    </style>


2) AndroidManifest.xml 설정하기

<application

        android:allowBackup="true"

        android:icon="@drawable/ic_launcher"

        android:label="@string/app_name"

        android:theme="@style/AppTheme" >

<activity

android:name="com.test.BaseActivity"

android:theme="@style/CustomActionBarTheme" >

</activity>

</application>


3) actionbar.xml 구현하기

<?xml version="1.0" encoding="utf-8"?>

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

    android:layout_width="match_parent"

    android:layout_height="wrap_content"

    android:background="@color/c_ffffff"

    android:orientation="vertical" >

    <LinearLayout

        android:id="@+id/ll_actionbar_background"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:gravity="center"

        android:orientation="horizontal"

        android:padding="10dp" >

        <Button

            android:id="@+id/btn_left"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:clickable="true"

            android:text="back"

            android:visibility="gone" />

        <TextView

            android:id="@+id/tv_title"

            android:layout_width="0dp"

            android:layout_height="wrap_content"

            android:layout_weight="1"

            android:gravity="center" />

        <Button

            android:id="@+id/btn_right"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:clickable="true"

            android:text="home"

            android:visibility="gone" />

    </LinearLayout>

</LinearLayout>


4) BaseActivity 구현하기

public class BaseActivity extends ActionBarActivity {

  private Context m_context;

  protected ActionBar m_actionBar;

  private LinearLayout m_llAbBackground; // 액션바 백그라운드


  @Override

  protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);


    m_context = this;


    m_llAbBackground = (LinearLayout) findViewById(R.id.ll_actionbar_background);

  }

  

  @Override

  protected void onResume() {

    super.onResume();

    

    m_llAbBackground.setBackgroundResource(R.drawable.main_navbar);

  }

}


5) 참고사항

매니페스트에서 커스텀할 액션바의 테마는 각 액티비티에 설정해 주어야 깔끔하게 커스텀을 할 수 있다.


댓글