【问题标题】:ListFragment FloatingActionButton is at wrong positionListFragment FloatingActionButton 位置错误
【发布时间】:2017-02-11 07:51:44
【问题描述】:

在我的应用程序中,ListFragment 中有一个 FloatingActionButton

这是ListFragment的布局:

<android.support.design.widget.CoordinatorLayout
    android:id="@+id/main_content"
    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">

    <include layout="@android:layout/list_content"/>

    <ListView
        android:id="@id/android:list"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <TextView
        android:id="@id/android:empty"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/no_contacts"/>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="16dp"
        android:src="@drawable/ic_add"
        app:layout_anchor="@id/android:list"
        app:layout_anchorGravity="bottom|end"
        android:layout_alignParentEnd="true"/>
</android.support.design.widget.CoordinatorLayout>

在 Android Studio 中,FAB 位于 bottom-right 位置,但当我运行应用程序时,它位于 top-left

【问题讨论】:

    标签: android user-interface material-design floating-action-button


    【解决方案1】:

    试试这个

    <android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="16dp"
    android:src="@drawable/ic_add"
    app:layout_anchor="@id/android:list"
    app:layout_anchorGravity="bottom|end"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    />
    

    【讨论】:

      【解决方案2】:

      在您使用包含标签添加到此布局的布局中添加列表视图和文本视图。

      【讨论】:

        【解决方案3】:

        这是我的工作布局。

        <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/main_content"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
        
            <include layout="@android:layout/list_content" />
        
            <ListView
                android:id="@id/android:list"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
        
            <TextView
                android:id="@id/android:empty"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="no contacts" />
        
            <android.support.design.widget.FloatingActionButton
                android:id="@+id/fab"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom|end"
                android:layout_margin="16dp"
                android:src="@android:drawable/ic_input_add"
                app:layout_anchor="@id/android:list"
                app:layout_anchorGravity="bottom|end" />
        </android.support.design.widget.CoordinatorLayout>
        

        并且很好地显示了设备中的布局。

        【讨论】:

        • 在我的应用程序中,ListFragment 位于 ViewPager(选项卡)中,这就是我得到的:link
        • 如果发现fab必须放在主要活动布局中,请多搜索一点(根据材料设计指南here以及如何做here
        猜你喜欢
        • 1970-01-01
        • 2013-01-20
        • 1970-01-01
        • 1970-01-01
        • 2015-11-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-31
        相关资源
        最近更新 更多