2015-12-30 45 views
2

我的布局是一个主要活动,我用片段替换内容。我正在尝试添加一个FloatingActionButton,我希望按钮的一半位于工具栏中,而另一半我希望位于片段内容中。我附上我想要的结果的照片。 result工具栏与浮动操作按钮重叠

但我得到这个

enter image description here

工具栏重叠晶圆厂的一半。 我试图使透明的工具栏,但没有任何变化。

片段的代码是

<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:fitsSystemWindows="true"> 

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:app="http://schemas.android.com/apk/res-auto" 
     android:id="@+id/home_content" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <!-- contains archived and liked buttons --> 
     <LinearLayout 
      android:id="@+id/home_buttons" 
      android:layout_width="match_parent" 
      android:layout_height="40dp" 
      android:baselineAligned="false" 
      android:orientation="horizontal"> 

      <!-- archived button --> 
      <FrameLayout 
       android:id="@+id/home_archived_button" 
       android:layout_width="0.0dp" 
       android:layout_height="match_parent" 
       android:layout_marginEnd="2dp" 
       android:layout_marginRight="2dp" 
       android:layout_weight="1" 
       android:background="@color/orange"> 

       <!-- archived button icon --> 
       <ImageView 
        android:id="@+id/home_archived_button_icon" 
        android:layout_width="25dp" 
        android:layout_height="25dp" 
        android:layout_gravity="center_vertical|start" 
        android:layout_marginLeft="3dp" 
        android:layout_marginStart="3dp" 
        android:background="@mipmap/down_white_cyrcle_arrow" 
        android:contentDescription="@string/app_name" /> 

       <!-- archived button --> 
       <TextView 
        android:id="@+id/home_archived_button_text" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_gravity="center_vertical" 
        android:layout_marginLeft="40dp" 
        android:layout_marginStart="40dp" 
        android:text="@string/home_archived" 
        android:textColor="@color/white" /> 

      </FrameLayout> 

      <!--liked button --> 
      <FrameLayout 
       android:id="@+id/home_liked_button" 
       android:layout_width="0.0dp" 
       android:layout_height="match_parent" 
       android:layout_marginEnd="2dp" 
       android:layout_marginRight="2dp" 
       android:layout_weight="1" 
       android:background="@color/orange"> 

       <!-- liked button icon --> 
       <ImageView 
        android:id="@+id/home_liked_button_icon" 
        android:layout_width="25dp" 
        android:layout_height="25dp" 
        android:layout_gravity="center_vertical|end" 
        android:layout_marginEnd="3dp" 
        android:layout_marginRight="3dp" 
        android:background="@mipmap/down_white_cyrcle_arrow" 
        android:contentDescription="@string/app_name" /> 

       <!-- liked button text--> 
       <TextView 
        android:id="@+id/home_liked_button_text" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_gravity="center_vertical|end" 
        android:layout_marginEnd="40dp" 
        android:layout_marginRight="40dp" 
        android:text="@string/home_liked" 
        android:textColor="@color/white" /> 
      </FrameLayout> 
     </LinearLayout> 

     <!-- no matches --> 
     <RelativeLayout 
      android:id="@+id/home_no_matches_panel" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:gravity="center"> 

      <!-- no matches text --> 
      <TextView 
       android:id="@+id/home_no_matches" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_centerHorizontal="true" 
       android:layout_centerVertical="true" 
       android:gravity="center" 
       android:text="@string/home_no_matches" 
       android:textColor="@color/black" 
       android:visibility="gone" /> 

      <!-- home invite button --> 
      <Button 
       android:id="@+id/home_invite_button" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_below="@+id/home_no_matches" 
       android:layout_centerHorizontal="true" 
       android:background="@color/orange" 
       android:padding="10dp" 
       android:text="@string/invite_friends" 
       android:textAllCaps="false" 
       android:textColor="@color/white" 
       android:visibility="gone" /> 
     </RelativeLayout> 

     <!-- recyclerView for home fragment --> 
     <android.support.v7.widget.RecyclerView 
      android:id="@+id/home_recycler_view" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_below="@+id/home_buttons" 
      android:layout_marginBottom="35dp" /> 

     <!-- search button --> 
     <FrameLayout 
      android:id="@+id/home_search_button" 
      android:layout_width="match_parent" 
      android:layout_height="35dp" 
      android:layout_alignParentBottom="true" 
      android:background="@color/orange"> 

     </FrameLayout> 
    </RelativeLayout> 

    <!--home add property --> 
    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/fab" 
     style="@style/floating_action_button" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:clickable="true" 
     android:src="@mipmap/home_add_property" 
     app:backgroundTint="@color/white" 
     app:borderWidth="0dp" 
     app:elevation="0dp" 
     app:layout_anchor="@id/home_buttons" 
     app:layout_anchorGravity="top|center_horizontal" /> 

    <!-- home search button --> 
    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/home_search_fab" 
     style="@style/floating_action_button" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:clickable="false" 
     android:src="@mipmap/home_search" 
     app:backgroundTint="@color/orange" 
     app:borderWidth="0dp" 
     app:elevation="0dp" 
     app:layout_anchor="@id/home_search_button" 
     app:layout_anchorGravity="bottom|center_horizontal" /> 


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

