5

Đây là mã.Thanh công cụ không hiển thị từ bên trong thanh công cụ thu gọn

<?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" 
    android:layout_height="match_parent" 
    android:layout_width="match_parent"> 

    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="100dp"> 

     <android.support.design.widget.CollapsingToolbarLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      app:layout_scrollFlags="scroll|enterAlways|exitUntilCollapsed"> 

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

      <android.support.design.widget.TabLayout 
       android:id="@+id/main_tablayout" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_gravity="bottom" 
       app:layout_collapseMode="none"/> 


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

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

    <!--<android.support.v4.widget.NestedScrollView--> 
     <!--android:id="@+id/nestedscroll"--> 
     <!--android:layout_width="match_parent"--> 
     <!--android:layout_height="match_parent"--> 
     <!--android:fillViewport="true"--> 
     <!--android:scrollbars="horizontal"--> 
     <!--app:layout_behavior="@string/appbar_scrolling_view_behavior">--> 

      <android.support.v4.view.ViewPager 
       android:id="@+id/main_viewpager" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       app:layout_behavior="@string/appbar_scrolling_view_behavior"/> 

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

    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/main_fab" 
     android:layout_margin="16dp" 
     android:src="@android:drawable/ic_media_play" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     app:layout_anchor="@id/main_viewpager" 
     app:layout_anchorGravity="bottom|end"/> 

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

Vấn đề:
1) Thanh công cụ là không nhìn thấy được.
2) Thanh công cụ thu gọn không hề sụp đổ. [Solved]
3) Viewpager và FAB cũng không hiển thị nếu được đặt bên trong nestedScrollView. [Solved]

thêm chi tiết:
Layout cho mảnh ViewPager có LinearLayout như là người chủ và bên trong đó có một recyclerview.

Mọi thứ dường như không sao theo mã. Không thể hiểu những gì còn thiếu. Một lời giải thích tốt về cách bố trí phối hợp và thu gọn thanh công cụ làm việc cùng nhau cũng sẽ thực sự giúp đỡ.

Trả lời

4

1) Thanh công cụ không hiển thị.

Trước hết bạn cần xác định những gì Thanh công cụ nào bạn muốn sử dụng trong lớp học hoạt động của bạn:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
setSupportActionBar(toolbar); 

Đổi mã xml hiện:

 <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="?attr/colorPrimary" 
      app:title="@string/app_name" 
      app:layout_collapseMode="parallax"> 
     </android.support.v7.widget.Toolbar> 

tới:

 <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" //set initial height 
      app:popupTheme="@style/ThemeOverlay.AppCompat.Light" //this might be also useful 
      app:title="@string/app_name" 
      app:layout_collapseMode="parallax" /> 

2) Thu gọn thanh công cụ không sụp đổ chút nào.

Hoạt động của bạn có sử dụng đúng chủ đề không. Đặt AppBarLayout của bạn:

android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 

như trong ví dụ này: include_list_viewpager.xml

3) ViewPager và FAB cũng không hiển thị nếu đặt bên nestedScrollView.

Không có lý do gì để làm điều đó. Thêm các dòng này:

android:layout_marginTop="?attr/actionBarSize" 
app:layout_behavior="@string/appbar_scrolling_view_behavior" 

đến ViewPager là đủ.

Cả hai người trong số họ phải là con trực tiếp của CoordinatorLayout.

theo ví dụ này: http://blog.nkdroidsolutions.com/collapsing-toolbar-with-tabs-android-example/

Nếu bạn mới để thiết kế Chất liệu hoặc cảm thấy một chút mất với một số hành vi của nó, tôi khuyên bạn nên kiểm tra dự án Chris Banes Vật liệu Thiết kế cheesequare: https://github.com/chrisbanes/cheesesquare/

Hy vọng nó sẽ giúp

+0

đã tìm hình thứ 2 và thứ 3. Cái đầu tiên không hoạt động ngay cả sau khi thực hiện điều này 'Thanh công cụ Thanh công cụ = (Thanh công cụ) findViewById (R.id.toolbar); setSupportActionBar (thanh công cụ); ' –

+0

@DarshanMiskin Tôi đã cập nhật câu trả lời của mình. Bạn đã quên đặt chiều cao Thanh công cụ mặc định - 'wrap_content' có thể nằm trong trường hợp đó bằng 0. Thuộc tính 'actionBarSize' đặt kích thước Thanh công cụ chuẩn của Android. – piotrek1543

+0

đã thử nó .. vẫn không có gì .. –

2

Trước hết bạn nên nói với hoạt động của bạn rằng những gì thanh công cụ bạn đang sử dụng, vì vậy trong phương pháp onCreate bạn nên có:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 

Vấn đề thứ hai và thứ ba của bạn nên giải quyết với nhau. Bạn nên sử dụng NestedScrollView làm bố cục chính cho các phân đoạn bên trong ViewPager và sau đó bên trong đó, đặt LinearLayout của bạn hoặc bất kỳ thứ gì khác.

+0

NestedScrollView không cần thiết thuộc tính 'layout_behavior' là quan trọng. kiểm tra điều này ra http://stackoverflow.com/questions/31275277/viewpager-not-appearing-when-using-collapsingtoolbarlayout. Ngoài ra, sau khi thay đổi chủ đề, thanh công cụ thu gọn không bị thu hẹp. Sẽ kiểm tra mã của bạn ngay bây giờ. –

+0

vẫn không hoạt động. –

+0

sau khi thay đổi chủ đề, thanh công cụ thu gọn là ** NOW ** bị thu gọn. –

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