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
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With