2017-07-27 2 views
1

Ich möchte eine Ansicht zwischen AppBarLayout und Layout verankert werden. Ich bekomme dieses Verhalten nur im Android Studio Editor, aber auf dem echten Gerät ist das anders: enter image description herelayout_anchor zwischen Symbolleiste und Layout

Ich bin wirklich verwirrt. Mein Code ist:

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

und '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

nicht sicher, aber ich denke, die Erhebung von „AvatarView“ zu ändern, um mehr als die von AppBarLayout funktionieren sollte –

+0

@AjilO. Was meinst du mit _ändernder Höhe_? Ich legte 'app: layout_anchor =" @ + id/whole_layout " app: layout_anchorGravity =" top | start "', um das gewünschte Verhalten zu erreichen – Choletski

+0

True. Aber 'whole_layout' würde unter' AppBarLayout' platziert werden. Ich habe es nicht bestätigt; Aber ich glaube, 'AppBarLayout' hat standardmäßig eine Höhe von 8dp. Wo wie Ihr LinearLayout ('whole_layout') wäre bei 0dp Höhe. Und die 'RelativeView', die Sie zu verankern versuchen, erhält standardmäßig die Höhe von 0dp –

Antwort

2

Gerade versucht es. Wie ich in den Kommentaren erwähnt habe, ist Ihr whole_layout ein LinearLayout mit Höhe 0dp. Jedes Element, das Sie an diesem LinearLayout verankern, erhält standardmäßig eine Höhe von 0dp (wie die Ansicht, an der Sie verankern). Dies bedeutet, dass RelativeLayout eine Höhe von 0dp haben wird.

Die AppBarLayout hat eine Höhe von 4dp, daher wird die immer standardmäßig darunter angezeigt.

The Fix

eine Erhöhung der relativen Layout geben, die außer dies wird Ihr Problem beheben größer oder gleich 4 ist, dass die RelativeLayout wird nun einen Schatten zu werfen. Hier

ist der Code für die 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> 
Verwandte Themen