和活动的代码是

<?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:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:context=".MainActivity"> 

    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     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" 
      android:gravity="center" 
      app:popupTheme="@style/AppTheme.PopupOverlay" 
      app:titleTextColor="@color/black"> 

      <!-- toolbar title --> 
      <TextView 
       android:id="@+id/toolbar_title" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center" 
       android:gravity="center" /> 

     </android.support.v7.widget.Toolbar> 

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

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

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

和晶圆厂

<style name="floating_action_button"> 
     <item name="android:layout_marginLeft">0dp</item> 
     <item name="android:layout_marginTop">-500dp</item> 
     <item name="android:layout_marginRight">0dp</item> 
     <item name="android:layout_marginBottom">-15dp</item> 
    </style> 
+1

后相关规范请,也不清楚你如何附上您的FAB –

回答

1

好作风,是不可能做到这一点以你的方式。您的FAB位于Fragment,而Toolbar位于Activity。我建议你做两件事情之一: 1)将FAB代码移动到Activity,并动态附加到它的动作(如果您需要与分段通信) 2)将Toolbar移动到Fragment。 不管怎么说,而不是试图内styles.xml对准你的工厂,你可以使用这样的锚标记(它是FAB在XML属性):

这是我如何做的:

片段布局:

<android.support.design.widget.AppBarLayout 
    android:id="@+id/appbar" 
    android:layout_width="match_parent" 
    android:layout_height="@dimen/detail_backdrop_height" 
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 
    android:fitsSystemWindows="true"> 

    <android.support.design.widget.CollapsingToolbarLayout 
     android:id="@+id/collapsing_toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:layout_scrollFlags="scroll|exitUntilCollapsed" 
     android:fitsSystemWindows="true" 
     app:contentScrim="?attr/colorPrimary" 
     app:expandedTitleMarginStart="48dp" 
     app:expandedTitleMarginEnd="64dp"> 

     <ImageView 
      android:id="@+id/backdrop" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:fitsSystemWindows="true" 
      android:scaleType="centerCrop" 
      app:layout_collapseMode="parallax" /> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/product_details_toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      app:layout_collapseMode="pin" 
      app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/> 

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

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

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

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

<android.support.design.widget.FloatingActionButton 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:clickable="true" 
    android:src="@android:drawable/ic_menu_save" 
    app:layout_anchor="@id/appbar" 
    app:layout_anchorGravity="bottom|center" 
    android:id="@+id/add_to_cart_fab" /> 

活动布局:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/fragment_container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
</FrameLayout> 

这是崩溃的视差模式。您可能想要更改它以便始终查看您的按钮,但它是附加FAB的正确方法。

+0

我已经尝试晶圆厂代码移到活动。在这种情况下,片段与工具栏重叠,所以我只能看到fab按钮的顶部。另外我使用应用程序:layout_anchorGravity属性。 – user4292106

+0

不使用FAB的风格。请参阅我的编辑例如 –

+0

该示例包含错误。在nestedScrollView属性上显示有多个根标签。 – user4292106