Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/198.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 Coordinatorlayout - Fatal编程技术网

android:如何在折叠工具栏中添加带有文本的按钮

android:如何在折叠工具栏中添加带有文本的按钮,android,android-coordinatorlayout,Android,Android Coordinatorlayout,如何实现以下布局。我可以实现没有添加按钮。但是如何添加添加按钮和添加按钮应该随着图像的视差在向上滚动时消失 我发现浮动操作按钮没有添加文本的功能。我只能用按钮 不带“添加”按钮的我的xml布局: <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res

如何实现以下布局。我可以实现没有添加按钮。但是如何添加添加按钮和添加按钮应该随着图像的视差在向上滚动时消失

我发现浮动操作按钮没有添加文本的功能。我只能用按钮

不带“添加”按钮的我的xml布局:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layoutplace1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <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:fitsSystemWindows="true">

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

                <ImageView
                    android:id="@+id/header"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    app:layout_collapseMode="parallax" />

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


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

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

        <FrameLayout
            android:id="@+id/framelayout"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_gravity="bottom"

            app:layout_anchor="@+id/appBarLayout"
            app:layout_anchorGravity="bottom"
            app:layout_collapseMode="none">

            <android.support.design.widget.TabLayout
                android:id="@+id/tabLayout"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:background="#ffffff"
                android:gravity="bottom"
                android:textAllCaps="false"
                android:theme="@style/MyCustomTabLayout"
                app:tabGravity="center"
                app:tabIndicatorColor="#574ec1"
                app:tabIndicatorHeight="2dp"
                app:tabMode="scrollable"
                app:tabSelectedTextColor="#574ec1"
                app:tabTextColor="#8A000000" />

        </FrameLayout>

        <android.support.v4.view.ViewPager
            android:id="@+id/viewPager"
            android:layout_width="match_parent"
            android:layout_height="fill_parent"
            android:paddingBottom="56dp"
            android:layout_marginTop="50dp"

            app:layout_behavior="@string/appbar_scrolling_view_behavior" />

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

    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_viewplace1"
        android:layout_height="match_parent"
        android:layout_width="wrap_content"
        android:layout_gravity="start"
        android:fitsSystemWindows="false"
        app:itemTextColor="#8A000000"
        app:itemIconTint="#8A000000"
        app:menu="@menu/drawer_view" />
</android.support.v4.widget.DrawerLayout>
但标题只显示了部分内容,显示了


相反,我希望它以多行显示。有没有可能像这样添加按钮和图像

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

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

        <ImageView
            android:id="@+id/header"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_collapseMode="parallax" />

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

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="bottom|end"
            app:layout_collapseMode="parallax"
            android:orientation="horizontal">
            <Button
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Button"/>
        </LinearLayout>
    </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.CollapsingToolbarLayout
        android:id="@+id/collapsing_toolbar"
        android:layout_width="match_parent"
        android:layout_height="150dip"
        android:fitsSystemWindows="true"
        app:contentScrim="?attr/colorPrimary"
        app:expandedTitleMarginBottom="20dp"
        app:expandedTitleMarginEnd="64dp"
        app:expandedTitleMarginStart="48dp"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

  <ImageView
            android:id="@+id/header"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_collapseMode="parallax" >

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

                         .......
                 </LinearLayout>

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

.......

要添加后退按钮(或任何其他按钮或图像),可以将其添加到工具栏。如果要始终显示此按钮,无论工具栏是否折叠,都可以将此行添加到工具栏:

                app:layout_collapseMode="pin"
如果希望工具栏折叠时按钮消失,请将此行替换为:

                app:layout_collapseMode="parallax"
然后,您可以从activity/fragment中按id找到此按钮/图像,并将侦听器添加到其中。工具栏的完整代码:

    <com.google.android.material.appbar.AppBarLayout
    android:id="@+id/appBarLayout"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:fitsSystemWindows="true"
    android:theme="@style/ThemeOverlay.AppCompat.Dark">

    <com.google.android.material.appbar.CollapsingToolbarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_scrollFlags="scroll|snap|exitUntilCollapsed"
        app:contentScrim="@color/colorPrimary"
        android:fitsSystemWindows="true"
        app:title="@string/app_name">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="250dp"
            android:src="@drawable/tech"
            android:scaleType="centerCrop"/>

        <androidx.appcompat.widget.Toolbar
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_collapseMode="parallax"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Dark">

            <ImageView
                android:id="@+id/toolbar_back_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/ic_back_arrow_left"/>
        </androidx.appcompat.widget.Toolbar>

    </com.google.android.material.appbar.CollapsingToolbarLayout>

</com.google.android.material.appbar.AppBarLayout>


我试过了。文本和按钮仅添加在顶部高度(?attr/actionBarSize)。它们并不像我建议的那样添加到工具栏内部,而是添加到工具栏内部和外部。在折叠工具栏布局中,检查看起来更接近我想要的。还可以告诉您如何获取什么的text properlytext属性吗?您是否希望文本视图也带有按钮?是的。如何得到整个东西“乌杜比室利克里希纳神庙”和按钮。我在线性布局中添加了一个文本视图,得到了它。谢谢,我有一个疑问。如果我在右边有一个工具栏图标,那么按钮就会重叠在上面。当我向上滚动时,按钮是否可能消失。
    <com.google.android.material.appbar.AppBarLayout
    android:id="@+id/appBarLayout"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:fitsSystemWindows="true"
    android:theme="@style/ThemeOverlay.AppCompat.Dark">

    <com.google.android.material.appbar.CollapsingToolbarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_scrollFlags="scroll|snap|exitUntilCollapsed"
        app:contentScrim="@color/colorPrimary"
        android:fitsSystemWindows="true"
        app:title="@string/app_name">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="250dp"
            android:src="@drawable/tech"
            android:scaleType="centerCrop"/>

        <androidx.appcompat.widget.Toolbar
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_collapseMode="parallax"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Dark">

            <ImageView
                android:id="@+id/toolbar_back_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/ic_back_arrow_left"/>
        </androidx.appcompat.widget.Toolbar>

    </com.google.android.material.appbar.CollapsingToolbarLayout>

</com.google.android.material.appbar.AppBarLayout>