gpt4 book ai didi

android - 启动时 fragment 中的 fab 位置不正确

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:10:47 27 4
gpt4 key购买 nike

我有一个 fragment Activity 。 fragment 有一个协调器布局,可以作为它和其他 View 的直接子级。

Activity 布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<include
android:id="@+id/toolbar"
layout="@layout/toolbar_layout" />

<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</LinearLayout>

fragment 布局

<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/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<com.github.aakira.expandablelayout.ExpandableLinearLayout
android:id="@+id/statistic_expand_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/toolbar"
android:background="@color/colorPrimary"
android:orientation="vertical"
app:ael_duration="150"
app:ael_interpolator="accelerateDecelerate">

<com.github.mikephil.charting.charts.BarChart
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/overall_statistic_bar_chart"
android:layout_width="match_parent"
android:layout_height="@dimen/statistic_small"/>

<TextView
style="@style/SmallTextTheme"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/spacing_small"
android:gravity="center"
android:text="@string/overall_statistic"
android:textColor="@color/colorTextPrimary"/>

</com.github.aakira.expandablelayout.ExpandableLinearLayout>

<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/statistic_expand_layout"
android:scrollbars="vertical"/>

<View
android:layout_width="match_parent"
android:layout_height="@dimen/spacing_tiny"
android:layout_below="@id/statistic_expand_layout"
android:background="@drawable/shadow_updown"/>

</RelativeLayout>

<android.support.design.widget.FloatingActionButton
android:id="@+id/activity_main_fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/spacing_standard"
android:src="@drawable/ic_add"
app:layout_anchor="@id/recycler_view"
app:layout_anchorGravity="bottom|end"/>

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

当我用这个 fragment 开始 Activity 时 fab 位置不正确

fab

但是当我按下任何按钮或查看 fab 时,它会转到右下角的正确位置。当我将此布局直接放入 Activity 中时,fab 位置始终是正确的。谁能帮我找出问题所在?

最佳答案

but when I press any button or view fab goes to its correct position in the right bottom corner. When I put this layout directly into activity fab position is always correct.

解决方案:这是 AppBarLayoutCoordinatorLayout 的文档,其中说明了它们的外观:

    <android.support.design.widget.CoordinatorLayout
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">

<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">

<!-- Your scrolling content -->

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

<android.support.design.widget.AppBarLayout
android:layout_height="wrap_content"
android:layout_width="match_parent">

<android.support.v7.widget.Toolbar
...
app:layout_scrollFlags="scroll|enterAlways"/>

<android.support.design.widget.TabLayout
...
app:layout_scrollFlags="scroll|enterAlways"/>

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

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

所以现在,您的设计中不应该存在 RelativeLayout。将CoordinatorLayout放在Activity的主布局中,然后尝试使用AppBarLayoutToolbarCoordinatorLayout 一起使用时 因为现在 CoordinatorLayoutRelativeLayout 想象成 AppBarLayout .

就是这样。还有一件事,不要忘记将:app:layout_behavior="@string/appbar_scrolling_view_behavior" 添加到 AppBarLayout 下的 View 中。

如果您需要任何帮助或提出问题,请告诉我。

关于android - 启动时 fragment 中的 fab 位置不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41380173/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com