可折叠工具栏:设置多少工具栏应的onCreate折叠 [英] Collapsible Toolbar: Set how much the toolbar should be collapsed in onCreate
问题描述
我要创建一个ListView及其相应的DetailView应用。 我的ListView具有如果点击将用户带到所述DetailViewActivity项目
I'm creating a ListView and its corresponding DetailView application. My ListView has items which if clicked take the user to the DetailViewActivity.
在DetailViewActivity,我已经实现了一个可折叠工具栏。 现在,每次DetailViewActivity被打开的时间,不同的图像(具有不同尺寸)被设置到可折叠工具条中的ImageView的。
On the DetailViewActivity, I've implemented a Collapsible Toolbar. Now, every time the DetailViewActivity is opened, a different image (with different dimensions) is set onto the ImageView within the Collapsible Toolbar.
我想,该工具栏应该是开放高达一定的高度默认(比如说256dp),但如果像高为大于时,用户应能拉下观看的图像的其余部分。 (酷似WhatsApp的)
I want that the Toolbar should be open upto a certain height by default (say 256dp), but if the image height is greater than that, the user should be able to pull down to view the rest of the image. (exactly like Whatsapp)
我已经成功地编程设定每个我打开活动时工具栏的高度,但问题是,工具栏总是完全展开。因此,如果该图像是越大,Tollbar默认是非常大的。我希望它被倒塌的图像的高度,不论以256dp。
I've managed to programatically set the height of the Toolbar each time I open the activity, but the problem is, the Toolbar is always fully expanded. So if the image is larger, the Tollbar by default is very big. I want it to be collapsed to 256dp irrespective of the height of the image.
在code我的布局是:
The code for my layout is:
<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>
和我的活动,我已经找到了高度并将其设置为工具栏是这样的:
And in my Activity, I've found the height and set it to the Toolbar like this:
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);
我附上截图,使我的观点更清晰。
I'm attaching screenshots to make my point clearer.
This is exactly how tall I want my Toolbar to be everytime the activity gets launched:
这是我从我的code得到:
And this is what I get from my code:
现在,我可以硬c中的高度$ C $到256dp在code,但随后用户将无法向下滚动才能看到图像的其余部分。请建议。
Now, I could hardcode the height to 256dp in code, but then the user will not be able to scroll down to see the rest of the image. Please suggest.
感谢您的答复。 任何反应可以让我开始
Thank you for your response. Any response could get me started
推荐答案
想出解决方案最终。我preSET工具栏的高度512dp。然后,一旦我收到了新的形象,我通过我的位图作为参数传递给我的expandToolbar()方法。 我从 http://stackoverflow.com/a/30747281/3286614 ,感谢团陈英雄想法P>
Figured out the solution finally. I preset the height of the toolbar to 512dp. Then once I received the new image, I passed my Bitmap as a parameter to my expandToolbar() method. I got the idea from http://stackoverflow.com/a/30747281/3286614, thanks to Tuấn Trần Anh
public static void expandToolbar(Bitmap bmp) {
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) appBar.getLayoutParams();
AppBarLayout.Behavior behavior = new AppBarLayout.Behavior();
behavior.setTopAndBottomOffset(0);
behavior.onNestedPreScroll(rootLayout, appBar, null, 0, bmp.getHeight() - 512, new int[2]);
params.setBehavior(behavior);
DetailActivity.appBar.setLayoutParams(params);
}
希望这可以帮助别人。
Hope this helps somebody.
这篇关于可折叠工具栏:设置多少工具栏应的onCreate折叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!