Android 關於FloatActionButton位置異常的相關解決方法

近期在學習Android,先前在ListView界面中添加了FAB控件來保證界面美觀;後面又想可以使ListView有下拉刷新的功能,我準備使用谷歌自身的控件SwipeRefreshLayout來完成刷新功能。android

  • 修改以前的activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:id="@+id/lvwStudent"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        tools:ignore="MissingConstraints"
        tools:layout_editor_absoluteX="8dp"
        tools:layout_editor_absoluteY="8dp" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/floatingActionButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="4dp"
        android:layout_marginStart="8dp"
        android:clickable="true"
        android:focusable="true"
        app:fabSize="normal"
        app:layout_anchorGravity="end|center"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:srcCompat="@drawable/add" />

</android.support.constraint.ConstraintLayout>

 

 爲了實現下拉刷新的需求,我將android.support.constraint.ConstraintLayout更改成了android.support.v4.widget.SwipeRefreshLayout
更改後發現FAB不見了,百思不得其解。app

我想應該是和FAB的錨點設置有關係,但怎麼改也不成功。佈局

因而想到了一個取巧的方法:把ListView和刷新功能放到另外一個XML佈局文件中,再用include引用過來。學習

  • student_list.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/swip_main_layout"
    xmlns:tools="http://schemas.android.com/tools">

    <ListView
        android:id="@+id/lvwStudent"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        tools:ignore="MissingConstraints"
        tools:layout_editor_absoluteX="8dp"
        tools:layout_editor_absoluteY="8dp" />

</android.support.v4.widget.SwipeRefreshLayout> 
  • activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <include
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        layout="@layout/student_list"/>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/floatingActionButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="4dp"
        android:layout_marginStart="8dp"
        android:clickable="true"
        android:focusable="true"
        app:fabSize="normal"
        app:layout_anchorGravity="end|center"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:srcCompat="@drawable/add" />

</android.support.constraint.ConstraintLayout>

 

再在真機演示一下spa

 

 問題解決code

 /*************************************************************************************************/orm

在後續的學習過程當中發現,SwipeRefreshLayout能夠像這樣使用,也能達成效果xml

     <android.support.v4.widget.SwipeRefreshLayout
            android:id="@+id/swip_main_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <ListView
                android:id="@+id/lvwStudent"
                android:layout_width="match_parent"
                android:layout_height="0dp" />

     </android.support.v4.widget.SwipeRefreshLayout>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/floatingActionButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="4dp"
        android:layout_marginStart="8dp"
        android:clickable="true"
        android:focusable="true"
        app:fabSize="normal"
        app:layout_anchorGravity="end|center"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:srcCompat="@drawable/add" />
相關文章
相關標籤/搜索