Android support,
Android support,
In Android development, CoordinatorLayout is a very powerful layout container to achieve complex user interface interaction effects.It is part of Android Support Library, providing developers with many useful attributes and functions to achieve coordinated layout and interactive behavior.
This article will introduce the common attributes of CoordinatorLayout and provide the corresponding Java code example.
1. layout_anchor
Layout_anchor attribute specifies the location of the position of a control.By setting this property, we can associate a certain control with the specified anchor point to achieve the coordination of the layout.
<androidx.coordinatorlayout.widget.CoordinatorLayout
...
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
...
app:layout_anchor="@id/anchor"
app:layout_anchorGravity="top|left"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
...
android:id="@+id/anchor"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
In the above example, the first button (with the layout_anchor attribute) will be associated with the specified anchor point (second button), and the position will be determined according to the Layout_anchorgravity property.
2. layout_behavior
Layout_behavior attributes are used to specify the interaction behavior of a control in CoordinatorLayout.We can customize a Behavior class and associate it with the corresponding control to achieve custom layout logic and interactive effects.
public class CustomBehavior extends CoordinatorLayout.Behavior<View> {
@Override
public boolean layoutDependsOn(@NonNull CoordinatorLayout parent, @NonNull View child, @NonNull View dependency) {
// Judging whether Child depends on Dependency according to the corresponding conditions
return dependency instanceof RecyclerView;
}
@Override
public boolean onDependentViewChanged(@NonNull CoordinatorLayout parent, @NonNull View child, @NonNull View dependency) {
// Update the location or size of Child according to the changes of Dependency
child.setX(dependency.getX());
child.setY(dependency.getY() + dependency.getHeight());
return true;
}
}
<androidx.coordinatorlayout.widget.CoordinatorLayout
...
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
...
app:layout_behavior=".CustomBehavior"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<android.support.v7.widget.RecyclerView
...
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
In the above example, we created a Custombehavior class and associated it with a TextView control.By rewriting the layoutDependson () method and the onDependentViewChanged () method, we can define the TextView control dependence on RecyclerView, and update the location or size of the textView according to the changes of RecyclerView.
3. layout_keyline
Layout_Keyline attribute allows us to define a key line in CoordinatorLayout and position it through other controls.This can be used to achieve complex page layout logic.
<androidx.coordinatorlayout.widget.CoordinatorLayout
...
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_keyline="100dp">
<Button
...
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_anchor="@id/keyline"
app:layout_anchorGravity="center_horizontal" />
<Button
...
android:id="@+id/keyline"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
In the above example, the first button is associated with the specified anchor point (second button) through the layout_anchor property, and determines its position through the layout_anchorgravity property.At the same time, the layout_keyline attribute specifies a key cable, that is, the position of 100dp from the left side.
Summarize
By using the attributes provided by CoordinatorLayout, we can achieve complex layout and interactive effects.Layout_anchor attributes are used to control the relationship between layout. Layout_behavior attributes are used to customize layout logic and interactive behaviors. Layout_keyline attributes are used to define key cables in the layout.
I hope the content described in this article will be helpful when you use CoordinatorLayout and can provide some inspiration.If these attributes are still doubtful, it is recommended to refer to the official documentation or further study related information.I wish you success in Android development!