Center Action bar title

耗尽温柔 提交于 2019-12-22 00:18:11

问题


How can I center activity's action bar title in Android?

I've seen MANY questions on SO for this specific topic. And every answer comes back to "using custom view" and having your own toolbar.

I've found a solution that works without creating a custom view.


回答1:


Have this method in your Activity:

private void centerTitle() {
    ArrayList<View> textViews = new ArrayList<>();

    getWindow().getDecorView().findViewsWithText(textViews, getTitle(), View.FIND_VIEWS_WITH_TEXT);

    if(textViews.size() > 0) {
        AppCompatTextView appCompatTextView = null;
        if(textViews.size() == 1) {
            appCompatTextView = (AppCompatTextView) textViews.get(0);
        } else {
            for(View v : textViews) {
                if(v.getParent() instanceof Toolbar) {
                    appCompatTextView = (AppCompatTextView) v;
                    break;
                }
            }
        }

        if(appCompatTextView != null) {
            ViewGroup.LayoutParams params = appCompatTextView.getLayoutParams();
            params.width = ViewGroup.LayoutParams.MATCH_PARENT;
            appCompatTextView.setLayoutParams(params);
            appCompatTextView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
        }
    }
}

Then, just call it in your onCreate():

centerTitle();

That's it!!!




回答2:


In Kotlin you can do :

private fun centerTitle() {
        val textViews = ArrayList<View>()
        window.decorView.findViewsWithText(textViews, title, View.FIND_VIEWS_WITH_TEXT)
        if (textViews.size > 0) {
            var appCompatTextView: AppCompatTextView? = null
            if (textViews.size == 1)
                appCompatTextView = textViews[0] as AppCompatTextView
            else {
                for (v in textViews) {
                    if (v.parent is Toolbar) {
                        appCompatTextView = v as AppCompatTextView
                        break
                    }
                }
            }
            if (appCompatTextView != null) {
                val params = appCompatTextView.layoutParams
                params.width = ViewGroup.LayoutParams.MATCH_PARENT
                appCompatTextView.layoutParams = params
                appCompatTextView.textAlignment = View.TEXT_ALIGNMENT_CENTER
            }
        }
    }

And call centerTitle() in your onCreate()




回答3:


You can use this solution. But it without ignoring another elements in your Toolbar.

fun setToolbarTextAlignment(textAlignment: Int) {
    for (i in 0..toolbarView.childCount) {
        (toolbarView.getChildAt(i) as? TextView)?.let {
            it.layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT
            it.textAlignment = textAlignment
        }
    }
}



回答4:


 <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" >

          <TextView
              android:layout_width="match_parent"
              android:layout_height="match_parent" 
              android:gravity="center"
              android:text="haiii"/>
 </android.support.v7.widget.Toolbar>


来源:https://stackoverflow.com/questions/42465386/center-action-bar-title

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!