停止碎片去在动作条在ViewPager [英] Stop fragments going over ActionBar in ViewPager
问题描述
正如你可以在下面的图片中看到,在更新片段的TextView被在ActionBar上面写的。我试图使这段文字坐在TabBar,所以在你期望它下面做出来。
主要活动布局是一个CoordinatorLayout,包含典型AppBarLayout,然后ViewPager。
该ViewPager正在填充整个屏幕(这我理解它有),但我怎样才能改变我的布局,使该片段位于标签之下,填满屏幕的剩余部分从那里?
activity_main.xml中:
<?XML版本=1.0编码=UTF-8&GT?;
< android.support.design.widget.CoordinatorLayout的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:程序=http://schemas.android.com/apk/res-auto
的xmlns:工具=http://schemas.android.com/tools
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:fitsSystemWindows =真
工具:上下文=MainActivity。> < android.support.design.widget.AppBarLayout
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
机器人:主题=@风格/ AppTheme.AppBarOverlay> < android.support.v7.widget.Toolbar
机器人:ID =@ + ID /工具栏
机器人:layout_width =match_parent
机器人:layout_height =?ATTR / actionBarSize
机器人:ATTR / colorPrimary背景=
应用:popupTheme =@风格/ AppTheme.PopupOverlay/> < android.support.design.widget.TabLayout
机器人:ID =@ + ID /标签
机器人:layout_width =match_parent
机器人:layout_height =WRAP_CONTENT
应用:tabMode =固定
应用:tabGravity =补/>
< /android.support.design.widget.AppBarLayout> < android.support.v4.view.ViewPager
机器人:ID =@ + ID / viewpager
机器人:layout_width =match_parent
机器人:layout_height =match_parent/>< /android.support.design.widget.CoordinatorLayout>
fragment_updates.xml:
<的RelativeLayout的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:工具=http://schemas.android.com/tools
机器人:layout_width =match_parent
机器人:layout_height =match_parent
工具:上下文=UpdatesFragment。> <的TextView
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:文字=更新片段/>
< / RelativeLayout的>
AndroidManifest.xml中:
<?XML版本=1.0编码=UTF-8&GT?;
<清单的xmlns:机器人=http://schemas.android.com/apk/res/android
包=uk.joel.myapp> <应用
机器人:allowBackup =真
机器人:图标=@的mipmap / ic_launcher
机器人:标签=堆栈
机器人:supportsRtl =真
机器人:主题=@风格/ AppTheme> <活动
机器人:名字=。MainActivity
机器人:标签=@字符串/ APP_NAME
机器人:screenOrientation =画像
机器人:主题=@风格/ AppTheme.NoActionBar>
&所述;意图滤光器>
<作用机器人:名字=android.intent.action.MAIN/> <类机器人:名字=android.intent.category.LAUNCHER/>
&所述; /意图滤光器>
< /活性GT; <元数据机器人:名字=com.google.android.geo.API_KEY机器人:值=键/>
< /用途>< /清单>
Styles.xml:
<资源> <! - Base应用程序的主题。 - >
<样式名称=AppTheme父=Theme.AppCompat.Light.DarkActionBar>
<! - 在这里自定义的主题。 - >
<项目名称=colorPrimary> @彩色/ colorPrimary< /项目>
<项目名称=colorPrimaryDark> @彩色/ colorPrimaryDark< /项目>
<项目名称=colorAccent> @彩色/ colorAccent< /项目>
< /风格> <样式名称=AppTheme.NoActionBar>
<项目名称=windowActionBar>假LT; /项目>
<项目名称=windowNoTitle>真< /项目>
< /风格> <样式名称=AppTheme.AppBarOverlay父=ThemeOverlay.AppCompat.Dark.ActionBar/> <样式名称=AppTheme.PopupOverlay父=ThemeOverlay.AppCompat.Light/>< /资源>
我觉得你在你的意见正确识别问题的一部分 - CoordinatorLayout
继承自的FrameLayout
,因此当前的布局文件简单地告诉 ViewPager
来填满整个屏幕,并继续其他内容的顶部(如在你的照片所示)。
编辑:我已经意识到,因为下面的这个发帖咨询,报价,是不相关的问题,因为它的立场。如果你想创建工具栏倒塌行为是唯一重要的。
要解决这个问题,并继续使用
CoordinatorLayout
,你需要
分配行为的内容,这是唯一可能的布局
嵌套滚动(<为code> RecyclerView 和NestedScrollView
)。
您可以尝试因此包布局文件为您的每个
里面的碎片的ViewPager
在NestedScrollView
。
块引用>此外,你可以尝试添加
应用:layout_behavior =@字符串/ appbar_scrolling_view_behavior
你的ViewPager
As you can see in the image below, the 'Updates fragment' TextView is being written above the ActionBar. I am trying to make it so that this text sits below the TabBar, where you'd expect it.
The main activity layout is a CoordinatorLayout, containing the typical AppBarLayout, and then a ViewPager.
The ViewPager is filling the whole screen (which I understand it has to), but how can I change my layout so that the fragment sits under the tabs and fills the remaining part of the screen from there?
activity_main.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=".MainActivity"> <android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/AppTheme.AppBarOverlay"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" /> <android.support.design.widget.TabLayout android:id="@+id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content" app:tabMode="fixed" app:tabGravity="fill" /> </android.support.design.widget.AppBarLayout> <android.support.v4.view.ViewPager android:id="@+id/viewpager" android:layout_width="match_parent" android:layout_height="match_parent" /> </android.support.design.widget.CoordinatorLayout>
fragment_updates.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".UpdatesFragment"> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:text="Updates fragment" /> </RelativeLayout >
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="uk.joel.myapp"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="Stack" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity" android:label="@string/app_name" android:screenOrientation="portrait" android:theme="@style/AppTheme.NoActionBar"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <meta-data android:name="com.google.android.geo.API_KEY" android:value="key" /> </application> </manifest>
Styles.xml:
<resources> <!-- 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> <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" /> <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" /> </resources>
解决方案I think you correctly identified part of the problem in your comments - that
CoordinatorLayout
inherits fromFrameLayout
, and as such your current layout file simply tells theViewPager
to fill the entire screen and go on top of other content (as seen in your photo).Edit: I've realised since posting this advice below, in quotes, isn't relevant to your question as it stands. It is only important if you want to create collapsing toolbar behaviour.
To resolve it, and keep using
CoordinatorLayout
, you would need to assign a behaviour to your content, which is only possible on a layout with nested scrolling (RecyclerView
andNestedScrollView
).You could therefore try wrapping the layout file for each of your Fragments inside the
ViewPager
inside aNestedScrollView
.Additionally, you can try adding
app:layout_behavior="@string/appbar_scrolling_view_behavior"
to yourViewPager
.这篇关于停止碎片去在动作条在ViewPager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!