Java 阻止片段在ViewPager中通过操作栏
如下图所示,“更新片段”文本视图写在操作栏上方。我正试图使它,使这篇文章坐在下面的选项卡栏,你会期待它 主活动布局是一个CoordinatorLayout,包含典型的AppBarLayout,然后是一个ViewPager ViewPager正在填充整个屏幕(我知道它必须如此),但是我如何才能更改布局,使片段位于选项卡下,并从那里填充屏幕的其余部分 activity_main.xml:Java 阻止片段在ViewPager中通过操作栏,java,android,android-fragments,Java,Android,Android Fragments,如下图所示,“更新片段”文本视图写在操作栏上方。我正试图使它,使这篇文章坐在下面的选项卡栏,你会期待它 主活动布局是一个CoordinatorLayout,包含典型的AppBarLayout,然后是一个ViewPager ViewPager正在填充整个屏幕(我知道它必须如此),但是我如何才能更改布局,使片段位于选项卡下,并从那里填充屏幕的其余部分 activity_main.xml: <?xml version="1.0" encoding="utf-8"?> <androi
<?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>
@颜色/原色
@颜色/原色暗
@颜色/颜色重音
错误的
符合事实的
尝试将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--" />
尝试将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--" />
由于您使用的是坐标布局:-
<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"/>
由于您使用的是坐标布局:-
<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"/>
我认为您在评论中正确地指出了问题的一部分-
协调布局
继承自框架布局
,因此您当前的布局文件只需告诉查看页面
填充整个屏幕并覆盖其他内容(如照片所示)
编辑:我意识到,在下面用引号写下这条建议与您的问题不相关。仅当您想要创建折叠工具栏行为时,这一点才很重要
要解决此问题并继续使用CoordinatorLayout
,您需要
为您的内容指定一种行为,这仅在版面上可行
使用嵌套滚动(RecyclerView
和NestedScrollView
)
因此,您可以尝试为每个应用程序包装布局文件
NestedScrollView
中的ViewPager
中的片段
此外,您可以尝试将
app:layout\u behavior=“@string/appbar\u scrolling\u view\u behavior”
添加到您的ViewPager
我认为您在注释中正确地识别了部分问题,即CoordinatorLayout
继承自FrameLayout
,因此,您当前的布局文件只需告诉ViewPager
填充整个屏幕并覆盖其他内容(如照片所示)
编辑:我意识到,在下面用引号写下这条建议与您的问题不相关。仅当您想要创建折叠工具栏行为时,这一点才很重要
要解决此问题并继续使用CoordinatorLayout
,您需要
为您的内容指定一种行为,这仅在版面上可行
使用嵌套滚动(RecyclerView
和NestedScrollView
)
因此,您可以尝试为每个应用程序包装布局文件
NestedScrollView
中的ViewPager
中的片段
此外,您可以尝试将
app:layout\u behavior=“@string/appbar\u scrolling\u view\u behavior”
添加到您的ViewPager
这没有什么区别,不幸的是,将ViewPager的布局高度更改为android:layout\u height=“fill\u parent”好的,我搞错了。协调器布局是框架布局,这意味着它将绘制在另一个框架的顶部。尝试将android:fitsystemwindows=“true”添加到ViewPager(您可以还原其他更改)仍然一样令人烦恼,不同的布局会比协调器布局更好吗?现在这很有趣。好的,试试最后一件事:将其添加到ViewPager应用程序:layout\u behavior=“@string/appbar\u scrolling\u view\u behavior”这没什么区别不幸的是,将ViewPager的布局高度更改为android:layout\u height=“fill\u parent”好的,我搞错了。协调器布局是框架布局,这意味着它将绘制在另一个框架的顶部。尝试将android:fitsystemwindows=“true”添加到ViewPager(您可以还原其他更改)仍然一样令人烦恼,不同的布局会比协调器布局更好吗?现在这很有趣。好的,尝试最后一件事:将其添加到ViewPager应用程序:布局_行为=“@string/appbar\u scrolling\u view_behavior”这是因为您在AppBarLayout
上激活了AppBarOverlay
主题?你能显示该主题的代码吗?@Bhargav这只是我选择的默认Android主题,这是最常见的主题?我会选择它,看看它是否修复it@Bhargav删除主题并不能解决问题,TextView仍保留在左上角只需在此处发布你的styles.xml以及你的清单我想看看你在应用程序中应用了什么主题以及这个特定的活动这是因为你在AppBarOverlayout
上激活了AppBarLayout
主题?你能显示该主题的代码吗?@Bhargav这只是我选择的默认Android主题,这是最常见的主题?我会选择它,看看它是否修复it@Bhargav删除主题并不能解决问题,TextView仍保留在左上角。只需在此处发布您的styles.xml以及您的清单,我想看看您应用于应用程序和此特定活动的主题是什么!谢谢你,这很有效!你能解释一下为什么添加这个布局行为可以解决这个问题,以便我将来参考吗?所以协调布局是扩展的FrameLayout,这意味着它是在彼此之上绘制东西。但是它有这样的布局行为,你可以做很多滚动的事情(它还告诉协调人把它一个接一个地画出来):是的!谢谢你,这很有效!你能解释一下为什么添加布局行为解决了这个问题,这样我就知道了