How to align title at center of ActionBar in default theme(Theme.Holo.Light)

后端 未结 12 2119
野性不改
野性不改 2020-11-27 12:08

I searched a lot but can not find a way, How can I set then title at center of ActionBar instead of left aligned. I used below code to set the title at center :



        
相关标签:
12条回答
  • 2020-11-27 12:34

    solution is based on these things:

    • you need to use your own class that extends Toolbar (support.v7.widget.toolbar)
    • you need to override one method Toolbar#addView

    what does it do:

    when first time toolbar is executing #setTitle, it creates AppCompatTextView and uses it to display title text.

    when the AppCompatTextView is created, toolbar (as ViewGroup), adds it into it's own hierarchy with #addView method.

    also, while trying to find solution i noticed that the textview has layout width set to "wrap_content", so i decided to make it "match_parent" and assign textalignment to "center".

    MyToolbar.kt, skipping unrelated stuff (constructors/imports):

    class MyToolbar : Toolbar {
    
      override fun addView(child: View, params: ViewGroup.LayoutParams) {
        if (child is TextView) {
            params.width = ViewGroup.LayoutParams.MATCH_PARENT
            child.textAlignment= View.TEXT_ALIGNMENT_CENTER
        }
        super.addView(child, params)
      }
    }
    

    possible "side effects" - this will apply to "subtitle" too

    0 讨论(0)
  • 2020-11-27 12:35

    if You are Using Toolbar the just Add

    android:layout_gravity="center_horizontal"

    Under the Toolbar Just like this snippet

     <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">
    
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay">
    
            <TextView
                android:id="@+id/tvTitle"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"    //Important 
                android:textColor="@color/whitecolor"
                android:textSize="20sp"
                android:textStyle="bold" />
        </android.support.v7.widget.Toolbar>
    
    </android.support.design.widget.AppBarLayout>
    
    0 讨论(0)
  • 2020-11-27 12:39

    Java code: write this in onCreate()
    getSupportActionBar().setDisplayShowCustomEnabled(true); getSupportActionBar().setCustomView(R.layout.action_bar);

    and for you custom view, simply use FrameLayout, east peasy!
    android.support.v7.widget.Toolbar is another option

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left|center_vertical"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="@string/app_name"
            android:textColor="@color/black"
            android:id="@+id/textView" />
    </FrameLayout>
    
    0 讨论(0)
  • 2020-11-27 12:41

    You can create a custom layout and apply it to the actionBar.

    To do so, follow those 2 simple steps:

    1. Java Code

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

    Where R.layout.actionbar is the following layout.

    1. XML

      <?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:layout_gravity="center"
          android:orientation="vertical">
      
      <TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_gravity="center"
          android:id="@+id/action_bar_title"
          android:text="YOUR ACTIVITY TITLE"
          android:textColor="#ffffff"
          android:textSize="24sp" />
      </LinearLayout>
      

    It can be as complex as you want. Try it out!

    EDIT:

    To set the background you can use the property android:background in the container layout (LinearLayout in that case). You may need to set the layout height android:layout_height to match_parent instead of wrap_content.

    Moreover, you can also add a LOGO / ICON to it. To do so, simply add an ImageView inside your layout, and set layout orientation property android:orientation to horizontal (or simply use a RelativeLayout and manage it by yourself).

    To change the title of above custom action bar dynamically, do this:

    TextView title=(TextView)findViewById(getResources().getIdentifier("action_bar_title", "id", getPackageName()));
    title.setText("Your Text Here");
    
    0 讨论(0)
  • 2020-11-27 12:41

    This actually works:

     getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
     getActionBar().setCustomView(R.layout.custom_actionbar);
      ActionBar.LayoutParams p = new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
            p.gravity = Gravity.CENTER;
    

    You have to define custom_actionbar.xml layout which is as per your requirement e.g. :

    <?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="50dp"
        android:background="#2e2e2e"
        android:orientation="vertical"
        android:gravity="center"
        android:layout_gravity="center">
    
        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/top_banner"
            android:layout_gravity="center"
            />
    </LinearLayout>
    
    0 讨论(0)
  • 2020-11-27 12:43

    After two days of going through the web, this is what I came up with in Kotlin. Tested and works on my app

      private fun setupActionBar() {
        supportActionBar?.apply {
            displayOptions = ActionBar.DISPLAY_SHOW_CUSTOM
            displayOptions = ActionBar.DISPLAY_SHOW_TITLE
            setDisplayShowCustomEnabled(true)
            title = ""
    
    
            val titleTextView = AppCompatTextView(this@MainActivity)
            titleTextView.text = getText(R.string.app_name)
            titleTextView.setSingleLine()
            titleTextView.textSize = 24f
            titleTextView.setTextColor(ContextCompat.getColor(this@MainActivity, R.color.appWhite))
    
            val layoutParams = ActionBar.LayoutParams(
                ActionBar.LayoutParams.WRAP_CONTENT,
                ActionBar.LayoutParams.WRAP_CONTENT
            )
            layoutParams.gravity = Gravity.CENTER
            setCustomView(titleTextView, layoutParams)
    
            setBackgroundDrawable(
                ColorDrawable(
                    ContextCompat.getColor(
                        this@MainActivity,
                        R.color.appDarkBlue
                    )
                )
            )
    
        }
    }
    
    0 讨论(0)
提交回复
热议问题