gpt4 book ai didi

android - 避免 ViewPager 在工具栏下

转载 作者:搜寻专家 更新时间:2023-11-01 07:50:22 25 4
gpt4 key购买 nike

基于 this answer 我试图为我的用例做一个解决方案。我没有使用折叠工具栏,而是使用了一个普通的工具栏,但带有 TabLayout。但是,对于 TabLayout,我需要使用 ViewPager,但我不知道将它放在哪里以避免 FrameLayout 出现问题。 ViewPager 在我的 Host Fragment 中,当它的布局替换了 FrameLayout 时,内容出于某种原因位于 Toolbar 和 TabLayout 下方...

我试着把它放在那条线下面

<include layout="@layout/content_main" />

app_bar_main.xml 中,但它弄乱了我的 UI。

真正的问题是,如果我的 fragment 需要一个 FrameLayout 并且我的 TabLayout 需要一个 ViewPager,我该如何实现解决方案?

您可以看到我稍微移动了 View 以便隐藏工具栏。这样就可以看到 ViewPager 甚至在工具栏下方(当它可见时)占用空间

Screenshot

当前尝试 - appbar_content(Toolbar 只是单独布局中的 Toolbar):

<?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"
tools:context=".MainActivity">

<android.support.design.widget.AppBarLayout
android:id="@+id/appBarLayout"

android:layout_width="match_parent"
android:layout_height="wrap_content">

<include layout="@layout/toolbar" />

<android.support.design.widget.TabLayout
android:visibility="visible"
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/teal"
app:tabGravity="fill"
app:tabIndicatorColor="@color/purple800"
app:tabIndicatorHeight="4dp"
app:itemIconTint="@color/tabicons"
app:tabMode="fixed"
app:tabSelectedTextColor="@color/tealfifty"
app:tabTextAppearance="@style/TabCapsFalse"
app:tabTextColor="@color/iron" />
</android.support.design.widget.AppBarLayout>


<include layout="@layout/content_main" />



</android.support.design.widget.CoordinatorLayout>

content_main(删除了 NestedScrollView,因为它搞砸了一切,将我的整个 fragment View 放在屏幕中间的小框架(可能是 50x50 大小)中)

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout android:id="@+id/content_main_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android" />

我的DrawerLayout:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<include
layout="@layout/appbar_content"
android:layout_width="match_parent"
android:layout_height="match_parent" />

<android.support.design.widget.NavigationView
android:fitsSystemWindows="true"
android:id="@+id/nvView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@color/tealfifty"
app:itemBackground="@drawable/drawer_item"
app:itemTextColor="@color/drawer_item"
app:itemIconTint="@color/drawer_item"
app:headerLayout="@layout/drawer_header"
app:menu="@menu/navigation_view_items" />
</android.support.v4.widget.DrawerLayout>

最佳答案

经过一番努力,我发现在我的 FrameLayout 中放置一个 NestedScrollView 并为我的 FrameLayout 分配一个 layout_behaviour 可以让 Scroll View 继承它,现在一切都很顺利!

这是我的 DrawerLayout:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">

<include
layout="@layout/appbar_content"
android:layout_width="match_parent"
android:layout_height="match_parent" />

<android.support.design.widget.NavigationView
android:fitsSystemWindows="true"
android:id="@+id/nvView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@color/tealfifty"
app:itemBackground="@drawable/drawer_item"
app:itemTextColor="@color/drawer_item"
app:itemIconTint="@color/drawer_item"
app:headerLayout="@layout/drawer_header"
app:menu="@menu/navigation_view_items" />
</android.support.v4.widget.DrawerLayout>

appbar_content.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"
tools:context=".MainActivity">

<android.support.design.widget.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<include layout="@layout/toolbar" />

<android.support.design.widget.TabLayout
android:visibility="visible"
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/teal"
app:tabGravity="fill"
app:tabIndicatorColor="@color/purple800"
app:tabIndicatorHeight="4dp"
app:itemIconTint="@color/tabicons"
app:tabMode="fixed"
app:tabSelectedTextColor="@color/tealfifty"
app:tabTextAppearance="@style/TabCapsFalse"
app:tabTextColor="@color/iron" />
</android.support.design.widget.AppBarLayout>



<FrameLayout android:id="@+id/content_main_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:showIn="@layout/appbar_content"
xmlns:tools="http://schemas.android.com/tools">
<android.support.v4.widget.NestedScrollView
android:id="@+id/scroll_content"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>


<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:visibility="invisible"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:clickable="true"
android:src="@drawable/addico"
app:layout_anchor="@id/content_main_frame"
app:layout_anchorGravity="bottom|end" />

关于android - 避免 ViewPager 在工具栏下,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35590799/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com