美文网首页
CoordinatorLayout高级用法-自定义Behavio

CoordinatorLayout高级用法-自定义Behavio

作者: 玖玖君 | 来源:发表于2019-08-01 16:50 被阅读0次

效果

效果

写动画类

public class FooterBehavior extends CoordinatorLayout.Behavior<View>{

    public FooterBehavior(Context context, AttributeSet attributeSet){
        super(context,attributeSet);
    }

    @Override
    public boolean layoutDependsOn(CoordinatorLayout parent, View child, View dependency) {
        return dependency instanceof AppBarLayout;
    }

    @Override
    public boolean onDependentViewChanged(CoordinatorLayout parent, View child, View dependency) {
        float scaleY = Math.abs(dependency.getY()) / dependency.getHeight();
        child.setTranslationY(child.getHeight() * scaleY);
        return true;
    }
}


写主布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <include layout="@layout/appbar_main"/>

    <include layout="@layout/content_main" />

    <include layout="@layout/footer_main"/>

</android.support.design.widget.CoordinatorLayout>


写appbar_main布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.AppBarLayout
    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:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        android:background="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|enterAlways"/>

</android.support.design.widget.AppBarLayout>

写content_main

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView
    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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <Button
        android:id="@+id/btn_nh"
        android:layout_width="match_parent"
        android:layout_height="@dimen/dp_200"
        android:text="你好"
        />
    <TextView
        android:layout_width="match_parent"
        android:layout_below="@+id/btn_nh"
        android:layout_height="wrap_content"
        android:text="你好"
        />

</RelativeLayout>
</android.support.v4.widget.NestedScrollView>

footer_main.xml布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="60dp"
    android:layout_gravity="bottom"
    android:background="?attr/colorPrimary"
    app:layout_behavior="com.example.dp_shipei.util.FooterBehavior">
    <android.support.design.widget.BottomNavigationView
        android:id="@+id/bnv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        app:itemBackground="@android:color/white"
        app:labelVisibilityMode="labeled"
        app:itemTextColor="@drawable/select_textcolor"
        app:itemIconTint="@drawable/select_textcolor"
        app:menu="@menu/nav_menu"/>
</LinearLayout>

相关文章

网友评论

      本文标题:CoordinatorLayout高级用法-自定义Behavio

      本文链接:https://www.haomeiwen.com/subject/ufdcdctx.html