2016-06-25 81 views
2

我有一个选项卡式布局,我想在底部有一个粘滞按钮。 当布局大于屏幕尺寸时,我可以向上滚动,并且工具栏也会向上滚动以将选项卡对齐为最顶层元素。当我滚动时,按钮也会向上滚动,因为底部有一个填充区域,以便在启动时保持其状态。Android底部粘贴按钮在滚动上向下移动

Before ScrollAfter Scroll (Button moves up)

activity_main.xml中

<?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="io.sampleapp.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:theme="@style/AppTheme.AppBarOverlay"> 

     <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"> 

     </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.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> 

fragment_main.xml

<RelativeLayout 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="io.sampleapp.MainActivity$PlaceholderFragment"> 

    <android.support.v7.widget.RecyclerView 
     android:id="@+id/mRecyclerview" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"/> 

    <Button 
     android:id="@+id/bottomButton" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_marginBottom="54dp" 
     android:background="#555555" 
     android:text="dummy" 
     android:textColor="#ffffff" /> 

</RelativeLayout> 
+0

那么,什么是预期?第一个截图还是第二个? – Rajkiran

+0

@Rajkiran当我滚动按钮不应该向上移动。它应该在底部 –

+0

为什么你在底部填充54dp?为了补偿导航栏? – Rajkiran

回答

1

我觉得按钮外应CoordinatorLayout标签,试图从fragment_main.xml删除的按钮,并把它添加到activity_main.xml,如:在其他片段

<android.support.v4.view.ViewPager 

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

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

<Button 
    android:id="@+id/bottomButton" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_marginBottom="54dp" 
    android:background="#555555" 
    android:text="dummy" 
    android:textColor="#ffffff" /> 

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

,如果你不需要按键就可以使它看不见。

+0

我没有测试过,如果有任何问题,请让我现在 –

1

把你的按钮RecyclerView之外。

+0

它在它自己之外 –

+0

对不起,我看不到它适当。它已经在外面了。 –

1

我可以说可能是你正在试图达到一个用例,而没有遵循适当的材料设计指导方针。

为什么不使用FAB又名FloatingActionButton

1

我建议,除去保证金底部按钮,添加保证金底部高度相同的recyclerview作为按钮的高度