图像未显示在折叠工具栏中

时间:2018-07-24 08:38:21

标签: android android-layout imageview android-toolbar android-collapsingtoolbarlayout

第一次启动活动时,不显示图像,但在u滚动时,显示图像。我真的很困惑这个问题,请帮忙。是因为我使用的是ConstraintLayout还是RecyclerView或LinearLayout?

下面是我使用的XML代码

     <?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".InsideActivity">

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

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:contentScrim="@android:color/transparent"
            android:fitsSystemWindows="true"
            >
            <ImageView
                android:id="@+id/collapsing_toolbar_image_view"//this where i add image to toolbar
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="centerCrop"
                app:layout_collapseMode="parallax"/>

            <android.support.v7.widget.Toolbar //this where the toolbar asign
                android:id="@+id/collapsing_toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"/>

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

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

    <android.support.v4.widget.NestedScrollView //Im using to this, to make it scrollable
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <android.support.v7.widget.CardView 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:layout_width="match_parent"
            android:layout_height="match_parent"
            app:cardCornerRadius="4dp">

            <com.facebook.shimmer.ShimmerFrameLayout // this to make shimmer effect when loading
                android:id="@+id/shimmer"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_alignParentStart="true"
                android:layout_alignParentTop="true"
                app:shimmer_clip_to_children="true"
                app:shimmer_direction="left_to_right"
                app:shimmer_shape="linear"
                android:layout_alignParentLeft="true">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

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

                </LinearLayout>

            </com.facebook.shimmer.ShimmerFrameLayout>

            <android.support.constraint.ConstraintLayout xmlns:app="http://schemas.android.com/apk/res-auto"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <ImageView
                    android:id="@+id/image_penulis"
                    android:layout_width="60dp"
                    android:layout_height="57dp"
                    android:layout_marginLeft="24dp"
                    android:layout_marginStart="24dp"
                    android:layout_marginTop="18dp"
                    android:src="@mipmap/ic_launcher_round"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/text_judul" />

                <TextView
                    android:id="@+id/text_penulis"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="8dp"
                    android:layout_marginEnd="8dp"
                    android:layout_marginLeft="8dp"
                    android:layout_marginRight="8dp"
                    android:layout_marginStart="8dp"
                    android:layout_marginTop="24dp"
                    android:text="TextView"
                    android:textAppearance="@android:style/TextAppearance.Medium"
                    android:textSize="14sp"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toEndOf="@+id/image_penulis"
                    app:layout_constraintTop_toBottomOf="@+id/text_judul"
                    app:layout_constraintVertical_bias="0.0" />

                <TextView
                    android:id="@+id/text_tanggal_tulis"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="8dp"
                    android:layout_marginEnd="8dp"
                    android:layout_marginLeft="8dp"
                    android:layout_marginRight="8dp"
                    android:layout_marginStart="8dp"
                    android:layout_marginTop="8dp"
                    android:text="TextView"
                    android:textAppearance="@style/TextAppearance.AppCompat.Small"
                    android:textSize="12sp"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintHorizontal_bias="0.0"
                    app:layout_constraintStart_toEndOf="@+id/image_penulis"
                    app:layout_constraintTop_toBottomOf="@+id/text_penulis"
                    app:layout_constraintVertical_bias="0.0" />

                <ImageView
                    android:id="@+id/image_thumbnail"
                    android:layout_width="0dp"
                    android:layout_height="200dp"
                    android:scaleType="fitXY"
                    app:layout_constraintBottom_toTopOf="@+id/text_judul"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent"
                    app:layout_constraintVertical_bias="0.0"
                    app:layout_constraintVertical_chainStyle="packed"
                    android:visibility="invisible"/>

                <TextView
                    android:id="@+id/text_judul"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginEnd="24dp"
                    android:layout_marginLeft="16dp"
                    android:layout_marginRight="24dp"
                    android:layout_marginStart="16dp"
                    android:layout_marginTop="16dp"
                    android:text="TextView"
                    android:textAppearance="@android:style/TextAppearance.Medium"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintHorizontal_bias="0.0"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/image_thumbnail" />

                <TextView
                    android:id="@+id/text_isi_berita"
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_marginEnd="8dp"
                    android:layout_marginLeft="16dp"
                    android:layout_marginRight="8dp"
                    android:layout_marginStart="16dp"
                    android:layout_marginTop="24dp"
                    android:text="TextView"
                    android:textAppearance="@android:style/TextAppearance.Medium"
                    android:textSize="14sp"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintHorizontal_bias="0.0"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/image_penulis" />

                <Button
                    android:id="@+id/btn_comment"
                    android:layout_width="200dp"
                    android:layout_height="30dp"
                    android:layout_marginBottom="24dp"
                    android:layout_marginEnd="8dp"
                    android:layout_marginLeft="8dp"
                    android:layout_marginRight="8dp"
                    android:layout_marginStart="8dp"
                    android:layout_marginTop="24dp"
                    android:background="@drawable/rounded_corner_button"
                    android:text="COMMENT"
                    android:textColor="@android:color/white"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintHorizontal_bias="0.51"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toBottomOf="@+id/text_isi_berita"
                    app:layout_constraintVertical_bias="0.0" />

            </android.support.constraint.ConstraintLayout>
        </android.support.v7.widget.CardView>
    </android.support.v4.widget.NestedScrollView>

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

下面是主要活动中的代码:

