2017-01-09 64 views
0

我有以下布局代码。相对布局是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:fab="http://schemas.android.com/tools" 
    android:layout_width="match_parent" android:layout_height="match_parent" 
    android:background="@android:color/background_light" 
    android:fitsSystemWindows="true" 
> 

    <android.support.v7.widget.Toolbar 
     android:id="@+id/toolbar" 
     android:layout_width="match_parent" 
     android:layout_height="?attr/actionBarSize" 
     android:background="@color/colorPrimary" 
     android:minHeight="?attr/actionBarSize" 
     app:navigationIcon="?attr/homeAsUpIndicator" 
     app:popupTheme="@style/ThemeOverlay.AppCompat.Light" 
     app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> 
     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 
      <TextView 
       android:id="@+id/toolbar_title" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_alignParentLeft="true" 
       android:layout_alignParentStart="true" 
       android:singleLine="true" 
       android:text="This will run the marquee animation forever" 
       android:textColor="@color/white" 
       android:textSize="@dimen/abc_text_size_title_material_toolbar"/> 

     </RelativeLayout> 

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


    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/share" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_margin="@dimen/activity_horizontal_margin" 
     android:src="@drawable/ic_menu_share" 
     app:layout_anchor="@+id/toolbar" 
     app:layout_anchorGravity="bottom|right|end" 
    /> 
    <RelativeLayout 
     android:layout_width="match_parent" 
     android:paddingTop="?attr/actionBarSize" 
     android:layout_height="match_parent" 
     android:orientation="vertical" 
     app:layout_anchor="@+id/share" 
     app:layout_anchorGravity="bottom" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior"> 

     <LinearLayout 
      android:id="@+id/header" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_alignParentTop="true" 
      android:layout_centerInParent="true" 
      android:orientation="vertical"> 

      <RadioGroup 
       android:id="@+id/segment_text" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center" 
       android:layout_margin="5dip" 
       android:layout_marginTop="?attr/actionBarSize" 
       android:background="@android:color/white" 
       android:bottomLeftRadius="10dp" 
       android:bottomRightRadius="10dp" 
       android:checkedButton="@+id/button_one" 
       android:orientation="horizontal" 
       android:topRightRadius="0dp"> 

       <RadioButton 
        android:id="@+id/button_one" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" 
        android:background="@drawable/rbtn_selector" 
        android:button="@null" 
        android:checked="true" 
        android:gravity="center" 
        android:padding="5dp" 
        android:text=" A  " 
        android:textColor="@drawable/rbtn_text_selector" 
       /> 

       <RadioButton 
        android:id="@+id/button_two" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" 
        android:background="@drawable/rbtn_selector_right" 
        android:button="@null" 
        android:gravity="center" 
        android:padding="5dp" 
        android:text=" B  " 
        android:textColor="@drawable/rbtn_text_selector"/> 

      </RadioGroup> 
     </LinearLayout> 

     <android.support.v7.widget.RecyclerView 
      android:id="@+id/list_view" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_below="@+id/header"/> 

     <com.getbase.floatingactionbutton.FloatingActionButton 
      android:id="@+id/play_song_fab" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentBottom="true" 
      android:layout_alignParentEnd="true" 
      android:layout_alignParentRight="true" 
      android:visibility="gone" 
      fab:fab_colorNormal="@color/dark_navy_blue" 
      fab:fab_colorPressed="@color/light_navy_blue" 
      fab:fab_icon="@drawable/ic_play_arrow_white"/> 
    </RelativeLayout> 
</android.support.design.widget.CoordinatorLayout> 

我试着去忍受相对布局工具栏下方。我尝试加入app:layout_behavior="@string/appbar_scrolling_view_behavior",但它仍然无效。我尝试添加android:paddingTopandroid:marginTop属性。添加这些属性可以在界面中正常工作,但是当涉及到真实设备时,相对布局仍与工具栏重叠。为什么这样?

回答

1

你可以把你的Toolbar布局在AppBarLayout里面。像这样

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

     // Your Toolar herer 
     <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/ThemeOverlay.AppCompat.Light"/> 

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

    <FrameLayout 
     android:id="@+id/content" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior"/> 
</android.support.design.widget.CoordinatorLayout> 
+0

我已经尝试过了。替换我的代码在这段代码中不起作用 –

+0

删除代码:'app:layout_anchor =“@ + id/share” app:layout_anchorGravity =“bottom”' –

+0

它的工作原理........ –