CoordinatorLayout、FAB和容器布局冲突
CoordinatorLayout, FAB and container layout conflict
我有一个主 activity 和一个带 container layout
的抽屉,我将每个片段替换为 FragmentManager
。
我想将 FAB 添加到 hide/show 滚动的我的一个子片段中,但我不确定我做错了什么并得到:
布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.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.support.design.widget.AppBarLayout
android:id="@+id/appBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
android:minHeight="?actionBarSize"
android:theme="@style/Toolbar"
app:layout_scrollFlags="scroll|enterAlways" />
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<android.support.design.widget.FloatingActionButton
style="@style/Widget.Design.FloatingActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right|end"
android:layout_marginBottom="@dimen/spacing_medium"
android:layout_marginRight="@dimen/spacing_medium"
android:elevation="@dimen/elevation_little"
app:fabSize="normal"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
app:srcCompat="@drawable/ic_add_white_18dp" />
</android.support.design.widget.CoordinatorLayout>
将 app:layout_anchor="@id/container"
添加到您的 FAB。此外,通过指示 bottom|right
,无需包含 end
尝试下面的代码
public class FABBehaviour extends FloatingActionButton.Behavior {
public FABBehaviour(Context context, AttributeSet attrs) {
super();
}
@Override
public void onNestedScroll(CoordinatorLayout coordinatorLayout, FloatingActionButton child, View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) {
super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed);
//child -> Floating Action Button
if (dyConsumed > 0) {
CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) child.getLayoutParams();
int fab_bottomMargin = layoutParams.bottomMargin;
child.animate().translationY(child.getHeight() + fab_bottomMargin).setInterpolator(new LinearInterpolator()).start();
} else if (dyConsumed < 0) {
child.animate().translationY(0).setInterpolator(new LinearInterpolator()).start();
}
}
@Override
public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, FloatingActionButton child, View directTargetChild, View target, int nestedScrollAxes) {
return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL;
} }
在Xml
app:layout_behavior="Your PpackageName.FABBehaviour"
看起来 CoordinatorLayout
无法处理不属于 "direct children".
[=25 的子视图的行为=]
您需要在 fab 视图中添加 app:layout_anchorGravity="bottom|right|end"
和 android:layout_gravity="end|bottom"
。
例如:
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab_feeds"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:clickable="true"
android:layout_gravity="end|bottom"
android:src="@drawable/ic_plus_white"
app:layout_anchorGravity="bottom|right|end"/>
我有一个主 activity 和一个带 container layout
的抽屉,我将每个片段替换为 FragmentManager
。
我想将 FAB 添加到 hide/show 滚动的我的一个子片段中,但我不确定我做错了什么并得到:
布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.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.support.design.widget.AppBarLayout
android:id="@+id/appBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
android:minHeight="?actionBarSize"
android:theme="@style/Toolbar"
app:layout_scrollFlags="scroll|enterAlways" />
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<android.support.design.widget.FloatingActionButton
style="@style/Widget.Design.FloatingActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right|end"
android:layout_marginBottom="@dimen/spacing_medium"
android:layout_marginRight="@dimen/spacing_medium"
android:elevation="@dimen/elevation_little"
app:fabSize="normal"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
app:srcCompat="@drawable/ic_add_white_18dp" />
</android.support.design.widget.CoordinatorLayout>
将 app:layout_anchor="@id/container"
添加到您的 FAB。此外,通过指示 bottom|right
,无需包含 end
尝试下面的代码
public class FABBehaviour extends FloatingActionButton.Behavior {
public FABBehaviour(Context context, AttributeSet attrs) {
super();
}
@Override
public void onNestedScroll(CoordinatorLayout coordinatorLayout, FloatingActionButton child, View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) {
super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed);
//child -> Floating Action Button
if (dyConsumed > 0) {
CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) child.getLayoutParams();
int fab_bottomMargin = layoutParams.bottomMargin;
child.animate().translationY(child.getHeight() + fab_bottomMargin).setInterpolator(new LinearInterpolator()).start();
} else if (dyConsumed < 0) {
child.animate().translationY(0).setInterpolator(new LinearInterpolator()).start();
}
}
@Override
public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, FloatingActionButton child, View directTargetChild, View target, int nestedScrollAxes) {
return nestedScrollAxes == ViewCompat.SCROLL_AXIS_VERTICAL;
} }
在Xml
app:layout_behavior="Your PpackageName.FABBehaviour"
看起来
[=25 的子视图的行为=]CoordinatorLayout
无法处理不属于 "direct children".您需要在 fab 视图中添加
app:layout_anchorGravity="bottom|right|end"
和android:layout_gravity="end|bottom"
。
例如:
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab_feeds"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:clickable="true"
android:layout_gravity="end|bottom"
android:src="@drawable/ic_plus_white"
app:layout_anchorGravity="bottom|right|end"/>