Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何使折叠工具栏布局的标题居中?_Android_Android Collapsingtoolbarlayout - Fatal编程技术网

Android 如何使折叠工具栏布局的标题居中?

Android 如何使折叠工具栏布局的标题居中?,android,android-collapsingtoolbarlayout,Android,Android Collapsingtoolbarlayout,我尝试了setExpandTitleTextAppearance,但没有成功。我想将展开的标题文本居中。@Javed,如果我错了,请纠正我,您想让标题在工具栏中居中,然后CollasingToolbarLayout被折叠,您的布局是这样的,对吗 <android.support.design.widget.CollapsingToolbarLayout android:id="@+id/collapsing_toolbar" android:layo

我尝试了
setExpandTitleTextAppearance
,但没有成功。我想将展开的标题文本居中。

@Javed,如果我错了,请纠正我,您想让标题在工具栏中居中,然后CollasingToolbarLayout被折叠,您的布局是这样的,对吗

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsing_toolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
        app:contentScrim="?attr/colorPrimary"
        app:expandedTitleMarginStart="48dp"
        app:expandedTitleMarginEnd="64dp"
        android:fitsSystemWindows="true">

        <ImageView
            android:id="@+id/image"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:scaleType="centerCrop"
            android:fitsSystemWindows="true"
            app:layout_collapseMode="parallax"/>

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:layout_collapseMode="pin" />

    </android.support.design.widget.CollapsingToolbarLayout>
关键是工具栏中的TextView具有宽度属性“Wrap Content”,所以我们需要将其更改为“Match Parent”。 (请参阅有关此反射的更多信息)


在安卓5.1.1和安卓4.3上测试(应该在任何地方都可以使用)

有一个属性
expandedTitleGravity
,您可以将其与折叠工具栏布局一起使用,以使展开的标题文本居中。将其添加到您的折叠工具栏布局:

app:expandedTitleGravity="bottom|center_horizontal"

在我的用例中,我将
app:titleEnabled
设置为false,反正我不需要它。在那之后,我的重力在工具栏布局中得到了适当的尊重。

作为Nguyễn Hoáng Anh在上面说,将
app:titleEnabled
设置为false就像一个符咒

启用该选项后,在使用布局检查器进行一些挖掘之后,可疑的未命名视图总是添加在
工具栏
内部
文本视图前面,就在“Up”按钮之后(如果启用)


因此,即使布局重力正常工作,一些可疑的视图也会占据
工具栏中的所有额外空间,以防您试图在可使用的折叠状态下将标题居中

android:paddingEnd="70dp"
android:paddingRight="70dp"
像这样:

<android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingEnd="70dp"
            android:paddingRight="70dp"
            app:collapsedTitleGravity="center_horizontal"        
            app:expandedTitleGravity="start"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin">

            </android.support.v7.widget.Toolbar>

        </android.support.design.widget.CollapsingToolbarLayout>

您可以在折叠的标题栏和折叠的标题栏中排列标题的位置 通过以下方式扩展状态

在扩张状态下

app:expandedTitleGravity="center" 
处于崩溃状态,

app:collapsedTitleGravity="center"

我认为这可能有助于您在折叠工具栏xml中包含这一点 对于倒塌的:
app:collapsedTitleGravity=“中心垂直|中心水平”

扩大
app:expandedTitleGravity=“center_vertical | center_horizontal”

向我们展示您已经尝试过的内容,这样更容易“帮助”我碰巧正在处理同一问题。在
工具栏中居中自定义标题
TextView
似乎应该可以,但由于某些原因
layout\u gravity=“center”
在这种情况下不会居中显示文本。
TextView
的文本改为右对齐。可能是一只虫子。@Ryan抓得好,喜欢它!修正了我的答案,并在安卓5.1.1和安卓4.3上进行了测试。谢谢你的通知!你的最新答案看起来应该有用,但我讨厌不得不使用反射。很遗憾,没有一个更精简的方法来解决这个问题。我希望我的自定义工具栏在中心显示标题。通过在CollasingToolbarLayout中设置此属性,我的需求得到了帮助。非常感谢。聪明的回答:)@androholic,
app:collapsedTitleGravity怎么样=“居中水平
,实际上不起作用。它从右侧添加了额外的空格,这不是最佳做法。它会破坏标题的动画效果。问题是,在
折叠
状态下,标题水平居中不当。左边有一个空白处。编辑:事实证明,你可以通过添加
android:paddingand:24dp
collavingToolbarLayout
@Ace
app:contentInsetStart=“0dp”
来修复它,这样更好:
app:collapsedTitleGravity="center"