如何保持标题和字幕工具栏的位置android?

时间:2015-09-07 02:21:13

标签: android android-toolbar

我的工具栏安卓有问题,我想保留标题和字幕工具栏android的位置,当折叠或展开收费栏时这样: enter image description here

这是我当前的例子

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/coordinatorLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.design.widget.AppBarLayout
            android:id="@+id/appBarLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

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

 <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
                app:layout_collapseMode="pin"
                app:layout_scrollFlags="scroll|enterAlways">

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

            </android.support.design.widget.CollapsingToolbarLayout>
            </android.support.design.widget.AppBarLayout>
            <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">



        </LinearLayout>

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

</RelativeLayout>

那怎么样?

2 个答案:

答案 0 :(得分:0)

我知道这是一个老问题,这个xml适用于我,希望它有所帮助!

<android.support.design.widget.CoordinatorLayout
android:id="@+id/coordinatorLayout"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:fitsSystemWindows="true">

<android.support.design.widget.AppBarLayout
    android:id="@+id/layout_bar"
    android:layout_width="match_parent"
    android:layout_height="192dp">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapse_toolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:popupTheme="@style/ThemeOverlay.AppCompat.Dark"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:contentScrim="#FFf3802b"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

        <ImageView
            android:id="@+id/brandLogo"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:adjustViewBounds="true"
            android:contentDescription="@string/logo_icon"
            android:scaleType="centerCrop"
            android:src="@drawable/avatar_krono"
            app:layout_collapseMode="parallax"/>

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

            <LinearLayout
                android:id="@+id/header_text_layout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="bottom"
                android:background="#00FFFFFF"
                android:gravity="center_vertical"
                android:orientation="vertical"
                android:paddingRight="10dp">

                <TextView
                    android:id="@+id/title"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="My title"
                    android:textColor="@android:color/white"
                    android:textSize="24sp"/>

                <TextView
                    android:id="@+id/subtitle"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="My subtitle"
                    android:textColor="@android:color/white"
                    android:textSize="17sp"/>
            </LinearLayout>

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

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

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

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

...

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

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

答案 1 :(得分:-1)

您必须在布局中创建自己的TextView包装并将其作为子项添加到工具栏,您必须自定义外观

      <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
            app:layout_collapseMode="pin"
            app:layout_scrollFlags="scroll|enterAlways">

            <LinearLayout android:orientation="vertical"
                 android:layout_width="match_parent"
                 android:layout_height="match_parent">
                 <TextView/>
                 <TextView/>
            </LinearLayout>

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