Reputation: 1842
I have some screens with different actionBar layouts and try to center the title of each actionBar. This is my layout:
actionbar_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:text="Control"
android:textColor="@color/fragment_title_font"
android:id="@+id/fragmentTitle"
android:textSize="14sp" />
<com.joanzapata.iconify.widget.IconButton
android:id="@+id/buttonActionbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_marginTop="15dp"
android:layout_marginBottom="15dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:visibility="gone"
android:background="@drawable/button_actionbar"
android:text=""
android:singleLine="true"
android:textColor="#ffffffff"
android:textSize="12sp"/>
</RelativeLayout>
and this is the code to show my custom actionBar:
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
LayoutInflater mInflater = LayoutInflater.from(context);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setCustomView(mInflater.inflate(R.layout.actionbar_layout, null),
new ActionBar.LayoutParams(
ActionBar.LayoutParams.WRAP_CONTENT,
ActionBar.LayoutParams.MATCH_PARENT,
Gravity.CENTER
));
if the screen has no right button and only left UpButton, it looks fine:
if the screen has no left UpButton but right button, it looks fine too:
but if I want to set both, my title is not centered more:
Is there any possibility to center my title in all cases?
EDIT: the suggestion of @tynn works only for my third case. Better then nothing, thanks!
Upvotes: 0
Views: 284
Reputation: 39853
Your title is centered in all your cases, just the buttons are overlaying. If you want to center the TextView
relative to the button you need to make the layout definitions relative to the button instead of centering it relative to the parent layout.
One possible way to achieve your goal by using a LinearLayout
(doing the same with a RelativeLayout
should be straightforward but a little more verbose)
<?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="match_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/fragmentTitle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:gravity="center_horizontal"
android:text="Control"
android:textColor="@color/fragment_title_font"
android:textSize="14sp" />
<com.joanzapata.iconify.widget.IconButton
android:id="@+id/buttonActionbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginTop="15dp"
android:layout_marginBottom="15dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:visibility="gone"
android:background="@drawable/button_actionbar"
android:text=""
android:singleLine="true"
android:textColor="#ffffffff"
android:textSize="12sp"/>
</LinearLayout>
If doing so you, shouldn't set the LayoutParams
explicitly and if you do don't use ActionBar.LayoutParams.WRAP_CONTENT
for the width
actionBar.setCustomView(R.layout.actionbar_layout);
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setDisplayHomeAsUpEnabled(true);
Upvotes: 1
Reputation: 106
Have you tried to change this:
actionBar.setCustomView(mInflater.inflate(R.layout.actionbar_layout, null),
new ActionBar.LayoutParams(
ActionBar.LayoutParams.WRAP_CONTENT,
ActionBar.LayoutParams.MATCH_PARENT,
Gravity.CENTER));
to this:
actionBar.setCustomView(mInflater.inflate(R.layout.actionbar_layout, null),
new ActionBar.LayoutParams(
ActionBar.LayoutParams.MATCH_PARENT,
ActionBar.LayoutParams.MATCH_PARENT));
Upvotes: 0