简体   繁体   English

为什么折叠工具栏标题在工具栏下?

[英]Why collapsing toolbar title is under toolbar?

My activity design like below: 我的活动设计如下:

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

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

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

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:background="#99000026"
            android:fitsSystemWindows="true"
            android:orientation="horizontal"
            android:weightSum="10">

            <ImageView
                android:id="@+id/detail_movie_poster"
                android:layout_width="90dp"
                android:layout_height="110dp"
                android:layout_margin="10dp"
                android:layout_weight="1"
                android:scaleType="centerInside"
                android:src="@drawable/no_movie_image" />

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_weight="9"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/detail_movie_name"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:ellipsize="marquee"
                    android:focusable="true"
                    android:focusableInTouchMode="true"
                    android:marqueeRepeatLimit="marquee_forever"
                    android:singleLine="true"
                    android:textColor="@android:color/white"
                    android:textSize="26sp" />

                <TextView
                    android:id="@+id/detail_movie_original_name"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:textSize="16sp" />

                <TextView
                    android:id="@+id/detail_movie_genres"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:singleLine="true"
                    android:textSize="14sp" />

            </LinearLayout>

        </LinearLayout>

        <android.support.v7.widget.Toolbar
            android:id="@+id/movie_detail_toolbar"
            android:layout_width="match_parent"
            android:layout_height="65dp"
            android:fitsSystemWindows="true"
            app:layout_collapseMode="pin"
            app:popupTheme="@style/ThemeOverlay.AppCompat.ActionBar" />


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


    <android.support.design.widget.TabLayout
        android:id="@+id/movie_detail_tabs"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:layout_gravity="bottom"
        app:tabIndicatorColor="@android:color/white"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

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

    <android.support.v4.view.ViewPager
        android:id="@+id/movie_detail_viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

But design happened like that. 但设计就是这样发生的。 Collapsing toolbar title is under the toolbar. 折叠工具栏标题位于工具栏下方。

在此输入图像描述

Also I used this initCollapsingToolbar() for dynamically collapsing toolbar title : 我还使用这个initCollapsingToolbar()来动态折叠工具栏标题:

private void initCollapsingToolbar() {
    final CollapsingToolbarLayout collapsingToolbar = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
collapsingToolbar.setTitle(" ");
AppBarLayout appBarLayout = (AppBarLayout) findViewById(R.id.appbar);
appBarLayout.setExpanded(true);

// hiding & showing the title when toolbar expanded & collapsed
appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
    boolean isShow = false;
    int scrollRange = -1;

    @Override
    public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
        if (scrollRange == -1) {
            scrollRange = appBarLayout.getTotalScrollRange();
        }
        if (scrollRange + verticalOffset == 0) {
            Results results = intent.getParcelableExtra(Constant.PARCELABLE_MOVIE_TITLE);
                collapsingToolbarLayout.setTitle(results.getTitle());
            isShow = true;
        } else if (isShow) {
            collapsingToolbar.setTitle(" ");
            isShow = false;
        }
       }
    });
}

I solved this problem. 我解决了这个问题。 My solution is here 我的解决方案在这里

Firstly i added app:titleEnabled="false" to CollapsingToolbar 首先,我将app:titleEnabled="false"CollapsingToolbar

<android.support.design.widget.CollapsingToolbarLayout
    android:id="@+id/movie_detail_collapsing_toolbar"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    app:contentScrim="?attr/colorPrimary"
    app:layout_scrollFlags="scroll|exitUntilCollapsed"
    app:titleEnabled="false">

and then change collapsingToolbar.setTitle(); 然后更改collapsingToolbar.setTitle(); to getSupportActionBar().setTitle(); to getSupportActionBar().setTitle(); in initCollapsingToolbar() initCollapsingToolbar()

and finally added app:titleMarginTop="14dp" to toolbar 最后将app:titleMarginTop="14dp"toolbar

Maybe this solution is bad way but that's all I can do. 也许这个解决方案很糟糕,但这就是我所能做的。

android:layout_height="match_parent"android:layout_height="match_parent"更改为android:layout_height="wrap_content"

这似乎只是在模拟器中的错误 ,真正的设备是好的。

I had this problem. 我有这个问题。 The toolbar title was cutting off from bottom. 工具栏标题从底部切断。 Adding some arbitrary bottom margin i in <toolbar> . <toolbar>添加一些任意底边距i。

Added app:titleMarginBottom="44dp" to <toolbar> 添加了app:titleMarginBottom="44dp"<toolbar>

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM