Android 浮动操作按钮未在片段内完全显示

Android 浮动操作按钮未在片段内完全显示,android,android-fragments,floating-action-button,androiddesignsupport,android-coordinatorlayout,Android,Android Fragments,Floating Action Button,Androiddesignsupport,Android Coordinatorlayout,我正在片段中使用FAB按钮和RecyclerView。此片段是TabViewPager的一个实例。我对FAB按钮有问题。我已经将RecyclerView和fab按钮放置在框架布局中,fab button位于右下角。现在,我面临的问题是,晶圆厂按钮不完全可见。它的一半部分是隐藏的,如下面的屏幕截图所示。谁能帮我解决这个问题。提前谢谢 注意:一旦卷动晶圆厂,晶圆厂将正确对齐。只有在理想情况下(滚动完成之前),才会出现问题 fragment.xml <?xml version="1.0" en

我正在片段中使用FAB按钮和RecyclerView。此片段是TabViewPager的一个实例。我对FAB按钮有问题。我已经将RecyclerView和fab按钮放置在框架布局中,fab button位于右下角。现在,我面临的问题是,晶圆厂按钮不完全可见。它的一半部分是隐藏的,如下面的屏幕截图所示。谁能帮我解决这个问题。提前谢谢

注意:一旦卷动晶圆厂,晶圆厂将正确对齐。只有在理想情况下(滚动完成之前),才会出现问题

fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end|bottom"
        android:layout_margin="10dp"
        app:backgroundTint="@color/red"
        android:src="@drawable/ic_done"/>
</FrameLayout>
<?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"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBarLayout"
        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"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:layout_scrollFlags="scroll|enterAlways" />


        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

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

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

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

tabviewpagerlayout.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end|bottom"
        android:layout_margin="10dp"
        app:backgroundTint="@color/red"
        android:src="@drawable/ic_done"/>
</FrameLayout>
<?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"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBarLayout"
        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"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
            app:layout_scrollFlags="scroll|enterAlways" />


        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

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

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

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

您应该将晶圆厂移到
坐标布局内
。 大概是这样的:

<android.support.design.widget.CoordinatorLayout>

    <android.support.design.widget.AppBarLayout>

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

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

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

    <android.support.v4.view.ViewPager
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_gravity="end|bottom"/>

</android.support.design.widget.CoordinatorLayout>
其中,RecycleServicewFragment布局为:

 <android.support.v7.widget.RecyclerView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

我有点像加布里埃尔建议的那样,将晶圆厂转移到包含晶圆的活动中。此外,您还需要在以下位置更新晶圆厂的颜色/clickListener:

public class MainActivity extends AppCompatActivity implements TabLayout.OnTabSelectedListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        ...

        setFloatingActionButtonForImagesTab();

    }

    ...

    @Override
    public void onTabSelected(final TabLayout.Tab tab) {
    switch (tab.getPosition()) {
        case 0:
            setFloatingActionButtonForImagesTab();
            break;
        case 1:
            setFloatingActionButtonForMusicTab();
            break;
        case 2:
            setFloatingActionButtonForVideoTab();
            break;
        }
    }

    ...

}
然后在设置功能中,只需设置颜色并单击listener:

private void setFloatingActionButtonForImagesTab() {
    myViewPager.setCurrentItem(0);
    floatingActionButton.setBackgroundTintList(getResources().getColorStateList(R.color.purple));
    floatingActionButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(coordinatorLayout, "images", Snackbar.LENGTH_LONG)
                .show();
        }
    });
}

记下上面对
setCurrentItem
的调用。现在您需要实现
TabLayout.OnTabSelectedListener

无论选择什么选项卡,都必须显示/隐藏晶圆厂,这不是一个可接受的解决方案。我尝试过各种布局组合,但将晶圆厂移至活动布局是唯一有效的解决方案。但是如果你只需要一个标签上的按钮怎么办?这是目前唯一可行的方法,但我期待着设计库的更新,因为这个版本有太多的bug。无论如何,最重要的是,FAB必须是CoordinatorLayout的直接后代,因此它不会被正在折叠的工具栏压下…

我刚刚遇到了同样的问题。不幸的是,我没有答案给你,但一些附加点

