2017-07-27 223 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> 
相關問題