gpt4 book ai didi

java - NestedScrollView 重叠工具栏(它们位于 fragment 布局内部)

转载 作者:行者123 更新时间:2023-12-02 02:51:35 28 4
gpt4 key购买 nike

我正在尝试添加 CoordinatorLayout,包括 Fragment 布局内的 Toolbar。但问题是 NestedScrollView 的内容与 Toolbar 重叠,如下图所示:

enter image description here

I tried to add a RelativeLayout parent and use android:layout_below="@+id/coordinatorlayout" for the NestedScrollView,but this make the content of NestedScrollView not showing up.

fragment 布局代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.design.widget.CoordinatorLayout
android:id="@+id/coordinatorlayout"
android:layout_height="match_parent"
android:layout_width="match_parent">

<android.support.v7.widget.Toolbar
android:background="@color/colorPrimary"
android:id="@+id/toolbar2"
android:layout_height="?attr/actionBarSize"
android:layout_width="match_parent"
app:contentInsetStart="0dp"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:titleTextColor="#FFFFFF">

<RelativeLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content">

<android.support.v7.widget.CardView
android:layout_height="40dp"
android:layout_width="match_parent">

<RelativeLayout
android:layout_height="match_parent"
android:layout_width="match_parent">

<ImageView
android:id="@+id/search_icon"
android:layout_alignParentRight="true"
android:layout_height="24dp"
android:layout_width="24dp"
android:src="@drawable/ic_search_black_24dp" />

<EditText
android:hint="Search"
android:id="@+id/searchEditText"
android:layout_height="wrap_content"
android:layout_width="match_parent"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.CoordinatorLayout>

<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_marginRight="50dp"
android:layout_marginLeft="50dp"
android:text="hello"/>

</RelativeLayout>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</RelativeLayout>

最佳答案

1.使用CoordinatorLayout作为root布局。

2.CoordinatorLayout内添加AppBarLayoutNestedScrollView

3.Toolbar放入AppBarLayout中。

这是工作代码:

<?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"
android:layout_width="match_parent"
android:layout_height="match_parent">

<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">

<android.support.v7.widget.Toolbar
android:background="@color/colorPrimary"
android:id="@+id/toolbar2"
android:layout_height="?attr/actionBarSize"
android:layout_width="match_parent"
app:contentInsetStart="0dp"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:titleTextColor="#FFFFFF">

<RelativeLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content">

<android.support.v7.widget.CardView
android:layout_height="40dp"
android:layout_width="match_parent">

<RelativeLayout
android:layout_height="match_parent"
android:layout_width="match_parent">

<ImageView
android:id="@+id/search_icon"
android:layout_alignParentRight="true"
android:layout_height="24dp"
android:layout_width="24dp"
android:src="@drawable/ic_search_black_24dp" />

<EditText
android:hint="Search"
android:id="@+id/searchEditText"
android:layout_height="wrap_content"
android:layout_width="match_parent"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>

<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_marginRight="50dp"
android:layout_marginLeft="50dp"
android:text="hello"/>

</RelativeLayout>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>

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

输出:

enter image description here

希望对你有帮助~

关于java - NestedScrollView 重叠工具栏(它们位于 fragment 布局内部),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43761775/

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