Toolbar toolbar = (Toolbar)findViewById(R.id.collapsing_toolbar);
    setSupportActionBar(toolbar);
    ActionBar actionBar = getSupportActionBar();
    if(actionBar!=null)
    {
        // Display home menu item.
        actionBar.setDisplayHomeAsUpEnabled(true);
    }
    CollapsingToolbarLayout collapsingToolbarLayout = (CollapsingToolbarLayout)findViewById(R.id.collapsing_toolbar_layout);
    collapsingToolbarLayout.setTitle("");
    ImageView collapsingToolbarImageView = (ImageView)findViewById(R.id.collapsing_toolbar_image_view);
    collapsingToolbarImageView.setImageResource(R.drawable.food);

我遵循了可以解决我的问题的所有解决方案,但事实并非如此。请帮助

1 个答案:

答案 0 :(得分:2)

尝试一下

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

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

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="@android:color/transparent"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <ImageView
                android:id="@+id/collapsing_toolbar_image_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="centerCrop"
                android:src="@drawable/kid"
                app:layout_collapseMode="parallax" />

            <android.support.v7.widget.Toolbar
                android:id="@+id/collapsing_toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin" />

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

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

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipToPadding="false"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <android.support.v7.widget.CardView 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:layout_width="match_parent"
            android:layout_height="match_parent"
            app:cardCornerRadius="4dp">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">
                <com.facebook.shimmer.ShimmerFrameLayout
                    android:id="@+id/shimmer"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true"
                    android:layout_alignParentTop="true"
                    app:shimmer_clip_to_children="true"
                    app:shimmer_direction="left_to_right"
                    app:shimmer_shape="linear">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content">

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

                    </LinearLayout>

                </com.facebook.shimmer.ShimmerFrameLayout>


                <android.support.constraint.ConstraintLayout xmlns:app="http://schemas.android.com/apk/res-auto"
                    xmlns:tools="http://schemas.android.com/tools"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <ImageView
                        android:id="@+id/image_penulis"
                        android:layout_width="60dp"
                        android:layout_height="57dp"
                        android:layout_marginLeft="24dp"
                        android:layout_marginStart="24dp"
                        android:layout_marginTop="18dp"
                        android:src="@mipmap/ic_launcher_round"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toBottomOf="@+id/text_judul" />

                    <TextView
                        android:id="@+id/text_penulis"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_marginBottom="8dp"
                        android:layout_marginEnd="8dp"
                        android:layout_marginLeft="8dp"
                        android:layout_marginRight="8dp"
                        android:layout_marginStart="8dp"
                        android:layout_marginTop="24dp"
                        android:text="TextView"
                        android:textAppearance="@android:style/TextAppearance.Medium"
                        android:textSize="14sp"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toEndOf="@+id/image_penulis"
                        app:layout_constraintTop_toBottomOf="@+id/text_judul"
                        app:layout_constraintVertical_bias="0.0" />

                    <TextView
                        android:id="@+id/text_tanggal_tulis"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_marginBottom="8dp"
                        android:layout_marginEnd="8dp"
                        android:layout_marginLeft="8dp"
                        android:layout_marginRight="8dp"
                        android:layout_marginStart="8dp"
                        android:layout_marginTop="8dp"
                        android:text="TextView"
                        android:textAppearance="@style/TextAppearance.AppCompat.Small"
                        android:textSize="12sp"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintHorizontal_bias="0.0"
                        app:layout_constraintStart_toEndOf="@+id/image_penulis"
                        app:layout_constraintTop_toBottomOf="@+id/text_penulis"
                        app:layout_constraintVertical_bias="0.0" />

                    <ImageView
                        android:id="@+id/image_thumbnail"
                        android:layout_width="0dp"
                        android:layout_height="200dp"
                        android:scaleType="fitXY"
                        android:visibility="invisible"
                        app:layout_constraintBottom_toTopOf="@+id/text_judul"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="parent"
                        app:layout_constraintVertical_bias="0.0"
                        app:layout_constraintVertical_chainStyle="packed" />

                    <TextView
                        android:id="@+id/text_judul"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_marginEnd="24dp"
                        android:layout_marginLeft="16dp"
                        android:layout_marginRight="24dp"
                        android:layout_marginStart="16dp"
                        android:layout_marginTop="16dp"
                        android:text="TextView"
                        android:textAppearance="@android:style/TextAppearance.Medium"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintHorizontal_bias="0.0"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toBottomOf="@+id/image_thumbnail" />

                    <TextView
                        android:id="@+id/text_isi_berita"
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_marginEnd="8dp"
                        android:layout_marginLeft="16dp"
                        android:layout_marginRight="8dp"
                        android:layout_marginStart="16dp"
                        android:layout_marginTop="24dp"
                        android:text="TextView"
                        android:textAppearance="@android:style/TextAppearance.Medium"
                        android:textSize="14sp"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintHorizontal_bias="0.0"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toBottomOf="@+id/image_penulis" />

                    <Button
                        android:id="@+id/btn_comment"
                        android:layout_width="200dp"
                        android:layout_height="30dp"
                        android:layout_marginBottom="24dp"
                        android:layout_marginEnd="8dp"
                        android:layout_marginLeft="8dp"
                        android:layout_marginRight="8dp"
                        android:layout_marginStart="8dp"
                        android:layout_marginTop="24dp"
                        android:background="@drawable/rounded_corner_button"
                        android:text="COMMENT"
                        android:textColor="@android:color/white"
                        app:layout_constraintBottom_toBottomOf="parent"
                        app:layout_constraintEnd_toEndOf="parent"
                        app:layout_constraintHorizontal_bias="0.51"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toBottomOf="@+id/text_isi_berita"
                        app:layout_constraintVertical_bias="0.0" />

                </android.support.constraint.ConstraintLayout>

            </LinearLayout>
        </android.support.v7.widget.CardView>
    </android.support.v4.widget.NestedScrollView>

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