如何更改CollapsingToolbarLayout标题的方向?

8

我在我的项目中使用了Design Support Library,并希望将CollapsingToolbarLayout的标题方向从ltr更改为rtl(如下所示)。我该怎么做?

CollapsingToolbarLayout's title is right to left

我检查了这个链接,但没有找到有用的内容。这个设计的布局还没有准备好,但这是一个类似的xml:
<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="@dimen/detail_backdrop_height"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    android:fitsSystemWindows="true">

    <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"
        android:fitsSystemWindows="true"
        app:contentScrim="?attr/colorPrimary"
        app:expandedTitleMarginStart="48dp"
        app:expandedTitleMarginEnd="64dp">

        <ImageView
            android:id="@+id/backdrop"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            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>

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

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:paddingTop="24dp">

        <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/card_margin">

            <LinearLayout
                style="@style/Widget.CardContent"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Info"
                    android:textAppearance="@style/TextAppearance.AppCompat.Title" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/cheese_ipsum" />

            </LinearLayout>

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

        <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="@dimen/card_margin"
            android:layout_marginLeft="@dimen/card_margin"
            android:layout_marginRight="@dimen/card_margin">

            <LinearLayout
                style="@style/Widget.CardContent"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Friends"
                    android:textAppearance="@style/TextAppearance.AppCompat.Title" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/cheese_ipsum" />

            </LinearLayout>

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

        <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="@dimen/card_margin"
            android:layout_marginLeft="@dimen/card_margin"
            android:layout_marginRight="@dimen/card_margin">

            <LinearLayout
                style="@style/Widget.CardContent"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Related"
                    android:textAppearance="@style/TextAppearance.AppCompat.Title" />

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="@string/cheese_ipsum" />

            </LinearLayout>

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

    </LinearLayout>

</android.support.v4.widget.NestedScrollView>

<android.support.design.widget.FloatingActionButton
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    app:layout_anchor="@id/appbar"
    app:layout_anchorGravity="bottom|right|end"
    android:src="@drawable/ic_discuss"
    android:layout_margin="@dimen/fab_margin"
    android:clickable="true"/>


1
请发布您的XML。 - Pratik Butani
你也可以通过将视图强制为RTL来改变方向。getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL); 但是对于API低于17的情况,这种方法不起作用。 - hadi
@hadi,那段代码在这种情况下不起作用。 - Loolooii
2个回答

14

最后,我发现我可以在XML和Java中都这样做: XML代码中的这些属性

<android.support.design.widget.CollapsingToolbarLayout
   app:collapsedTitleGravity="right"
   app:expandedTitleGravity="right"

在Java中,这些代码是:

collapsingToolbar.setExpandedTitleGravity(Gravity.RIGHT);
collapsingToolbar.setCollapsedTitleGravity(Gravity.RIGHT);

可以改变标题的方向。


1
请注意,建议同时添加垂直重力:对于collapsedTitleGravity,请添加center_vertical,对于expandedTitleGravity,请添加bottom。 - android developer
1
我不明白为什么“start”值不起作用。对于从右到左的语言,它应该在右边,在其他语言中应该在左边。但它仍然在左边。 - ATom
@ATom,也许你可以在你的清单文件中添加“android:supportsRtl="true"”。 - Hamed Ghadirian
@HamedGh 我在清单文件中也添加了android:supportsRtl="true",但仍然无效。 - varotariya vajsi

1

我从xml中删除了ExpandedTitleGravity和CollapsedTitleGravity,并检查了布局的方向,然后以以下方式通过编程向collapsingToolbarLayout添加gravity。

请确保在您的条件语句中正确获取方向,有时它可能无法给出正确的方向。

    if (ViewCompat.getLayoutDirection("your_view_here") == ViewCompat.LAYOUT_DIRECTION_LTR) {
        collapsingToolbarLayout.setExpandedTitleGravity(Gravity.START | Gravity.BOTTOM);
    } else {
        collapsingToolbarLayout.setExpandedTitleGravity(Gravity.END | Gravity.BOTTOM);
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接