6

Wenn ein CoordinatorLayout mit AppBarLayout auf einige Aktivitäten mit brauche ich den Inhalt unter die AppBarLayout sein, das heißt die Toolbar eine transparente Farbe verwendet, und liegt über den Inhalt. Standardmäßig werden CoordinatorLayout + AppBarLayout so angeordnet, dass die Symbolleiste und der Bildlauf-Inhalt nebeneinander liegen, ohne sich zu überlappen.Wie AppBarLayout zu schaffen, die Inhalte von CoordinatorLayout überlappt

Android Entwickler Führer haben die Dokumentation zu diesem here und es sieht so aus (aber diese Flaggen scheinen nicht mit Toolbar zu arbeiten und appcompat - Ich habe versucht):

Overlaying ActionBar

Also ich brauche etwas, das sieht aus wie auf dem Bild oben, aber mit allen Scrolling Goodies von CoordinatorLayout + AppBarLayout. Und es gibt keine Notwendigkeit, CollapsingToolbarLayout zu verwenden - nur dieses einfache.

Irgendwelche Hinweise, wie Sie dies erreichen können? Hier ist mein Aktivitätslayout.

<android.support.design.widget.CoordinatorLayout 
    android:id="@+id/top_content_frame" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <android.support.design.widget.AppBarLayout 
     android:background="@android:color/transparent" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     > 
     <include layout="@layout/main_toolbar"/> 
    </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" 
     > 
     <!-- to be filled by content fragment --> 
    </FrameLayout> 
    <android.support.design.widget.FloatingActionButton 
     style="@style/FabStyle" 
     android:id="@+id/fab_button" 
     android:src="@drawable/bt_filters" 
     /> 
</android.support.design.widget.CoordinatorLayout> 
+0

gegeben habe http://inthecheesefactory.com/blog/android-design-support -library-codelab/de –

+0

@JudeFernandes dies beschreibt ein Setup mit 'CollapsingToolbarLayout' welches durch ein bestimmtes Image gesichert wird. Ich habe eine andere Situation beschrieben - ich brauche die Aktivität ** content ** in der transparenten Symbolleiste. Und ich brauche nicht 'CollapsingToolbarLayout', wie ich oben erwähnt habe. – dimsuz

+0

Muss Ihre AppBar beim Scrollen nach Inhalten immer noch angezeigt werden oder kann sie statisch bleiben? Ist es nur der Aktivitätshintergrund, der unterhalb der Toolbar sein muss, oder auch der Aktivitätsinhalt? –

Antwort

3

Ich habe diese Lösung versucht, es funktioniert.

Transparenz: hinzugefügt Hintergrund AppBarLayout und platziert in das Layout vor AppBarLayout

<android.support.design.widget.AppBarLayout 
android:id="@+id/app_bar_layout" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:background="#00000000" > 

Inhalt Positionierung Scrolling-Ansicht: verlängert AppBarLayout.ScrollingViewBehavior durch neue AppbBarTransparentScrollingViewBehavior onDependentViewChanged() Überschreiben und die Änderung updateOffset() zu offset = 0

@Override 
public boolean onDependentViewChanged(CoordinatorLayout parent, View child, 
     View dependency) { 
    updateOffset(parent, child, dependency); 
    return false; 
} 

private boolean updateOffset(CoordinatorLayout parent, View child, 
     View dependency) { 
    final CoordinatorLayout.Behavior behavior = ((CoordinatorLayout.LayoutParams) dependency 
      .getLayoutParams()).getBehavior(); 
    if (behavior instanceof Behavior) { 
     // Offset the child so that it is below the app-bar (with any 
     // overlap) 
     final int offset = 0; // CHANGED TO 0 
     setTopAndBottomOffset(offset); 
     return true; 
    } 
    return false; 
} 

neuer Verhalten Inhalt: set Verhalten auf

Ansicht scrollen
<android.support.v4.view.ViewPager 
android:id="@+id/view_pager" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
layout_behavior="AppbBarTransparentScrollingViewBehavior" /> 

Ergebnis: mit einem Image in einem NestedScrollView als Lauf Ansicht

enter image description here

+0

die einzige Antwort auf den gesamten Stackoverflow und mehr, die als crappy 'com.android.support:appcompat-v7:23.1.1' funktioniert. GG Mann, danke eine Million! – kellogs

+2

Große Antwort. Vergessen Sie nicht, einen Konstruktor mit dem Kontext und den attributeSet-Parametern hinzuzufügen (nennen Sie einfach 'super()' darin), wenn Sie Ihr benutzerdefiniertes Verhalten definieren, sonst werden Sie beim Aufblasen des Layouts einige Probleme bekommen. – fstephany

3

Wenn Sie die Zeile

app:layout_behavior="@string/appbar_scrolling_view_behavior" 

vom FrameLayout, die Inhalte entfernen wird vom Toolbar überlappen. Ich hoffe, das hilft.

EDIT: Oh Sie erwähnen, dass Sie die Scroll-Goodies brauchen, so ist dies nicht wirklich eine Lösung.

1

