AndroidDev
AndroidDev

Reputation: 16405

actionbarsherlock TitleTextStyle not working

I have the following style for my the ActionBar in my project

     <style name="Theme.MyTheme" parent="Theme.Sherlock.Light">
    <item name="actionBarStyle">@style/Widget.MyTheme.ActionBar</item>
    <item name="android:actionBarStyle">@style/Widget.MyTheme.ActionBar</item>
    <item name="homeAsUpIndicator">@drawable/icon_back</item>
    <item name="android:homeAsUpIndicator">@drawable/icon_back</item>
</style>

<style name="Widget.MyTheme.ActionBar" parent="Widget.Sherlock.ActionBar">
    <item name="android:background">#0099CC</item>
    <item name="android:height">?android:attr/actionBarSize</item>
    <item name="background">#0099CC</item>
    <item name="android:titleTextStyle">@style/Theme.ActionBar.TitleTextStyle</item>
    <item name="titleTextStyle">@style/Theme.ActionBar.TitleTextStyle</item>
    <item name="android:displayOptions">useLogo|showTitle|homeAsUp</item>
    <item name="displayOptions">useLogo|showTitle|homeAsUp</item>
</style>

<style name="Theme.ActionBar.TitleTextStyle" parent="TextAppearance.Sherlock.Widget.ActionBar.Title">
    <item name="android:textColor">@color/white</item>
    <item name="android:padding">10dp</item>
</style>

Now the text in the title bar is 0dp aligned to the left. The TextColor attribute works fine but the padding seems to be ignored. As a result, the title of the Activity is stuck to the left hand of the screen that is not very pleasing to look at. How can i add padding to the text ?

Kind Regards,

Upvotes: 0

Views: 2123

Answers (2)

Ahmad
Ahmad

Reputation: 72653

To archive haveing a centered title in the ABS, without having the home button, you could just do this:

In your Activity, in your oncreate methode:

getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); 
getSupportActionBar().setCustomView(R.layout.abs_layout);

abs_layout:

<?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:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="my Title"
        android:textColor="#ffffff"
        android:id="@+id/mytext"
        android:textSize="25dp" />


</LinearLayout>

Now you should have a Actionbar wih just a Title. If you want to set a custom background, set it in the Layout above(but then don't forget to set android:layout_height="match_parent")

or with:

getSupportActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.yourimage));

Upvotes: 1

Archie.bpgc
Archie.bpgc

Reputation: 24012

This is how I was able to set the padding between the home icon and the title.

ImageView view = (ImageView)findViewById(android.R.id.home);
view.setPadding(left, top, right, bottom);

I couldn't find a way to customize this via the ActionBar xml styles though. That is, the following XML doesn't work:

<style name="ActionBar" parent="android:style/Widget.Holo.Light.ActionBar">        
    <item name="android:titleTextStyle">@style/ActionBarTitle</item>
    <item name="android:icon">@drawable/ic_action_home</item>        
</style>

<style name="ActionBarTitle" parent="android:style/TextAppearance.Holo.Widget.ActionBar.Title">
    <item name="android:textSize">18sp</item>
    <item name="android:paddingLeft">12dp</item>   <!-- Can't get this padding to work :( -->
</style>

from here:

Padding between ActionBar's home icon and title

Upvotes: 1

Related Questions