【问题标题】:How to set the title of action bar in center in Android如何在Android中设置动作栏的标题
【发布时间】:2014-09-23 18:36:12
【问题描述】:

您好,我正在开发演示应用程序,我需要在其中心设置 ActionBar 的标题。我尝试了一些对我不起作用的 SO 帖子。我正在使用 Android 4.3 手机。

https://stackoverflow.com/a/21770534/2455259 , https://stackoverflow.com/a/19244633/2455259

但没有什么对我有用。有没有办法使用 appcompat 将 ActionBar 的标题设置为 Android 中心?

我正在这样做,但标题仍然在左侧。

center_action_bar_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" />

活动中

TextView customView = (TextView)
        LayoutInflater.from(getActivity()).inflate(R.layout.center_action_bar_title,
        null);

 ActionBar.LayoutParams params = new 
         ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT, 
         ActionBar.LayoutParams.MATCH_PARENT, Gravity.CENTER); 

    customView.setText(text);
((ActionBarActivity)getActivity()).getSupportActionBar().setCustomView(customView, params);

【问题讨论】:

    标签: android android-actionbar android-appcompat android-actionbar-compat


    【解决方案1】:

    我知道这个问题很老了,但我找到了解决方案。

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

    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);
            }
        }
    }
    

    然后,只需拨打您的onCreate():

    centerTitle();
    

    就是这样。就这么简单。
    希望它可以帮助某人。

    【讨论】:

    • 我设置了 onResume 是因为 onCreate 的 UI 可能已经准备好了。
    【解决方案2】:

    我认为使用自定义操作栏是自定义它的最佳方式! 您应该首先为操作栏构建 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>
    

    那么你应该使用 LayoutInflater 来参考这个布局:

    ViewGroup actionBarLayout = (ViewGroup) youractivity.getLayoutInflater().inflate(R.layout.nameofxmlfile, null);
    

    最后,您应该将其设置为操作栏的布局:

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

    【讨论】:

    • 不,我不想让它完全自定义操作栏。我只希望标题出现在中心,其余功能与操作栏相同
    • 其余功能是什么意思?如果您将此代码用作自定义操作栏,其余功能将保持与操作栏相同!
    • 我的意思是操作栏操作项出现在右端,导航抽屉图标打开和关闭。它会像操作栏一样保持不变还是我需要自定义所有内容?我已接受您的回答,请确认。 :)
    • 不,它将保持与操作栏一样:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-02
    • 2011-01-25
    • 1970-01-01
    • 2023-03-13
    相关资源
    最近更新 更多