Here i Haupthintergrundbild zu geben versucht, zu windowBackground und ToolBar/ActionBar Hintergrund als Transparent. Unter Style habe ich in Manifest angegeben. Fensterhintergrundänderungen können wie erforderlich vorgenommen werden. Stil in Menifest

<style name="AppThemeSliderToolbar" parent="Theme.AppCompat.Light.NoActionBar"> 
     <item name="android:windowBackground">@drawable/t_img</item> 
     <item name="colorPrimary">#ff5b45</item> 
     <item name="colorPrimaryDark">#FF5722</item> 
    </style> 

-Layout mit AppBar mit halbtransparenten Hintergrund

<RelativeLayout 
     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:background="#50000000" 

      > 
      <!--Change Opacity background as per required ..android:background="#50000000"--> 
      <android.support.v7.widget.Toolbar 
       android:id="@+id/toolbar" 
       android:layout_width="match_parent" 
       android:layout_height="?attr/actionBarSize" 
       app:layout_scrollFlags="scroll|enterAlways" /> 
     </android.support.design.widget.AppBarLayout> 

     <FrameLayout 
      android:id="@+id/frgmentcontainer" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_below="@+id/appbar"></FrameLayout> 


    </RelativeLayout> 

    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/fab" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom|right" 
     android:layout_marginBottom="20dp" 
     android:layout_marginRight="20dp" 
     android:src="@android:drawable/ic_dialog_email" 
     app:fabSize="normal" /> 
</android.support.design.widget.CoordinatorLayout> 

UPDATE

Laut unserer Diskussion auf Kommentar CollapsingToolbarLayout mit Fragment.

<?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=".ScrollingActivity"> 

    <android.support.design.widget.AppBarLayout android:id="@+id/app_bar" 
     android:fitsSystemWindows="true" android:layout_height="@dimen/app_bar_height" android:background="#00FFFFFF" 
     android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay"> 

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

      <android.support.v7.widget.Toolbar android:id="@+id/toolbar" 
       android:layout_height="?attr/actionBarSize" android:layout_width="match_parent" 
       app:layout_collapseMode="pin" app:popupTheme="@style/AppTheme.PopupOverlay" /> 

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

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

    <android.support.design.widget.FloatingActionButton android:id="@+id/fab" 
     android:layout_width="wrap_content" android:layout_height="wrap_content" 
     android:layout_margin="@dimen/fab_margin" app:layout_anchor="@id/app_bar" 
     app:layout_anchorGravity="bottom|end" android:src="@android:drawable/ic_dialog_email" /> 

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

content_scrolling.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.NestedScrollView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    tools:showIn="@layout/activity_scrolling" android:layout_width="match_parent" 
    android:layout_height="match_parent" tools:context=".ScrollingActivity"> 
    <FrameLayout android:id="@+id/framcontainer" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"></FrameLayout> 

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

Stil zu Aktivität in Manifest gegeben.

<style name="AppThemeSliderToolbar" parent="Theme.AppCompat.Light.NoActionBar"> 
     <item name="android:windowBackground">@drawable/t_img</item> 
     <item name="android:windowContentOverlay">@null</item> 
     <item name="windowActionBarOverlay">true</item> 
     <item name="colorPrimary">@android:color/transparent</item> 
    </style> 

ScrollingActivity

public class ScrollingActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_scrolling); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
     fab.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 
         .setAction("Action", null).show(); 
      } 
     }); 
     getSupportFragmentManager().beginTransaction(). 
       replace(R.id.framcontainer, new HomeFragment(), "Home").commit(); 
    } 

} 

Home2

public class Home2 extends Fragment { 
    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     return inflater.inflate(R.layout.home_2, container, false); 
    } 
} 

HomeFragment

public class HomeFragment extends Fragment { 
    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     return inflater.inflate(R.layout.homefragment, container, false); 
    } 

    @Override 
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 
     super.onViewCreated(view, savedInstanceState); 
     view.findViewById(R.id.txt).setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       getActivity().getSupportFragmentManager().beginTransaction() 
         .replace(R.id.framcontainer, new Home2(), "Home2").addToBackStack("Home2").commit(); 
      } 
     }); 
    } 
} 

Screenshot URL. Vermieden Ambeding Bilder i url

Bevor aktualisiertem Am http://i.stack.imgur.com/5cVOw.jpg
HomeFragment Von aktualisiert Am>http://i.stack.imgur.com/UF8LW.jpg
Home2 Von aktualisiert Am http://i.stack.imgur.com/cD480.jpg

+0

können wir dasselbe in einem Fragment tun, ohne eine neue Symbolleiste im Fragmentlayout zu erstellen? – user2095470

+0

@ user2095470 hier habe ich Fragment & ToolBar normalerweise in Aktivität verwendet. also kannst du bitte mehr erklären damit ich es versuchen kann? – user1140237

+0

danke für deine Mühe. Denken Sie, dass es möglich ist, nur in diesem Fragment zusammenklappbares Bild hinzuzufügen. (vorausgesetzt, ich habe mehr als 1 Fragment in der App) Und alle anderen Fragmente haben normale Aktivitätssymbolleiste ohne zusammenklappbare Ansicht. – user2095470

Verwandte Themen