Title with the name of the button bar

I would like to center the title in the action bar, having action icons next to it (and ignoring them to set gravity).

I would like to have this: enter image description here

Instead, I have the following: enter image description here

Here is my code:

ActionBar actionBar = getSupportActionBar(); ActionBar.LayoutParams params = new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT); actionBar.setBackgroundDrawable(getResources().getDrawable(R.drawable.top_bar)); actionBar.setCustomView(LayoutInflater.from(this).inflate(R.layout.actionbar_layout, null), params); actionBar.setDisplayShowCustomEnabled(true); actionBar.setDisplayShowHomeEnabled(false); actionBar.setDisplayShowTitleEnabled(false); 

My layout:

 <?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="match_parent" android:orientation="vertical" > <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="match_parent" android:padding="8dp" android:src="@drawable/launcher_icon" /> <TextView android:id="@+id/actionbar_title" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:maxHeight="1dp" android:maxLines="1" android:paddingLeft="48dp" android:paddingRight="48dp" android:text="Bacon ipsum" android:textColor="#777777" android:textSize="20sp" /> </RelativeLayout> 

So my problem is that centering the heading only works if I have 0 taskbar elements next to it, as soon as I add the element, the centering will be screwed up. It would also be great if I could use the free access button with it (which also twists the centering).

Any help would be appreciated.

I do not want to implement my own control element / button logic.

+4
source share
3 answers

You can try this in TextView:

 <TextView android:id="@+id/actionbar_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:maxHeight="1dp" android:maxLines="1" android:paddingLeft="48dp" android:paddingRight="48dp" android:text="Bacon ipsum" android:textColor="#777777" android:textSize="20sp" /> 
+3
source
  • Remove the startup icon from your RelativeLayout.
  • Change the width of the RelativeLayout parameter to wrap_content
  • Delete

    actionBar.setDisplayShowCustomEnabled (true); actionBar.setDisplayShowHomeEnabled (false); actionBar.setDisplayShowTitleEnabled (false);

  • Add

     ActionBar.LayoutParams lp = new ActionBar.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, Gravity.CENTER); actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM) actionBar.setCustomView(yourInflatedCustomView, lp); 
+2
source

Have you tried to implement the text of this code?

 android:layout_centerHorizontal="true" 
0
source

Source: https://habr.com/ru/post/1480483/


All Articles