如何在Android中设置中心操作栏的标题

如何在Android中设置中心操作栏的标题,android,android-actionbar,android-appcompat,android-actionbar-compat,Android,Android Actionbar,Android Appcompat,Android Actionbar Compat,您好,我正在开发演示应用程序,我需要在其中心的ActionBar中设置标题。我试过一些帖子,所以对我不起作用。我正在试用安卓4.3手机 , 但对我来说什么都不管用。有没有办法使用appcompat在Android中将ActionBar的标题设置为中心 我这样做就像下面,但标题仍然在左边 center\u action\u bar\u title.xml <?xml version="1.0" encoding="utf-8"?> <TextView xmlns:andr

您好,我正在开发演示应用程序,我需要在其中心的
ActionBar
中设置标题。我试过一些帖子,所以对我不起作用。我正在试用安卓4.3手机

,

但对我来说什么都不管用。有没有办法使用appcompat在Android中将
ActionBar
的标题设置为中心

我这样做就像下面,但标题仍然在左边

center\u action\u bar\u title.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="18sp" />

我认为使用自定义操作栏是定制它的最好方式! 首先,您应该为操作栏构建xml文件,下面是一个示例:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/custom_actionbar_relative_layout_main"
    android:layout_width="match_parent"
    android:layout_height="48dp"
    android:background="@color/actionbar_background"
    android:enabled="false" >

    <TextView
        android:id="@+id/textviewactivityname"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_centerInParent="true"
        android:layout_centerVertical="true"
        android:background="@android:color/transparent"
        android:gravity="center_vertical"
        android:text="text"
        android:textAlignment="center"
        android:textColor="@android:color/white"
        android:textSize="18sp" />

</RelativeLayout>
最后,您应该将其设置为actionbar的布局:

ActionBar actionBar = youractivity.getSupportActionBar();
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setCustomView(actionBarLayout);

我认为使用自定义操作栏是定制它的最好方式! 首先,您应该为操作栏构建xml文件,下面是一个示例:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/custom_actionbar_relative_layout_main"
    android:layout_width="match_parent"
    android:layout_height="48dp"
    android:background="@color/actionbar_background"
    android:enabled="false" >

    <TextView
        android:id="@+id/textviewactivityname"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_centerInParent="true"
        android:layout_centerVertical="true"
        android:background="@android:color/transparent"
        android:gravity="center_vertical"
        android:text="text"
        android:textAlignment="center"
        android:textColor="@android:color/white"
        android:textSize="18sp" />

</RelativeLayout>
最后,您应该将其设置为actionbar的布局:

ActionBar actionBar = youractivity.getSupportActionBar();
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setCustomView(actionBarLayout);

我知道这个问题很老了,但我还是找到了解决办法

不需要您创建自定义操作栏的解决方案

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);
        }
    }
}
就这样。这很简单。

希望它能帮助别人。

我知道这个问题很老了,但我找到了解决办法

不需要您创建自定义操作栏的解决方案

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);
        }
    }
}
就这样。简单。

希望它能帮助别人。

不,我不想让它完全成为定制动作栏。我只希望标题显示在中间,其他功能保持与like ActionBar相同您所说的其他功能是什么意思?如果您将此代码用作自定义操作栏,其他功能将保持与like ActionBar相同!我的意思是,位于的操作栏操作项显示在右端,导航抽屉图标用于打开和关闭。它会和动作栏一样吗?还是我需要定制所有东西?我已接受你的答复,请确认。:)不,它将保持像动作栏:)不,我不想让它完全定制动作栏。我只希望标题显示在中间,其他功能保持与like ActionBar相同您所说的其他功能是什么意思?如果您将此代码用作自定义操作栏,其他功能将保持与like ActionBar相同!我的意思是,位于的操作栏操作项显示在右端,导航抽屉图标用于打开和关闭。它会和动作栏一样吗?还是我需要定制所有东西?我已接受你的答复,请确认。:)不,它将保持像操作栏一样:)我加入onResume是因为onCreate可能已经准备好了UI。我加入onResume是因为onCreate可能已经准备好了UI。