[英]Stop fragments going over ActionBar in ViewPager
As you can see in the image below, the 'Updates fragment' TextView is being written above the ActionBar. 如您在下图中所看到的,“更新片段” TextView正在ActionBar上方编写。 I am trying to make it so that this text sits below the TabBar, where you'd expect it. 我正在尝试使它位于您期望的TabBar下方。
The main activity layout is a CoordinatorLayout, containing the typical AppBarLayout, and then a ViewPager. 主要活动布局是一个CoordinatorLayout,其中包含典型的AppBarLayout,然后是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? ViewPager会填满整个屏幕(据我所知必须这样做),但是如何更改布局,使片段位于选项卡下并从此处填满屏幕的其余部分?
activity_main.xml: 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: 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: 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: 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>
Since you are usigng CoOrdinatorLayout:- 由于您使用的是CoOrdinatorLayout:-
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
I think you correctly identified part of the problem in your comments - that CoordinatorLayout
inherits from FrameLayout
, and as such your current layout file simply tells the ViewPager
to fill the entire screen and go on top of other content (as seen in your photo). 我想你正确识别问题的一部分,在您的意见-这CoordinatorLayout
从继承FrameLayout
,因此当前的布局文件简单地告诉ViewPager
填满整个屏幕,去的其他内容的顶部(如在你的照片看到的)。
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
). 要解决该问题,并继续使用CoordinatorLayout
,您需要为您的内容分配一个行为,这仅在具有嵌套滚动的布局(RecyclerView
和NestedScrollView
)上才是可能的。You could therefore try wrapping the layout file for each of your Fragments inside the
ViewPager
inside aNestedScrollView
. 因此,您可以尝试在ViewPager
的NestedScrollView
包装每个片段的布局文件。
Additionally, you can try adding app:layout_behavior="@string/appbar_scrolling_view_behavior"
to your ViewPager
. 此外,您可以尝试将app:layout_behavior="@string/appbar_scrolling_view_behavior"
到ViewPager
。
Try adding layout_below AppBarLayout to ViewPager: 尝试将layout_below AppBarLayout添加到ViewPager中:
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="--some id of AppBarLayout--" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.