不是按钮被按下了,而是整个碎片被按下了。到目前为止,我还没有对它进行测试,但我想,如果有一种方法可以让您看到片段页面的大小,那么您将看到它不会像应该的那样在屏幕底部结束

对于可能的解决方案,我考虑在底部添加一个大小为“?attr/actionBarSize”的填充

我将对此进行测试,并在完成后发回

更新: 当我使用80 dp的边界时,得到a(我没有10个声誉来发布屏幕截图,wth)

解决方法:

<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/rootLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

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

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

        <FrameLayout
            android:id="@+id/fragment_root"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/border"
            android:layout_marginTop="?attr/actionBarSize"
            >
            <!--android:paddingBottom="?attr/actionBarSize"-->
            <!--app:layout_behavior="@string/appbar_scrolling_view_behavior"-->
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/border"></LinearLayout>
            <fragment
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:name="com.derek.hianthy.mydiary.ui.BaseFragment"
                tools:layout="@layout/layout"
                android:background="@drawable/border"
                />

        </FrameLayout>
        <android.support.design.widget.FloatingActionButton
            android:id="@+id/fabBtn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|right"
            android:layout_marginBottom="@dimen/fab_margin_bottom"
            android:layout_marginRight="@dimen/fab_margin_right"
            android:src="@drawable/ic_action_add"
            app:borderWidth="0dp"
            app:fabSize="normal"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="false"
            android:layout_alignParentLeft="false" />

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

  • 我也有同样的问题,浮动操作按钮是完全不可见的,它在屏幕底部是不可见的。当我向下滚动时,它会出现
  • 此外,向下滚动时选项卡被隐藏,但我希望它们始终可见,因此我通过删除
    app:layout\u scrollFlags=“scroll | enterally”
    对其进行了修复。JDev在
    这解决了FloatingActionButton的问题,它现在是可见的

  • 可能有点晚了,但对我来说,最好的选择似乎是创建另一个片段,其中包含带有内容和浮动按钮的片段。此代码在我的应用程序中运行良好:

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="domain.ItemsFragment">
    
    
        <fragment
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:name="com.lucyapp.client.ItemFragment"
            android:id="@+id/fragment"
            tools:layout="@layout/fragment_item_list" />
    
        <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="@dimen/fab_margin"
            android:src="@android:drawable/ic_dialog_email" />
    
    </FrameLayout>
    

    在屏幕下方和滚动显示的“查看寻呼机第一片段”按钮上,我也面临同样的问题。 但经过一些调查,我发现我已经写了

    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    
    在ViewPager中协调布局。因此,在移除此卷轴后,工厂位置问题得到解决

    请检查我的实现是否正常工作:

    <?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"
        android:id="@+id/main_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <android.support.design.widget.AppBarLayout
            android:id="@+id/appbar"
            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"
                android:background="?attr/colorPrimary"
                app:layout_scrollFlags="scroll|enterAlways"
                app:popupTheme="@style/AppTheme.PopupOverlay">
    
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">
    
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:gravity="center_horizontal"
                        android:text="@string/app_name"
                        android:textColor="@color/textPrimary"
                        android:textSize="18sp" />
    
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:gravity="center_horizontal"
                        android:text="@string/str_toolbar_subtittle"
                        android:textColor="@color/textPrimary"
                        android:textSize="14sp" />
                </LinearLayout>
            </android.support.v7.widget.Toolbar>
    
            <android.support.design.widget.TabLayout
                android:id="@+id/tabs"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom"
                android:background="?attr/colorPrimary"
                app:tabGravity="fill"
                app:tabIndicatorColor="@color/colorAccent"
                app:tabSelectedTextColor="@color/errorColor"
                app:tabTextColor="@color/white" />
    
        </android.support.design.widget.AppBarLayout>
    
        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/app_bar" />
    
    </android.support.design.widget.CoordinatorLayout>
    

    这个解决方案适合我。使用以下代码创建名为CustomBehavior的新类:

    public class CustomBehavior extends CoordinatorLayout.Behavior<ViewPager> {
    
    private int mActionBarHeight;
    
    public CustomBehavior(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        init(context);
    }
    
    public CustomBehavior(Context context) {
        init(context);
    }
    
    private void init(Context context) {
        TypedValue tv = new TypedValue();
        if (context.getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true)) {
            mActionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data, context.getResources().getDisplayMetrics());
        }
    }
    
    @Override
    public boolean layoutDependsOn(CoordinatorLayout parent, ViewPager child, View dependency) {
        return dependency instanceof AppBarLayout;
    }
    
    public boolean onDependentViewChanged(CoordinatorLayout parent, ViewPager child, View dependency) {
        offsetChildAsNeeded(parent, child, dependency);
        return false;
    }
    
    private void offsetChildAsNeeded(CoordinatorLayout parent, View child, View dependency) {
        if (child instanceof ViewPager) {
            ViewPager viewPager = (ViewPager) child;
            View list = viewPager.findViewById(R.id.recycler_view);
            if (list != null) {
                final CoordinatorLayout.Behavior behavior =
                        ((CoordinatorLayout.LayoutParams) dependency.getLayoutParams()).getBehavior();
                if (behavior instanceof AppBarLayout.Behavior) {
                    final AppBarLayout.Behavior ablBehavior = (AppBarLayout.Behavior) behavior;
                    AppBarLayout appBarLayout = (AppBarLayout) dependency;
                    ViewCompat.setTranslationY(list, ablBehavior.getTopAndBottomOffset()
                            + appBarLayout.getTotalScrollRange()
                            + mActionBarHeight);
                }
            }
        }
    }
    
    公共类CustomBehavior扩展了CoordinatorLayout.Behavior{
    私家车高度;
    公共自定义行为(上下文上下文,属性集属性集){
    super(上下文、属性集);
    init(上下文);
    }
    公共自定义行为(上下文){
    init(上下文);
    }
    私有void init(上下文){
    TypedValue tv=新的TypedValue();
    if(context.getTheme().resolveAttribute(android.R.attr.actionBarSize,tv,true)){
    mActionBarHeight=TypedValue.complexToDimensionPixelSize(tv.data,context.getResources().getDisplayMetrics());
    }
    }
    @凌驾
    public boolean layoutDependsOn(协调布局父项、ViewPager子项、视图依赖项){
    返回AppBarLayout的依赖实例;
    }
    公共布尔onDependentViewChanged(协调布局父项、ViewPager子项、视图依赖项){
    Offsetchildasneed(父项、子项、依赖项);
    返回false;
    }
    私有void offsetchildasneed(协调布局父项、视图子项、视图依赖项){
    if(ViewPager的子实例){
    ViewPager ViewPager=(ViewPager)子级;
    视图列表=viewPager.findViewById(R.id.recycler\u视图);
    如果(列表!=null){
    最终协调人布局。行为=
    ((CoordinatorLayout.LayoutParams)dependency.getLayoutParams()).getBehavior();
    if(AppBarLayout.behavior的行为实例){
    最终的AppBarLayout.Behavior ablBehavior=(AppBarLayout.Behavior)行为;
    AppBarLayout AppBarLayout=(AppBarLayout)依赖项;
    ViewCompat.setTranslationY(列表,ablBehavior.getTopAndBottomOffset()
    +appBarLayout.getTotalScrollRange()
    +高度);
    }
    }
    }
    }
    
    }

    现在,将自定义行为类分配给xml文件中的viewpager小部件

    <android.support.v4.view.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="yourpackage.CustomBehavior"
        />
    

    我遇到了同样的问题,要求在片段中显示FAB。我所做的只是从工具栏中删除了该行

    app:layout_scrollFlags="scroll|enterAlways"
    
    你可以查看我的布局

    <?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/main_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        tools:context=".MainActivity">
    
        <android.support.design.widget.AppBarLayout
            android:id="@+id/appbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingTop="@dimen/appbar_padding_top"
            android:background="@color/white"
            app:elevation="0dp"
            android:elevation="0dp"
            >
    
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar_main"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:layout_weight="1"
                android:background="@color/white"
                app:title="@string/app_name">
    
            </android.support.v7.widget.Toolbar>
    
            <android.support.design.widget.TabLayout
                android:id="@+id/tabs_main"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                >
    
    
            </android.support.design.widget.TabLayout>
    
        </android.support.design.widget.AppBarLayout>
    
        <android.support.v4.view.ViewPager
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />
    
    </android.support.design.widget.CoordinatorLayout>