我有一个用于工具栏的布局;
<?xml version="1.0" encoding="utf-8"?>
<merge
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="wrap_content">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/expanded_collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="120dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<android.support.v7.widget.Toolbar
android:id="@+id/expanded_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"/>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
然后在我的布局中使用它;
<com.dan.finance.ui.ExpandedToolbar
android:id="@+id/expandable_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:toolbarNavColor="?attr/NavigationIconColor"
app:toolbarNavIcon="?attr/NavigationUpArrow"
app:toolbarTitle="My Finances"
app:toolbarTitleColor="?attr/NavigationTitleColor"/>
在大多数布局下面,我都有一个嵌套的滚动视图,我的问题是布局上默认情况下内容不应该滚动,打开软键盘可以使用adjustResize来滚动内容,但是我的工具栏没有对此做出反应并应有的崩溃。
我的布局的完整代码是;
<?xml version="1.0" encoding="utf-8"?>
<layout 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">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.CoordinatorLayout
android:id="@+id/coordinator_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<com.dan.finance.ui.ExpandedToolbar
android:id="@+id/expandable_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:toolbarNavColor="?attr/NavigationIconColor"
app:toolbarNavIcon="?attr/NavigationUpArrow"
app:toolbarTitle="My Finances"
app:toolbarTitleColor="?attr/NavigationTitleColor"/>
<android.support.v4.widget.NestedScrollView
android:id="@+id/nested_scrollview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
app:layout_anchor="@id/expandable_toolbar"
app:layout_anchorGravity="bottom"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<android.support.constraint.ConstraintLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.AppCompatTextView
android:id="@+id/title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:paddingStart="32dp"
android:paddingEnd="0dp"
android:text="Finances"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<android.support.v7.widget.AppCompatEditText
android:id="@+id/edit_text"
android:layout_width="0dp"
android:layout_height="56dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/title"/>
<android.support.v7.widget.AppCompatTextView
android:id="@+id/details"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="DETAILS TODO"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/edit_text"/>
<android.support.v7.widget.RecyclerView android:id="@+id/finances_list"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layoutManager="android.support.v7.widget.LinearLayoutManager"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/details"/>
<android.support.v7.widget.AppCompatButton
android:id="@+id/button_see_all"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="See All"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@id/finances_list"
app:layout_constraintVertical_bias="1.0"/>
</android.support.constraint.ConstraintLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
</RelativeLayout>
此布局总体上不会在大型设备上滚动,但可能会在较小的设备/将来的版本上滚动,因此我认为这可能是我的问题所在,但我尝试了很多不同的方法,但未解决任何问题。我还尝试使用
以编程方式扩展和折叠工具栏mAppBarLayout.setExpanded(expand, true);
但这不会使我假设的布局具有动画效果,因为它不在滚动布局中,因为可能没有内容可显示?
答案 0 :(得分:2)
我在xml文件中使用了以下代码行,并且通过这种方式工作,可见和消失了软键盘
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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.CoordinatorLayout
android:id="@+id/coordinator_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/expanded_collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="120dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<android.support.v7.widget.Toolbar
android:id="@+id/expanded_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"/>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:id="@+id/nested_scrollview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
app:layout_anchor="@id/appbar_layout"
app:layout_anchorGravity="bottom"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<android.support.constraint.ConstraintLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.AppCompatTextView
android:id="@+id/title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:paddingStart="32dp"
android:paddingEnd="0dp"
android:text="Finances"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<android.support.v7.widget.AppCompatEditText
android:id="@+id/edit_text"
android:layout_width="0dp"
android:layout_height="56dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/title"/>
<android.support.v7.widget.AppCompatTextView
android:id="@+id/details"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="DETAILS TODO"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/edit_text"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/finances_list"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layoutManager="android.support.v7.widget.LinearLayoutManager"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/details"/>
<android.support.v7.widget.AppCompatButton
android:id="@+id/button_see_all"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="See All"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@id/finances_list"
app:layout_constraintVertical_bias="1.0"/>
</android.support.constraint.ConstraintLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
</RelativeLayout>
您还可以使用此侦听器签入活动AppBarLayout折叠/展开过程:
appbar_layout.addOnOffsetChangedListener(object : AppBarLayout.OnOffsetChangedListener {
override fun onOffsetChanged(p0: AppBarLayout?, p1: Int) {
if (Math.abs(p1) - appbar_layout.totalScrollRange == 0) {
Log.d("tag", "Collapsed")
} else {
Log.d("tag", "Expanded")
}
}
})
我认为这是因为您在自定义xml中使用AppBarLayout。
答案 1 :(得分:2)
AppBarLayout 必须是 CoordinatorLayout 的直接子级,才能滚动和折叠布局以按预期工作。 (请参见AppBarLayout documentation。)
此视图在很大程度上取决于在CoordinatorLayout中用作直接子级。如果您在其他ViewGroup中使用AppBarLayout,则大多数功能将无法使用。
这是您的布局当前所编码的样子。 (来自 Layout Inspector 。)
如您所见, AppBarLayout 不是 CoordinatorLayout 的直接子级,而是 ExpandedToolbar 的子级,它本身就是 AppBarLayout 。
要解决此问题,您需要将 expanded_toolbar.xml 更改为以下内容:
saveImageToServer
如您所见,我通过注释掉了 AppBarLayout 。现在,当我们运行应用程序时,我们看到以下层次结构:
在这里,您可以看到实际上是 AppBarLayout 的 ExpandedToolbar 是 CoordinatorLayout 的直接子代。这可行。这是视觉效果。我没有实现整个自定义布局-仅用于演示目的。
这是更新后的主布局:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<merge 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="wrap_content">
<!--<android.support.design.widget.AppBarLayout-->
<!--android:id="@+id/appbar_layout"-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="wrap_content"-->
<!--android:fitsSystemWindows="true">-->
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/expanded_collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="120dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<android.support.v7.widget.Toolbar
android:id="@+id/expanded_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin" />
</android.support.design.widget.CollapsingToolbarLayout>
<!--</android.support.design.widget.AppBarLayout>-->
</merge>
作为旁注,我从 NestedScrollView 中删除了与锚相关的标签和<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.CoordinatorLayout
android:id="@+id/coordinator_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<com.example.customviewtoolbar.ExpandedToolbar
android:id="@+id/expandable_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:toolbarNavColor="?attr/NavigationIconColor"
app:toolbarNavIcon="?attr/NavigationUpArrow"
app:toolbarTitle="My Finances"
app:toolbarTitleColor="?attr/NavigationTitleColor" />
<android.support.v4.widget.NestedScrollView
android:id="@+id/nested_scrollview"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<android.support.constraint.ConstraintLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.AppCompatTextView
android:id="@+id/title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:paddingStart="32dp"
android:paddingEnd="0dp"
android:text="@string/finances"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<android.support.v7.widget.AppCompatEditText
android:id="@+id/edit_text"
android:layout_width="0dp"
android:layout_height="56dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/title" />
<android.support.v7.widget.AppCompatTextView
android:id="@+id/details"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="DETAILS TODO"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/edit_text" />
<android.support.v7.widget.RecyclerView
android:id="@+id/finances_list"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layoutManager="android.support.v7.widget.LinearLayoutManager"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/details" />
<android.support.v7.widget.AppCompatButton
android:id="@+id/button_see_all"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="See All"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@id/finances_list"
app:layout_constraintVertical_bias="1.0" />
</android.support.constraint.ConstraintLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
</RelativeLayout>
,因为它们并不是真正需要的标签,并且阻止了布局检查器的工作。
您总是可以不使用自定义视图,但是为了方便起见,我认为您想要它。
这是我用于演示目的的android:fillViewport="true"
的模型。
ExpandedToolbar.java
ExpandedToolbar
答案 2 :(得分:0)
尝试仅android:fitsSystemWindows="true"
删除adjustResize
最终用途。应该工作