Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActionBarSherlock with custom RelativeLayout does not show title text

The ActionBar does not show title when using relative layout. And when I not use the RelativeLayout, the items are not aligned to right. Please don't tell me to use menu items as the solution. It must be custom.

Here is how I set it:

    ActionBar bar = getSupportActionBar();
    bar.setDisplayOptions(
            ActionBar.DISPLAY_HOME_AS_UP |
            ActionBar.DISPLAY_SHOW_CUSTOM |
            ActionBar.DISPLAY_SHOW_HOME |
            ActionBar.DISPLAY_SHOW_TITLE |
            ActionBar.DISPLAY_USE_LOGO);

    bar.setTitle(title);
    bar.setCustomView(actionBar);
    this.setTitle(title);

This is when the align is good, but title is not shown

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_alignParentRight="true" >

        <ImageButton
            android:id="@+id/acion_add"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/menu_label_add"
            style="?attr/actionButtonStyle" />

        <ImageButton
            android:id="@+id/action_prev"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/menu_label_prev"
            style="?attr/actionButtonStyle" />

        <ImageButton
            android:id="@+id/action_next"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/menu_label_next"
            style="?attr/actionButtonStyle" />

    </LinearLayout>

</RelativeLayout>

Screenshot

And here comes the layout where the title is shown, but the items are aligned to left

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="right"
    android:gravity="right">

    <ImageButton
        android:id="@+id/acion_add"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/menu_label_add"
        style="?attr/actionButtonStyle" />

    <ImageButton
        android:id="@+id/action_prev"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/menu_label_prev"
        style="?attr/actionButtonStyle" />

    <ImageButton
        android:id="@+id/action_next"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/menu_label_next"
        style="?attr/actionButtonStyle" />

</LinearLayout>

Screenshot

like image 949
Yaroslav Mytkalyk Avatar asked Oct 30 '12 10:10

Yaroslav Mytkalyk


1 Answers

I had the same problem and found this link: https://groups.google.com/forum/?fromgroups=#!topic/actionbarsherlock/He6gst4nvR0

This worked for me:

layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    ...

</LinearLayout>

code:

import com.actionbarsherlock.app.ActionBar.LayoutParams;
...
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.RIGHT | Gravity.CENTER_VERTICAL);
getSupportActionBar().setCustomView(segmentView, lp);
like image 93
Linda Avatar answered Sep 19 '22 15:09

Linda