2017-07-27 222 views
1

我想要将视图锚定在AppBarLayout和布局之间。我只在Android Studio编辑器上得到这种行为,但在真实设备上是这样的: enter image description here工具栏和布局之间的layout_anchor

我真的很困惑。我的代码是:

<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" 
    tools:context="view.activity.MainActivity"> 

<android.support.design.widget.AppBarLayout 
    android:id="@+id/bar_layout" 
    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" 
     app:popupTheme="@style/AppTheme.PopupOverlay" /> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_marginStart="84dp" 
     android:layout_marginTop="@dimen/activity_vertical_margin"> 

     <!--views on toolbar--> 

    </RelativeLayout> 

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

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

<RelativeLayout 
    android:layout_width="60dp" 
    android:layout_height="60dp" 
    android:layout_marginStart="@dimen/activity_horizontal_margin" 
    android:background="@drawable/circle_accent" 
    app:layout_anchor="@+id/whole_layout" 
    app:layout_anchorGravity="top|start"> 

    <!--img view--> 
</RelativeLayout> 

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

和 'activity_home_content'

<LinearLayout 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/whole_layout" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
app:layout_behavior="@string/appbar_scrolling_view_behavior" 
tools:context="view.activity.MainActivity" 
tools:showIn="@layout/activity_home_toolbar"> 


<!--some other views--> 

</LinearLayout> 
+2

不知道的代码,但我想改变“AvatarView”的标高比AppBarLayout更应该工作 –

+0

@AjilO。你是什​​么意思_改变高程_?我把'app:layout_anchor =“@ + id/whole_layout” app:layout_anchorGravity =“top | start”'实现所需的行为 – Choletski

+0

是的。但'whole_layout'会放在'AppBarLayout'下面。我没有确认;但我相信'AppBarLayout'默认情况下的海拔高度为8dp。当你的LinearLayout('whole_layout')在0dp标高时。并且您试图锚定的'RelativeView'默认情况下也会将其作为0dp提升 –

回答

2

就试了一下。正如我在评论中提到的,您的whole_layout是一个LinearLayout,高程为0dp。默认情况下,您锚定到此LinearLayout的任何元素将收到0dp的高程(与您要锚定的视图相同)。这意味着RelativeLayout将具有0dp的海拔。

AppBarLayout的标高为4dp,因此默认情况下RelativeLayout将始终在其下方绘制。

的修复

举一个海拔相对布局大于或等于4,这将解决您的问题,不同之处在于RelativeLayout现在蒙上了一层阴影了。

下面是RelativeLayout

<RelativeLayout 
    android:layout_width="60dp" 
    android:layout_height="60dp" 
    android:layout_marginStart="@dimen/activity_horizontal_margin" 
    android:background="@drawable/circle_accent" 
    app:layout_anchor="@+id/whole_layout" 
    app:layout_anchorGravity="top|start" 
    android:elevation="4dp"> 

    <!--img view--> 

</RelativeLayout> 
相关问题