C. *_*odd 9 android android-layout layout-gravity floating-action-button layout-anchor
这里有点新手.我进入Android开发大约两个月,但我在其他环境中有多年的开发经验.
好的.我有一个FloatingActionButton没有出现在我预期或想要的地方.这里面CoordinatorLayout,有一个沿AppBarLayout/ Toolbar和承接ListView.
这是布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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:id="@+id/fragment_coordinator"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".ViewVehicleList">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
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"
android:background="?attr/colorPrimary"
android:title="Vehicle List"
app:layout_scrollFlags="scroll|enterAlways|snap"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
</android.support.design.widget.AppBarLayout>
<ListView
android:id="@+id/Vehicle_ListView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="#FFFFFF"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
</ListView>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab_AddVehicle"
style="@style/FloatingAddButton"
android:src="@drawable/ic_green_add"
android:layout_gravity="bottom|end"
app:layout_anchor="@id/Vehicle_ListView"
android:onClick="addVehicle"/>
</android.support.design.widget.CoordinatorLayout>
Run Code Online (Sandbox Code Playgroud)
使用此布局,屏幕如下所示:

我layout_gravity说"bottom|end".我把它改成了"bottom|right",但我仍然得到了相同的结果.我已经阅读了很多教程,并通过Stack Overflow进行了研究,并且没有运气.
我设法通过删除FAB元素中列出的锚来解决它app:layout_anchor="@id/Vehicle_ListView",这似乎与我读过的内容相反:使用FAB并正确定位它需要使用layout_anchor和layout_gravity.没有锚标记,它看起来像这样:

所以这是我的问题:为什么我的锚固定了我的定位FloatingActionButton?我究竟做错了什么?
Hus*_*eky 16
你只需要添加layout_anchorGravity.
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab_AddVehicle"
style="@style/FloatingAddButton"
android:src="@drawable/ic_green_add"
android:onClick="addVehicle"
app:layout_anchor="@id/Vehicle_ListView"
app:layout_anchorGravity="bottom|end" />
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
22956 次 |
| 最近记录: |