在这篇文章中,我们将带领您了解android–折叠折叠工具栏时隐藏浮动按钮的全貌,包括安卓折叠工具栏的相关情况。同时,我们还将为您介绍有关android–CollapsingToolbarLayout
在这篇文章中,我们将带领您了解android – 折叠折叠工具栏时隐藏浮动按钮的全貌,包括安卓折叠工具栏的相关情况。同时,我们还将为您介绍有关android – CollapsingToolbarLayout并在滚动时隐藏工具栏、android – 为什么折叠工具栏标题在工具栏下?、android – 从具有折叠工具栏的选项卡切换时隐藏Bottomnavigationview、android – 使用textShadow折叠工具栏的知识,以帮助您更好地理解这个主题。
本文目录一览:- android – 折叠折叠工具栏时隐藏浮动按钮(安卓折叠工具栏)
- android – CollapsingToolbarLayout并在滚动时隐藏工具栏
- android – 为什么折叠工具栏标题在工具栏下?
- android – 从具有折叠工具栏的选项卡切换时隐藏Bottomnavigationview
- android – 使用textShadow折叠工具栏
android – 折叠折叠工具栏时隐藏浮动按钮(安卓折叠工具栏)
我的浮动按钮位于折叠工具栏的右下端.我希望折叠工具栏完全折叠时按钮消失.在其他情况下可见.
我的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" android:fitsSystemWindows="true" tools:context="fr.djey.secretapp.MainActivity"> <android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/AppTheme.AppBarOverlay" android:id="@+id/appBar"> <android.support.design.widget.CollapsingToolbarLayout android:id="@+id/collapse_toolbar" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_scrollFlags="scroll|exitUntilCollapsed" android:fitsSystemWindows="true" app:contentScrim="?attr/colorPrimary" app:expandedTitleMarginStart="48dp" app:expandedTitleMarginEnd="64dp"> <ImageView android:id="@+id/collapsing_image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:fitsSystemWindows="true" app:layout_collapseMode="parallax" android:scaleType="centerCrop" android:src="@drawable/welcome"/> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" app:popupTheme="@style/ThemeOverlay.AppCompat.Light" 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" xmlns:tools="http://schemas.android.com/tools" android:layout_gravity="fill_vertical" app:layout_behavior="@string/appbar_scrolling_view_behavior" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/content_frame"> </FrameLayout> </android.support.v4.widget.nestedScrollView> <android.support.design.widget.FloatingActionButton android:id="@+id/fab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="-60dp" android:layout_marginRight="16dp" android:src="@android:drawable/ic_dialog_email" app:layout_anchor="@id/collapse_toolbar" app:layout_anchorGravity="bottom|right|end" /> </android.support.design.widget.CoordinatorLayout>
有可能吗?怎么样?
解决方法
<android.support.design.widget.FloatingActionButton ... app:layout_anchor="@id/appBar" app:layout_anchorGravity="bottom|right|end" />
android – CollapsingToolbarLayout并在滚动时隐藏工具栏
在第一个状态下,当我们在最上面的页面上,还没有滚动时,我希望工具栏消耗如下所示(是的,我做到了):
在第二种状态下,当开始向下滚动时,图像和工具栏应该消失,如下所示(仅显示标签):
在最后一个状态,一旦我在列表中的某个点(但不是列表的顶部),我想开始向上滚动,一旦我开始滚动我想要的工具栏(而不是消耗的图像)到开始whowing,如下图所示(如果没有到达列表的顶部,图像将不会显示,只有工具栏):
我能够掌握第一个状态,但另外2个状态是有问题的,
一旦在CollapsingToolbarLayout中实现了一个工具栏,我可以在CollapsingToolbarLayout组件之外实现的可伸缩性并不清楚.
我无法使工具栏隐藏,如果我这样做,那么只有当我到达顶部才会显示.
无论如何,我当前的XML(如下所示)处于第一张图片被执行的状态,但一旦我开始向下滚动,工具栏保持在顶部,不要隐藏.注意:我必须告诉工具栏保持“pin”,因为如果我没有,那么工具栏中的信息就会消失,只有一个空的工具栏会显示(这是另一个帖子,但仍然有意思的是为什么会发生这种情况?) .
这是我当前的xml:
<android.support.design.widget.CoordinatorLayout android:id="@+id/benefit_coordinator_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.design.widget.AppBarLayout android:id="@+id/app_bar_material_layout" android:layout_width="match_parent" android:layout_height="wrap_content"> <android.support.design.widget.CollapsingToolbarLayout android:id="@+id/main.collapsing" android:layout_width="match_parent" android:layout_height="wrap_content" app:contentScrim="?attr/colorPrimary" app:layout_scrollFlags="scroll|exitUntilCollapsed" > <include android:id="@+id/toolbar_search_container" layout="@layout/search_Box" android:layout_height="192dp" android:layout_width="match_parent" app:layout_collapseMode="parallax" /> <include android:id="@+id/toolbar_benefit" layout="@layout/toolbar_main" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" app:contentScrim="?attr/colorPrimary" /> </android.support.design.widget.CollapsingToolbarLayout> <android.support.design.widget.TabLayout android:id="@+id/benefit_tab_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/primaryColor" app:tabIndicatorColor="@color/accentColor" app:tabSelectedTextColor="@android:color/white" app:tabTextColor="@android:color/black" app:tabIndicatorHeight="4dp" /> </android.support.design.widget.AppBarLayout> <android.support.v4.view.ViewPager android:id="@+id/benefit_pager" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" /> <include layout="@layout/floating_btn_benefits" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|right" android:layout_margin="16dp" /> </android.support.design.widget.CoordinatorLayou
解决方法
所以基本上解决方案是,使用以下属性更改组件CollapsingToolbarLayout:
app:layout_scrollFlags="scroll|enteralways|enteralwaysCollapsed"
并使用以下属性更改工具栏组件
android:minHeight="?attr/actionBarSize"
关于我的paralex效果图像(这是我的toolbar_search_container)我不应该添加任何layout_scrollFlags到它.
那为什么它工作?
要了解它,你需要知道什么是enteralwaysCollapsed,
enteralwaysCollapsed效果视图添加了minHeight属性.这意味着,具有minHeight的CollapsingToolbarLayout的每个孩子都将受到此属性的影响.
所以我的工具栏会受到影响.
enteralwaysCollapsed attrubute定义简单单词:
假设enteralways被声明,并指定了一个minHeight,你也可以指定enteralwaysCollapsed.当使用此设置时,您的视图将仅显示在此最小高度.只有当滚动到达顶部时,视图才能扩展到其完整高度…“
那么这不是我们想要的吗? (不要回答这个retorical问题))
另外还有一件事情是,paralexed组件(toolbar_search_container)取决于要扩展的收费栏,因为工具栏只有到达顶部时才会扩展,所以这一切都是非常棒的!
新的代码是:
<android.support.design.widget.CoordinatorLayout android:id="@+id/benefit_coordinator_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true"> <android.support.design.widget.AppBarLayout android:id="@+id/app_bar_material_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:fitsSystemWindows="true"> <android.support.design.widget.CollapsingToolbarLayout android:id="@+id/main.collapsing" android:layout_width="match_parent" android:layout_height="wrap_content" android:fitsSystemWindows="true" app:contentScrim="?attr/colorPrimary" app:layout_scrollFlags="scroll|enteralways|enteralwaysCollapsed" > <include android:id="@+id/toolbar_search_container" layout="@layout/search_Box" android:layout_height="192dp" android:layout_width="match_parent" app:layout_collapseMode="parallax" /> <include android:id="@+id/toolbar_benefit" layout="@layout/toolbar_main" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:minHeight="?attr/actionBarSize" app:contentScrim="?attr/colorPrimary" app:layout_collapseMode="pin" android:fitsSystemWindows="true" /> </android.support.design.widget.CollapsingToolbarLayout> <android.support.design.widget.TabLayout android:id="@+id/benefit_tab_layout" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/primaryColor" app:tabIndicatorColor="@color/accentColor" app:tabSelectedTextColor="@android:color/white" app:tabTextColor="@android:color/black" app:tabIndicatorHeight="4dp" /> </android.support.design.widget.AppBarLayout> <android.support.v4.view.ViewPager android:id="@+id/benefit_pager" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" /> <include layout="@layout/floating_btn_benefits" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|right" android:layout_margin="16dp" /> </android.support.design.widget.CoordinatorLayout>
android – 为什么折叠工具栏标题在工具栏下?
<android.support.design.widget.AppBarLayout android:id="@+id/movie_detail_appbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:fitsSystemWindows="true" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> <android.support.design.widget.CollapsingToolbarLayout android:id="@+id/movie_detail_collapsing_toolbar" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" app:contentScrim="?attr/colorPrimary" app:layout_scrollFlags="scroll|exitUntilCollapsed"> <ImageView android:id="@+id/movie_detail_header_image" android:layout_width="match_parent" android:layout_height="250dp" android:fitsSystemWindows="true" android:scaleType="centerCrop" app:layout_collapseMode="parallax" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" android:background="#99000026" android:fitsSystemWindows="true" android:orientation="horizontal" android:weightSum="10"> <ImageView android:id="@+id/detail_movie_poster" android:layout_width="90dp" android:layout_height="110dp" android:layout_margin="10dp" android:layout_weight="1" android:scaleType="centerInside" android:src="@drawable/no_movie_image" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="9" android:orientation="vertical"> <TextView android:id="@+id/detail_movie_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:ellipsize="marquee" android:focusable="true" android:focusableInTouchMode="true" android:marqueeRepeatLimit="marquee_forever" android:singleLine="true" android:textColor="@android:color/white" android:textSize="26sp" /> <TextView android:id="@+id/detail_movie_original_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="16sp" /> <TextView android:id="@+id/detail_movie_genres" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:singleLine="true" android:textSize="14sp" /> </LinearLayout> </LinearLayout> <android.support.v7.widget.Toolbar android:id="@+id/movie_detail_toolbar" android:layout_width="match_parent" android:layout_height="65dp" android:fitsSystemWindows="true" app:layout_collapseMode="pin" app:popupTheme="@style/ThemeOverlay.AppCompat.ActionBar" /> </android.support.design.widget.CollapsingToolbarLayout> <android.support.design.widget.TabLayout android:id="@+id/movie_detail_tabs" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:layout_gravity="bottom" app:tabIndicatorColor="@android:color/white" app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/> </android.support.design.widget.AppBarLayout> <android.support.v4.view.ViewPager android:id="@+id/movie_detail_viewpager" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" />
但设计就是这样发生的.折叠工具栏标题位于工具栏下方.
我还使用这个initCollapsingToolbar()来动态折叠工具栏标题:
private void initCollapsingToolbar() { final CollapsingToolbarLayout collapsingToolbar = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar); collapsingToolbar.setTitle(" "); AppBarLayout appBarLayout = (AppBarLayout) findViewById(R.id.appbar); appBarLayout.setExpanded(true); // hiding & showing the title when toolbar expanded & collapsed appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() { boolean isShow = false; int scrollRange = -1; @Override public void onOffsetChanged(AppBarLayout appBarLayout,int verticalOffset) { if (scrollRange == -1) { scrollRange = appBarLayout.getTotalScrollRange(); } if (scrollRange + verticalOffset == 0) { Results results = intent.getParcelableExtra(Constant.PARCELABLE_MOVIE_TITLE); collapsingToolbarLayout.setTitle(results.getTitle()); isShow = true; } else if (isShow) { collapsingToolbar.setTitle(" "); isShow = false; } } }); }
解决方法
首先,我将应用程序:titleEnabled =“false”添加到CollapsingToolbar
<android.support.design.widget.CollapsingToolbarLayout android:id="@+id/movie_detail_collapsing_toolbar" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" app:contentScrim="?attr/colorPrimary" app:layout_scrollFlags="scroll|exitUntilCollapsed" app:titleEnabled="false">
然后更改collapsingToolbar.setTitle(); to getSupportActionBar().setTitle();
在initCollapsingToolbar()中
最后将app:titleMarginTop =“14dp”添加到工具栏
也许这个解决方案很糟糕,但这就是我所能做的.
android – 从具有折叠工具栏的选项卡切换时隐藏Bottomnavigationview
有没有办法阻止这种情况?
屏幕包含包含折叠工具栏的片段.
具有普通片段的另一个选项卡的屏幕.
片段类包含用于折叠工具栏的代码
public class Profile extends Fragment { public Profile() { } @Override public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) { //setHasOptionsMenu(true); View rootView = inflater.inflate(R.layout.fragment_profile,container,false); Toolbar toolbar = rootView.findViewById(R.id.toolbar); AppCompatActivity activity = (AppCompatActivity) getActivity(); activity.setSupportActionBar(toolbar); activity.getSupportActionBar().setdisplayHomeAsUpEnabled(true); CollapsingToolbarLayout collapsingToolbar = rootView.findViewById(R.id.collapsing_toolbar); collapsingToolbar.setTitle("test"); return rootView; }
对应的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:fitsSystemWindows="true"> <android.support.v7.widget.RecyclerView android:id="@+id/recycler" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" /> <android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="180dp" android:theme="@style/ThemeOverlay.AppCompat.Dark"> <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:layout_scrollFlags="scroll|exitUntilCollapsed"> <ImageView android:id="@+id/header" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/ani_dog_one" android:contentDescription="whut" 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" /> </android.support.design.widget.CollapsingToolbarLayout> </android.support.design.widget.AppBarLayout> </android.support.design.widget.CoordinatorLayout>
普通片段类
public class Review extends Fragment { public Review() { } @Override public View onCreateView(LayoutInflater inflater,Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_review,false); //setHasOptionsMenu(true); return rootView; } @Override public void onCreateOptionsMenu(Menu menu,MenuInflater inflater) { inflater.inflate(R.menu.search_bar_menu,menu); super.onCreateOptionsMenu(menu,inflater); }
普通片段类的XML只包含一个简单的FrameLayout
主要活动
public class MainActivity extends AppCompatActivity { private Intent intent; private android.support.v4.app.FragmentManager manager; private android.support.v4.app.FragmentTransaction transaction; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); manager = getSupportFragmentManager(); transaction = manager.beginTransaction(); discover discoverFragment = new discover(); transaction.replace(R.id.container,discoverFragment,discoverFragment.getTag()).commit(); setupBottomNavigationView(); } private void setupBottomNavigationView() { BottomNavigationViewEx bottomNavigationViewEx = (BottomNavigationViewEx) findViewById(R.id.bottom_navigation); BottomNavigationViewHelper.setupBottomNavigationView(bottomNavigationViewEx); BottomNavigationViewHelper.enableNavigation(this,bottomNavigationViewEx); } public void goToOptions(MenuItem menu) { intent = new Intent(this,Options.class); overridePendingTransition(R.anim.left_in,R.anim.right_out); startActivity(intent); }
}
用于MainActivity的XML
<LinearLayout 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:orientation="vertical" tools:context="com.example.android.project_qwer.MainActivity"> <!-- main fragments goes here --> <FrameLayout android:id="@+id/container" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"> </FrameLayout> <!-- bottom navigation view --> <com.ittianyu.bottomnavigationviewex.BottomNavigationViewEx android:id="@+id/bottom_navigation" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" android:background="?android:attr/windowBackground" app:menu="@menu/bottom_navigation_menu" > </com.ittianyu.bottomnavigationviewex.BottomNavigationViewEx> </LinearLayout>
解决方法
android:fitsSystemWindows=”true”
在您的CollapsingToolbarLayout上.
android – 使用textShadow折叠工具栏
collapsingToolbar.setExpandedTitleTextAppearance(R.style.toolbar_text);
用:
<style name="toolbar_text"> <item name="android:textColor">@color/white</item> <item name="android:shadowColor">@color/black</item> <item name="android:shadowDx">2</item> <item name="android:shadowDy">2</item> <item name="android:shadowRadius">4</item> </style>
我可以更改textColor,它可以工作,但阴影不起作用.我为阴影尝试了很多不同的值.
是否可以为折叠文本投射阴影?因为在浅色图像上,标题有时难以阅读.
解决方法
关于android – 折叠折叠工具栏时隐藏浮动按钮和安卓折叠工具栏的介绍现已完结,谢谢您的耐心阅读,如果想了解更多关于android – CollapsingToolbarLayout并在滚动时隐藏工具栏、android – 为什么折叠工具栏标题在工具栏下?、android – 从具有折叠工具栏的选项卡切换时隐藏Bottomnavigationview、android – 使用textShadow折叠工具栏的相关知识,请在本站寻找。
本文标签: