2016-01-12 22 views
5

Làm cách nào để thay đổi bù đắp của AppBarLayout theo lập trình?Thay đổi bù đắp AppBarLayout lập trình

Tôi muốn một khoản bù đắp nhất định cho AppBarLayout (được mở rộng một phần) khi Hoạt động tải lần đầu tiên, sau đó người dùng có thể mở rộng thêm hoặc thu gọn hoạt động đó. Hành vi hiện tại là nó được mở rộng hoàn toàn khi Hoạt động tải lần đầu tiên.

my.xml

<android.support.design.widget.AppBarLayout 
     android:id="@+id/app_bar_layout" 
     android:layout_width="match_parent" 
     android:layout_height="300dp" 
     android:fitsSystemWindows="true" 
     android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> 

     <android.support.design.widget.CollapsingToolbarLayout 
      android:id="@+id/collapsing_toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:clickable="true" 
      android:fitsSystemWindows="true" 
      app:expandedTitleMarginEnd="64dp" 
      app:expandedTitleMarginStart="48dp" 
      app:layout_collapseParallaxMultiplier="0.7" 
      app:layout_scrollFlags="scroll|exitUntilCollapsed"> 

      <FrameLayout 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       app:layout_collapseMode="parallax"> 

       <ImageView 
        android:id="@+id/image" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:scaleType="centerCrop" 
        android:src="@drawable/pic"/> 

       <View 
        android:id="@+id/overlay" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:background="@drawable/overlay" 
        app:layout_collapseMode="pin"/> 
      </FrameLayout> 

      <android.support.v7.widget.Toolbar 
       android:id="@+id/toolbar" 
       android:layout_width="match_parent" 
       android:layout_height="?attr/actionBarSize" 
       app:layout_collapseMode="pin" 
       app:popupTheme="@style/ThemeOverlay.AppCompat.Light" 
       app:title="@string/app_name"/> 

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

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

Cần hỗ trợ API 16+

Cảm ơn!

Trả lời

2

Sử dụng này: ScrollableAppBar

Bạn chỉ cần phải thay thế của bạn AppBarLayout với:

<it.michelelacorte.scrollableappbar.ScrollableAppBar 
android:id="@+id/appbar" 
android:layout_width="match_parent" 
android:layout_height="380dp" 
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 
android:fitsSystemWindows="true"> 
</it.michelelacorte.scrollableappbar.ScrollableAppBar> 

và hơn:

ScrollableAppBar appBarLayout = (ScrollableAppBar) findViewById(R.id.appbar); 

//To give the effect "in the middle" of the image (like gif) 
appBarLayout.collapseToolbar(); 

Output:

enter image description here

+0

Hi Michele, Cảm ơn bạn đã trả lời! Nó thực hiện công việc! nhưng, tôi phải hỗ trợ API 16+, tôi đã cập nhật câu hỏi của mình cho phù hợp. –

+0

Tôi nghĩ rằng không thể @AdamGhani –

+1

đó là cách whatsapp đã làm điều đó trên hồ sơ người dùng, cảm ơn –

Các vấn đề liên quan