2017-06-13 87 views
1

我有折叠工具栏的问题。折叠工具栏不能在Android 5.0下工作

此代码适用于android 5+,但不适用于android 4.1.2。我对Collapsing Toolbar不太熟悉,我想我错过了一些事情。 在Android 4.1.2上,当此活动启动时,只有黑屏出现在我的设备上,没有别的,Android Monitor中没有错误。

有什么建议吗?

您的帮助将不胜感激。 感谢

Profile.java

public class Profile extends AppCompatActivity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_profile); 

     Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 
     if(getSupportActionBar() != null) 
      getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
    } 
} 

activity_profile.xml

<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:background="@android:color/white" 
    android:fitsSystemWindows="true"> 

    <android.support.design.widget.AppBarLayout 
     android:id="@+id/appbar" 
     android:layout_width="match_parent" 
     android:layout_height="350dp" 
     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:fitsSystemWindows="true" 
      app:contentScrim="?attr/colorPrimary" 
      app:expandedTitleMarginBottom="10dp" 
      app:expandedTitleMarginStart="20dp" 
      app:layout_scrollFlags="scroll|exitUntilCollapsed"> 

      <ImageView 
       android:src="@drawable/b1" 
       android:id="@+id/backdrop" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:fitsSystemWindows="true" 
       android:scaleType="centerCrop" 
       app:layout_collapseMode="parallax" /> 

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

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

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

    <android.support.v4.widget.NestedScrollView 
     android:id="@+id/nested_scroll_view_detail" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior"> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical" 
      android:layout_margin="5dp" 
      android:background="#456789"> 
      <TextView 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:padding="12dp" 
       android:layout_margin="12dp" 
       android:textSize="22dp" 
       android:textStyle="bold" 
       android:background="#333" 
       android:textColor="#F0F0F0" 
       android:text="A Demo Text View"/> 
     </LinearLayout> 

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

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

style.xml

<!-- Base application theme. --> 
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> 
    <!-- Customize your theme here. --> 
    <item name="colorPrimary">@color/colorPrimary</item> 
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item> 
    <item name="colorAccent">@color/colorAccent</item> 
</style> 

<style name="AppTheme.NoActionBar"> 
    <item name="windowActionBar">false</item> 
    <item name="windowNoTitle">true</item> 
</style> 

Android清单

<activity 
    android:name=".Profile" 
    android:theme="@style/AppTheme.NoActionBar" /> 

回答

0

我不知道为什么,但下面的是Android 5.0

折叠式工具不能正常工作时分配在折叠式工具android:[email protected]/b1到 图片浏览。 删除android:src后,它将工作。 只需将图像加载到onCreate的ImageView中即可。 现在每件事情都很好。

这工作对我来说