正如您在下图中看到的,“更新 fragment ”TextView 被写在 ActionBar 上方。我正在努力使此文本位于 TabBar 的下方,您期望它的位置。
主要的 Activity 布局是一个 CoordinatorLayout,包含典型的 AppBarLayout,然后是一个 ViewPager。
ViewPager 正在填满整个屏幕(据我所知,它必须填满),但我如何更改布局,以便 fragment 位于选项卡下方并从那里填满屏幕的剩余部分?
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 更新.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>
样式.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>
最佳答案
我认为您在评论中正确地指出了部分问题 - CoordinatorLayout
继承自 FrameLayout
,因此您当前的布局文件只是告诉 ViewPager
以填满整个屏幕并位于其他内容之上(如您的照片所示)。
编辑:我意识到自从在下面用引号括起这条建议后,它与您目前的问题无关。仅当您想创建折叠工具栏行为时才重要。
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
.
此外,您可以尝试将 app:layout_behavior="@string/appbar_scrolling_view_behavior"
添加到您的 ViewPager
。
关于java - 停止 fragment 在 ViewPager 中越过 ActionBar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33982527/