我正在创建一个ListView
及其相应的DetailView
应用程序。 我的ListView
有项目,如果点击将用户带到DetailViewActivity
。可折叠工具栏:设置在创建多少工具栏应该折叠
在DetailViewActivity
上,我实现了一个可折叠工具栏。 现在,每次打开DetailViewActivity
时,都会在可折叠工具栏中的ImageView
上设置不同的图像(具有不同的尺寸)。
我希望Toolbar
默认应该打开到一定的高度(比如256dp),但如果图片高度大于此值,用户应该能够拉下来查看图片的其余部分。 (完全像Whatsapp)
我已经设法以编程方式设置每次打开活动时Toolbar
的高度,但问题是,Toolbar
始终完全展开。因此,如果图像较大,默认情况下工具栏非常大。无论图像的高度如何,我都希望它可以折叠到256dp。
我布局的代码是:
<RelativeLayout
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.support.design.widget.CoordinatorLayout
android:id="@+id/rootLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="@dimen/app_bar_height"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsingToolbarLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginStart="@dimen/expanded_toolbar_title_margin_start"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="@drawable/background_navdrawer"
app:layout_collapseMode="parallax"
app:layout_collapseParallaxMultiplier="0.7"/>
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="130dp"
android:background="@drawable/gradient_header_background"
app:layout_collapseMode="parallax"
app:layout_collapseParallaxMultiplier="0.1"/>
<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:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:id="@+id/detail_nested_scroll_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<FrameLayout
android:id="@+id/detail_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:ignore="MergeRootFrame"/>
</android.support.v4.widget.NestedScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="12dp"
android:orientation="vertical"
app:layout_anchor="@+id/appbar"
app:layout_anchorGravity="bottom">
<View
android:id="@+id/toolbar_shadow_transparent"
android:layout_width="match_parent"
android:layout_height="@dimen/toolbar_elevation"
android:background="@color/transparent"/>
<View
android:id="@+id/toolbar_shadow"
android:layout_width="match_parent"
android:layout_height="@dimen/toolbar_elevation"
android:background="@drawable/dropshadow"/>
</LinearLayout>
<com.github.clans.fab.FloatingActionButton
android:id="@+id/action_edit"
style="@style/MenuButtonsStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:src="@drawable/ic_edit"
app:layout_anchor="@+id/appbar"
app:layout_anchorGravity="bottom|right|end"/>
</android.support.design.widget.CoordinatorLayout>
<ImageView
android:id="@+id/detail_back_arrow_land"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"/>
<TextView
android:id="@+id/course_name_textview"
android:layout_width="0dp"
android:layout_height="0dp"
android:visibility="gone"/>
</RelativeLayout>
而且在我的活动,我发现高度,并将其设置为Toolbar
这样的:
appBar = (AppBarLayout) findViewById(R.id.appbar);
CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams) appBar.getLayoutParams();
lp.height = my_bitmap.getHeight();
DetailActivity.appBar.setLayoutParams(lp);
DetailActivity.mImageView.setImageBitmap(my_bitmap);
我附上截图让我的p oint更清晰。
这是我从我的代码获得:
现在,我可以将代码中的高度硬编码为256dp,但用户将无法向下滚动查看图像的其余部分。请建议。
感谢您的回复。 任何响应可以让我开始
漂亮的键盘) –