webentwicklung-frage-antwort-db.com.de

Wie blende ich die Toolbar aus, wenn ich Inhalte in Android nach oben scrolle?

Ich versuche, meine Werkzeugleiste auszublenden, wenn ich meinen Text und mein Bild mit Inhalten scrolle. Hier verwende ich ScrollView, um Bildlaufinhalte zu erhalten 

hier mein Xml-Code

content_main.XML

<Android.support.v4.widget.NestedScrollView
xmlns:app="http://schemas.Android.com/apk/res-auto"
Android:layout_width="match_parent"
Android:layout_height="match_parent"
xmlns:Android="http://schemas.Android.com/apk/res/Android"
app:layout_behavior="@string/appbar_scrolling_view_behavior">


<LinearLayout
    Android:orientation="vertical"
    Android:layout_width="match_parent"
    Android:layout_height="match_parent">

<LinearLayout
    Android:paddingTop="?android:attr/actionBarSize"
    Android:orientation="vertical"
    Android:layout_width="match_parent"
    Android:layout_height="match_parent">

    <TextView
        Android:layout_marginLeft="10dp"
        Android:layout_marginRight="10dp"
        Android:id="@+id/textone"
        Android:layout_width="match_parent"
        Android:layout_height="match_parent"
        Android:textSize="23dp"
        Android:textStyle="bold"
        Android:text="hello world jheds sdjhs jds sjbs skjs ksjs kksjs ksj sdd dskd js sk "/>

    <ImageView
        Android:id="@+id/imge"
        Android:layout_width="match_parent"
        Android:layout_height="250dp"
        Android:src="@drawable/imag_bg"/>


    <TextView
        Android:id="@+id/texttwo"
        Android:layout_width="match_parent"
        Android:layout_height="match_parent"
        Android:layout_marginLeft="10dp"
        Android:layout_marginRight="10dp"
        Android:text="Pretty good, the Toolbar is moving along with the list and getting back just as we expect it to. This is thanks to the restrictions that we put on the mToolbarOffset variable.
        If we would omit checking if it’s bigger than 0 and lower than mToolbarHeight then when
        we would scroll
         up our list, the Toolbar would move along far away off the screen, so to show it back you
         would have to scroll the list down to 0. Right now it just scrolls up to mToolbarHeight
         position and not more so it’s “sitting” right above the list all of the time and if we
         start scrolling down, we can see it immediately showing.



          up our list, the Toolbar would move along far away off the screen, so to show it back you
         would have to scroll the list down to 0. Right now it just scrolls up to mToolbarHeight
         position and not more so it’s “sitting” right above the list all of the time and if we
         start scrolling down, we can see it immediately showing

          up our list, the Toolbar would move along far away off the screen, so to show it back you
         would have to scroll the list down to 0. Right now it just scrolls up to mToolbarHeight
         position and not more so it’s “sitting” right above the list all of the time and if we
         start scrolling down, we can see it immediately showing

          up our list, the Toolbar would move along far away off the screen, so to show it back you
         would have to scroll the list down to 0. Right now it just scrolls up to mToolbarHeight
         position and not more so it’s “sitting” right above the list all of the time and if we
         start scrolling down, we can see it immediately showing

          up our list, the Toolbar would move along far away off the screen, so to show it back you
         would have to scroll the list down to 0. Right now it just scrolls up to mToolbarHeight
         position and not more so it’s “sitting” right above the list all of the time and if we
         start scrolling down, we can see it immediately showing

          up our list, the Toolbar would move along far away off the screen, so to show it back you
         would have to scroll the list down to 0. Right now it just scrolls up to mToolbarHeight
         position and not more so it’s “sitting” right above the list all of the time and if we
         start scrolling down, we can see it immediately showing
        It works pretty well, but this is not what we want. It feels weird that you can
        stop it in the middle of
        the

         scroll and the Toolbar will stay half visible. Actually this is how it’s done in Google Play
         Games app
        which I consider as a bug

        It works pretty well, but this is not what we want. It feels weird that you can
        stop it in the middle of
        the
         scroll and the Toolbar will stay half visible. Actually this is how it’s done in Google Play
         Games app
        which I consider as a bug
        It works pretty well, but this is not what we want. It feels weird that you can
        stop it in the middle of
        the
         scroll and the Toolbar will stay half visible. Actually this is how it’s done in Google Play
         Games app
        which I consider as a bug."/>




</LinearLayout>

<View
    Android:layout_width="wrap_content"
    Android:layout_height="30dp" />
<LinearLayout

    Android:layout_width="match_parent"
    Android:layout_height="match_parent">
    <Button
         Android:text="hai"
         Android:layout_width="160dp"
         Android:layout_height="match_parent" />
    <Button
         Android:text="hello"
         Android:layout_width="160dp"
         Android:layout_height="match_parent" />


</LinearLayout>

activity_main.XML

<Android.support.design.widget.AppBarLayout
    Android:layout_height="wrap_content"
    Android:layout_width="match_parent" 
    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" />

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

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

 here my image

14
chanti

sie müssen viele Änderungen an beiden Layouts vornehmen. Verwenden Sie zuerst CoordinatorLayout in activity_main.XML wie unten (ändern Sie das Design entsprechend Ihrer Anforderung).

<?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"
    Android:id="@+id/main_content"
    Android:layout_width="match_parent"
    Android:layout_height="match_parent"
    Android:fitsSystemWindows="true">

    <Android.support.design.widget.AppBarLayout
        Android:layout_width="match_parent"
        Android:layout_height="wrap_content"
        Android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <Android.support.v7.widget.Toolbar
            Android:id="@+id/toolbar"
            Android:layout_width="match_parent"
            Android:layout_height="?attr/actionBarSize"
            Android:background="?attr/colorPrimary"
            Android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

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

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

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

verwenden Sie in content_main.XMLAndroid.support.v4.widget.NestedScrollView anstelle von ScrollView

verwenden Sie auch app:layout_behavior="@string/appbar_scrolling_view_behavior" in Android.support.v4.widget.NestedScrollView wie unten.

<Android.support.v4.widget.NestedScrollView
        xmlns:app="http://schemas.Android.com/apk/res-auto"
        Android:layout_width="match_parent"
        Android:layout_height="match_parent"
        xmlns:Android="http://schemas.Android.com/apk/res/Android" 
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <LinearLayout
            Android:layout_width="match_parent"
            Android:layout_height="match_parent"
            Android:orientation="vertical">

            <TextView
                Android:id="@+id/textone"
                Android:layout_width="match_parent"
                Android:layout_height="match_parent"
                Android:text="hello world jheds sdjhs jds sjbs skjs ksjs kksjs ksj sdd dskd js sk "
                Android:textSize="25dp"
                Android:textStyle="bold" />

            /// Add your other code here

            </LinearLayout>

    </Android.support.v4.widget.NestedScrollView>
29

Dies ist das beste Szenario, um CoordinatorLayout in Ihrer App zu verwenden. CoordinatorLayout ist ein FrameLayout mit Super-Power, das viele raffinierte Tricks auf den Ärmeln hat.

Die Designbibliothek führt CoordinatorLayout ein, ein Layout, das bietet eine zusätzliche Kontrolle über Berührungsereignisse zwischen Kindansichten, etwas, das viele der Komponenten im Design Bibliothek nutzen.

Sie können mit this und this Tutorial beginnen. 

1
Aritra Roy

Wickeln Sie activity_main.xml in das Koordinatorlayout ein, sodass es das übergeordnete Layout 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:id="@+id/main_content"
Android:layout_width="match_parent"
Android:layout_height="match_parent"
Android:fitsSystemWindows="true">

<Android.support.design.widget.AppBarLayout
Android:layout_height="wrap_content"
Android:layout_width="match_parent" 
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" />
</Android.support.design.widget.AppBarLayout>

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



</Android.support.design.widget.CoordinatorLayout>
0
Lukas Anda