我有一个像这样的协调员布局:
<android.support.design.widget.CoordinatorLayout
android:id="@+id/coordinator_subreddit_selection"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.github.clans.fab.FloatingActionMenu
android:id="@+id/addFabMenu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"/>
</android.support.design.widget.CoordinatorLayout>
在我的代码中,我有一个像这样的小吃吧:
Snackbar.make(getView().findViewById(R.id.addFabMenu),
R.string.no_entered_subreddit_name, Snackbar.LENGTH_SHORT).show();
这是我得到的:
这就是我的意思:
我已经尝试完全复制示例活动中的代码,但它仍然不像示例活动那样。
答案 0 :(得分:1)
通过创建协调器行为来修复它:
public class MoveUpwardBehavior extends CoordinatorLayout.Behavior<View> {
private static final boolean SNACKBAR_BEHAVIOR_ENABLED;
@Override
public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) {
return SNACKBAR_BEHAVIOR_ENABLED && dependency instanceof Snackbar.SnackbarLayout;
}
@Override
public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) {
float translationY = Math.min(0, dependency.getTranslationY() - dependency.getHeight());
child.setTranslationY(translationY);
return true;
}
static {
SNACKBAR_BEHAVIOR_ENABLED = Build.VERSION.SDK_INT >= 11;
}
}
并扩展了我的视图类,将行为应用于它
@CoordinatorLayout.DefaultBehavior(MoveUpwardBehavior.class)
public class MoveUpwardsFloatingMenu extends FloatingActionMenu {
public MoveUpwardsFloatingMenu(Context context) {
super(context);
}
public MoveUpwardsFloatingMenu(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MoveUpwardsFloatingMenu(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
}
答案 1 :(得分:0)
尝试使用支持库中的标准FloatingActionButton
而不是:
<com.github.clans.fab.FloatingActionMenu
android:id="@+id/addFabMenu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"/>
使用:
<android.support.design.widget.FloatingActionButton
android:id="@+id/addFabMenu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"/>
答案 2 :(得分:0)
CoordinatorLayout.DefaultBehavior批注已从API中弃用 级别27.1.0。
要将行为添加到自定义视图中,只需实现 CoordinatorLayout.AttachedBehavior 接口即可返回默认行为,或使用存在的 layout_behavior属性会覆盖附加行为。
在这里您可以看到一个示例:
class MoveUpwardBehavior : CoordinatorLayout.Behavior<View>() {
override fun layoutDependsOn(
parent: CoordinatorLayout,
child: View,
dependency: View): Boolean = dependency is SnackbarLayout
override fun onDependentViewChanged(
parent: CoordinatorLayout,
child: View,
dependency: View
): Boolean {
child.translationY = min(DEFAULT_CHILD_SIZE, dependency.translationY - dependency.height)
return true
}
override fun onDependentViewRemoved(parent: CoordinatorLayout, child: View, dependency: View) {
super.onDependentViewRemoved(parent, child, dependency)
child.translationY = DEFAULT_CHILD_SIZE
}
companion object {
private const val DEFAULT_CHILD_SIZE = 0f
}
}
class ExampleComponentView @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : ConstraintLayout(context, attrs, defStyleAttr), CoordinatorLayout.AttachedBehavior {
.....
override fun getBehavior(): CoordinatorLayout.Behavior<*> =
MoveUpwardBehavior()
}