我开始学习android,偶然发现了一个我现在无法解决的问题。我正在尝试使按钮的行为类似于默认的“浮动动作按钮”(以便它在与他一起上下移动时,“粘合”到小吃栏上)。我知道它是通过行为来完成的,但是直到小吃栏出现时,我才意识到按钮会弹出到空间中:D我将非常感激它的完成方向或示例。预先感谢!
这是我的xml和行为类
XML:
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/EntryScreenCoordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorWhite"
android:orientation="vertical">
<Button
android:id="@+id/EntryScreenAccelerometerButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:text="Accelerometer screen"
app:layout_behavior="com.example.robert.testapp.ButtonSnackbarBehaviour" />
<Button
android:id="@+id/EntryScreenButtonToImages"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|start"
android:text="Make Fragment"
app:layout_behavior="com.example.robert.testapp.ButtonSnackbarBehaviour" />
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/EntryScreenButtonToJSONLoader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Load JSON"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toRightOf="@id/EntryScreenButtonToNoXmlFragment"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/EntryScreenButtonToNoXmlFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/entry_screen_to_no_xml_button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@id/EntryScreenButtonToJSONLoader"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/EntryScreenButtonInternetCheck"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/entry_screen_internet_check_button"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
行为:
class ButtonSnackbarBehaviour : CoordinatorLayout.Behavior<Button> {
constructor() : super()
constructor( context:Context, attrs:AttributeSet) : super(context, attrs)
override fun layoutDependsOn(parent: CoordinatorLayout, child: Button, dependency: View): Boolean {
return dependency is Snackbar.SnackbarLayout
}
override fun onDependentViewChanged(parent: CoordinatorLayout, child: Button, dependency: View): Boolean {
child.y += getFabTranslationYForSnackbar(parent, child)
return false
}
private fun getFabTranslationYForSnackbar(parent: CoordinatorLayout,
button: Button): Float {
var minOffset = 0f
val dependencies = parent.getDependencies(button)
var i = 0
val z = dependencies.size
while (i < z) {
val view = dependencies[i]
if (view is Snackbar.SnackbarLayout && parent.doViewsOverlap(button, view)) {
minOffset = Math.min(minOffset,
ViewCompat.getTranslationY(view) - view.getHeight())
}
i++
}
return minOffset
}
}