• Aucun résultat trouvé

Customize the Tab Indicator

Dans le document 2. Building Your First App (Page 46-49)

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

<resources>

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

<style name="CustomActionBarTheme"

parent="@style/Theme.AppCompat">

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

<item name="android:actionBarTabTextStyle">@style/MyActionBarTabText</item>

<item name="android:actionMenuTextColor">@color/actionbar_text</item>

<!-- Support library compatibility -->

<item name="actionBarStyle">@style/MyActionBar</item>

<item name="actionBarTabTextStyle">@style/MyActionBarTabText</item>

<item name="actionMenuTextColor">@color/actionbar_text</item>

</style>

<!-- ActionBar styles -->

<style name="MyActionBar"

parent="@style/Widget.AppCompat.ActionBar">

<item name="android:titleTextStyle">@style/MyActionBarTitleText</item>

<!-- Support library compatibility -->

<item name="titleTextStyle">@style/MyActionBarTitleText</item>

</style>

<!-- ActionBar title text -->

<style name="MyActionBarTitleText"

parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">

<item name="android:textColor">@color/actionbar_text</item>

<!-- The textColor property is backward compatible with the Support Library -->

</style>

<!-- ActionBar tabs text -->

<style name="MyActionBarTabText"

parent="@style/Widget.AppCompat.ActionBar.TabText">

<item name="android:textColor">@color/actionbar_text</item>

<!-- The textColor property is backward compatible with the Support Library -->

</style>

</resources>

Customize the Tab Indicator

To change the indicator used for the navigation tabs, create an activity theme that overrides the

actionBarTabStyle property. This property points to another style resource in which you override the background property that should specify a state-list drawable.

Note: A state-list drawable is important so that the tab currently selected indicates its state with a background different than the other tabs. For more information about how to create a drawable resource that handles multiple button states, read the State List documentation.

For example, here's a state-list drawable that declares a specific background image for several different states of an action bar tab:

Styling the Action Bar

47

Content from developer.android.com/training/basics/actionbar/styling.html through their Creative Commons Attribution 2.5 license

res/drawable/actionbar_tab_indicator.xml

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

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

<!-- STATES WHEN BUTTON IS NOT PRESSED -->

<!-- Non focused states -->

<item android:state_focused="false" android:state_selected="false"

android:state_pressed="false"

android:drawable="@drawable/tab_unselected" />

<item android:state_focused="false" android:state_selected="true"

android:state_pressed="false"

android:drawable="@drawable/tab_selected" />

<!-- Focused states (such as when focused with a d-pad or mouse hover) -->

<item android:state_focused="true" android:state_selected="false"

android:state_pressed="false"

android:drawable="@drawable/tab_unselected_focused" />

<item android:state_focused="true" android:state_selected="true"

android:state_pressed="false"

android:drawable="@drawable/tab_selected_focused" />

<!-- STATES WHEN BUTTON IS PRESSED -->

<!-- Non focused states -->

<item android:state_focused="false" android:state_selected="false"

android:state_pressed="true"

android:drawable="@drawable/tab_unselected_pressed" />

<item android:state_focused="false" android:state_selected="true"

android:state_pressed="true"

android:drawable="@drawable/tab_selected_pressed" />

<!-- Focused states (such as when focused with a d-pad or mouse hover) -->

<item android:state_focused="true" android:state_selected="false"

android:state_pressed="true"

android:drawable="@drawable/tab_unselected_pressed" />

<item android:state_focused="true" android:state_selected="true"

android:state_pressed="true"

android:drawable="@drawable/tab_selected_pressed" />

</selector>

For Android 3.0 and higher only

When supporting Android 3.0 and higher only, your style XML file might look like this:

res/values/themes.xml

Styling the Action Bar

48

Content from developer.android.com/training/basics/actionbar/styling.html through their Creative Commons Attribution 2.5 license

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

<resources>

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

<style name="CustomActionBarTheme"

parent="@style/Theme.Holo">

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

</style>

<!-- ActionBar tabs styles -->

<style name="MyActionBarTabs"

parent="@style/Widget.Holo.ActionBar.TabView">

<!-- tab indicator -->

<item name="android:background">@drawable/actionbar_tab_indicator</item>

</style>

</resources>

For Android 2.1 and higher

When using the Support Library, your style XML file might look like this:

res/values/themes.xml

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

<resources>

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

<style name="CustomActionBarTheme"

parent="@style/Theme.AppCompat">

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

<!-- Support library compatibility -->

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

</style>

<!-- ActionBar tabs styles -->

<style name="MyActionBarTabs"

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

<!-- tab indicator -->

<item name="android:background">@drawable/actionbar_tab_indicator</item>

<!-- Support library compatibility -->

<item name="background">@drawable/actionbar_tab_indicator</item>

</style>

</resources>

More resources

• See more style properties for the action bar are listed in the Action Bar guide.

• Learn more about how themes work in the Styles and Themes guide.

• For even more complete styling for the action bar, try the Android Action Bar Style Generator.

Overlaying the Action Bar

49

Content from developer.android.com/training/basics/actionbar/overlaying.html through their Creative Commons Attribution 2.5 license

11. Overlaying the Action Bar

Content from developer.android.com/training/basics/actionbar/overlaying.html through their Creative Commons Attribution 2.5 license

By default, the action bar appears at the top of your activity window, slightly reducing the amount of space available for the rest of your activity's layout. If, during the course of user interaction, you want to hide and show the action bar, you can do so by calling hide() and show() on the ActionBar. However, this causes your activity to recompute and redraw the layout based on its new size.

Figure 1. Gallery's action bar in overlay mode.

To avoid resizing your layout when the action bar hides and shows, you can enable overlay mode for the action bar. When in overlay mode, your activity layout uses all the space available as if the action bar is not there and the system draws the action bar in front of your layout. This obscures some of the layout at the top, but now when the action bar hides or appears, the system does not need to resize your layout and the transition is seamless.

Tip: If you want your layout to be partially visible behind the action bar, create a custom style for the action bar with a partially transparent background, such as the one shown in figure 1. For information about how to define the action bar background, read Styling the Action Bar.

Dans le document 2. Building Your First App (Page 46-49)