【问题标题】:How work the collapsing toolbar layout? And how to personalize it when toolbar collapse?折叠工具栏布局如何工作?工具栏折叠时如何个性化?
【发布时间】:2017-01-05 17:16:24
【问题描述】:

这是我的问题:我需要折叠所有包含一些文本字段、按钮和线性布局的协调布局;但是当我实现我的代码(下面的代码)时,折叠并没有达到我的预期。我希望在not collapsed layout 上显示所有布局,当用户向下滚动时,折叠工具栏布局应显示一个布局,其中包含:“partenza: ... , arrivo: ...”代替标题。

有什么想法吗?

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.design.widget.AppBarLayout
    android:id="@+id/allAppBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimaryDark"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimaryDark"
        android:subtitle="@string/subtitle_app"
        android:title="@string/app_name"
        app:popupTheme="@style/AppTheme.PopupOverlay" />


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimaryDark"
        android:descendantFocusability="beforeDescendants"
        android:focusableInTouchMode="true"
        android:orientation="vertical"
        android:paddingBottom="5dp"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:paddingTop="5dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <LinearLayout
                android:layout_width="315dp"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:theme="@style/InputTheme"
                android:weightSum="2">

                <RelativeLayout
                    android:id="@+id/inputPartenzaGroupBox"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_weight="1">

                    <Button
                        android:id="@+id/clearPartenzaBox"
                        android:layout_width="50dp"
                        android:layout_height="wrap_content"
                        android:layout_alignParentRight="true"
                        android:layout_centerVertical="true"
                        android:layout_marginRight="5dip"
                        android:background="@android:color/transparent" />

                    <android.support.design.widget.TextInputLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:textColorHint="@color/colorAccent">

                        <AutoCompleteTextView
                            android:id="@+id/tBoxPartenza"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:backgroundTint="@color/colorAccent"
                            android:hint="@string/example_text_1"
                            android:maxLines="1"
                            android:singleLine="true"
                            android:textColor="@color/white" />

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

                </RelativeLayout>

                <RelativeLayout
                    android:id="@+id/inputArrivoGroupBox"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@id/inputPartenzaGroupBox"
                    android:layout_weight="1">


                    <Button
                        android:id="@+id/clearArrivoBox"
                        android:layout_width="50dp"
                        android:layout_height="wrap_content"
                        android:layout_alignParentRight="true"
                        android:layout_centerVertical="true"
                        android:layout_marginRight="5dip"
                        android:background="@android:color/transparent" />

                    <android.support.design.widget.TextInputLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:textColorHint="@color/colorAccent">

                        <AutoCompleteTextView
                            android:id="@+id/tBoxArrivo"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:backgroundTint="@color/colorAccent"
                            android:hint="@string/example_text_2"
                            android:maxLines="1"
                            android:singleLine="true"
                            android:textColor="@color/white" />

                    </android.support.design.widget.TextInputLayout>
                </RelativeLayout>
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <ImageButton
                    android:id="@+id/btnInvertPosition"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_gravity="center"
                    android:background="@android:color/transparent"
                    android:src="@drawable/ic_arrow" />
            </LinearLayout>
        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <Button
                android:id="@+id/btnSearchBus"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:background="@android:color/transparent"
                android:gravity="center"
                android:text="@string/btn_text_search"
                android:textColor="@color/colorAccent"
                android:theme="@style/ThemeOverlay.AppCompat.Light" />

        </LinearLayout>

    </LinearLayout>

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

<ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/mainList"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

</ListView>

【问题讨论】:

  • 你的可折叠布局的java代码是什么?
  • @VivekSolanki 现在没有,我只想知道是否可以做类似的事情
  • 您可以在工具栏下方使用两个布局并切换它们的可见性...要显示折叠布局,只需删除操作栏的标题并将第一个布局设置为您的 TextViews 等可见。当您想要展开,将可见性切换为消失,并使第二个布局可见。为了有一个动画,只需设置工具栏的高度并为其设置动画。它可能会帮助您处理这个previous answer with a expanding/collapsing animation 以获得另一种效果。

标签: android android-layout android-collapsingtoolbarlayout


【解决方案1】:

是的,这是可能的! 对于简单的可折叠布局,您可以执行以下操作:

final TextView descriptionText = (TextView) view.findViewById(R.id.detail_description_content);
        final TextView showAll = (TextView) view.findViewById(R.id.detail_read_all);
        showAll.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                showAll.setVisibility(View.INVISIBLE);

                descriptionText.setMaxLines(Integer.MAX_VALUE);
            }
        });

XML:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:id="@+id/detail_description_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >

        <TextView
            android:id="@+id/detail_description_content"
            android:maxLines="5"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
        <TextView
            android:id="@+id/detail_read_all"
            android:clickable="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </LinearLayout>
</ScrollView>

也检查this lib

希望这会有所帮助!

【讨论】:

  • 哇,它看起来很神奇,但还有一个问题,折叠的概念对于工具栏是相同的,或者有必要以另一种方式重新定义所有代码。因为起初我的想法是创建一个可折叠的工具栏布局? @VivekSolanki
  • 对不起!我不太清楚。但据我所知,您将需要动态/onscrollevent 的动画,工具栏的折叠。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多