android - 折叠工具栏在 Android 5.0 以下不工作

标签 android xml android-collapsingtoolbarlayout

我在折叠工具栏时遇到问题。

此代码适用于 android 5+ 但不适用于 android 4.1.2。我不太熟悉折叠工具栏,我想我错过了一些东西。 在 Android 4.1.2 上,当此 Activity 启动时,我的设备上只会出现黑屏,Android Monitor 中没有错误。

有什么建议吗?

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

配置文件.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 list

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

最佳答案

不知道为什么低于Android 5.0

android:src=@drawable/b1分配给时折叠工具栏不起作用 折叠工具栏中的 ImageView 。 删除 android:src 后它会工作。 只需在 Activity 的 onCreate 中将图像加载到 ImageView。 现在一切都很好。

这对我有用

关于android - 折叠工具栏在 Android 5.0 以下不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44510727/

相关文章:

android - 检查元素是否适合屏幕

在最后一个屏幕上滑动时的 Android ViewPager

java - cordova 8 发送无声短信

java - AssignQualification 操作可能出现的错误列表 (Mechanical Turk)

android - AppBarLayout 和 CollapsingToolbarLayout 无法包含 Button?

java - 尝试在空对象引用上调用接口(interface)方法sharedPreferences.edit()'

java - Android 应用程序一开始就崩溃

xml - 根据数组中数据的匹配更新 XML 文件的特定节点

android - CollapsingToolbarLayout 箭头颜色

android - 如何将 View 放在 CollapsingToolbarLayout 上方