滚动视图重叠工具栏

7

我想使用ScrollView和Toolbar作为LinearLayout的唯一子元素,但它们彼此重叠。搜索了一下,但没有找到有用的信息,有什么解决方法吗?除了给ScrollView设置padding/margin之外。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:float="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="right"
android:background="@color/white_dark_50">

<android.support.v7.widget.Toolbar
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/toolbar"
    android:theme="@style/AppTheme"
    android:scrollbarStyle="outsideOverlay"
    android:gravity="center_vertical|right"
    android:layout_alignParentTop="true"
    android:background="@color/colorPrimary"
    >
    <!-- -->



</android.support.v7.widget.Toolbar>



<ScrollView
    android:scrollbarStyle="outsideOverlay"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:layout_gravity="center"
    android:background="@color/white_dark_50" >
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="right"
        android:background="@color/white_dark_50">


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="ایجاد جمله جدید"
            android:id="@+id/new_sentence_title"
            android:textColor="@color/midnight_blue"
            android:layout_margin="10dp"
            android:textStyle="bold"
            android:gravity="center_vertical"
            android:drawableRight="@drawable/ic_colored_doll"
            android:textAppearance="?android:attr/textAppearanceLarge"
            />

        <View
            android:layout_width="match_parent"
            android:layout_height="3dp"
            android:layout_marginLeft="25dp"
            android:layerType="software"
            android:layout_marginRight="25dp"
            android:background="@drawable/dashed_line" />

        <com.wrapp.floatlabelededittext.FloatLabeledEditTextRightSided
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dp"
            android:layout_marginLeft="5dp"
            android:layout_marginBottom="5dp"
            float:fletPaddingLeft="10dp"
            float:fletPaddingRight="10dp"
            android:id="@+id/new_sentence_name"
            float:fletPadding="10dp">

            <EditText
                android:layout_marginLeft="10dp"
                android:id="@+id/new_sentence_name_et"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:theme="@style/AppTheme"
                android:backgroundTint="@color/accentColor"
                android:singleLine="true"
                android:textColor="@color/midnight_blue"
                android:textColorHint="@color/hintColor"
                android:maxLength="20"
                android:drawableRight="@drawable/ic_name"
                android:gravity="center_vertical|right"
                android:hint="عنوان"/>
        </com.wrapp.floatlabelededittext.FloatLabeledEditTextRightSided>


        <com.wrapp.floatlabelededittext.FloatLabeledEditTextRightSided
            android:layout_width="match_parent"
            android:foregroundGravity="center_vertical|right"
            android:layout_height="wrap_content"
            android:layout_marginRight="5dp"
            android:layout_marginLeft="5dp"
            android:layout_marginBottom="5dp"
            float:fletPaddingLeft="10dp"
            float:fletPaddingRight="10dp"
            android:id="@+id/new_sentence_period"
            float:fletPadding="10dp">

            <EditText
                android:id="@+id/new_sentence_period_et"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:theme="@style/AppTheme"
                android:backgroundTint="@color/accentColor"
                android:singleLine="true"
                android:layout_marginLeft="10dp"
                android:inputType="number"
                android:textColor="@color/midnight_blue"
                android:textColorHint="@color/hintColor"
                android:maxLength="20"
                android:drawableRight="@drawable/ic_name"
                android:gravity="center_vertical|right"
                android:hint="دوره تکرار"/>
        </com.wrapp.floatlabelededittext.FloatLabeledEditTextRightSided>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_marginRight="5dp"
            android:gravity="center_vertical|right">


            <Spinner
                android:id="@+id/repeat_kind"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                style="@style/AppTheme"
                android:minHeight="25dp"
                android:gravity="right"
                android:layout_marginRight="5dp"
                android:layout_marginLeft="10dp"
                />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="نوع تکرار"
                android:id="@+id/doll_type_tv"
                android:textColor="@color/midnight_blue"
                android:layout_margin="10dp"
                android:gravity="center_vertical"
                android:drawableRight="@drawable/ic_doll"
                android:textAppearance="?android:attr/textAppearanceMedium" />


        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_marginRight="5dp"
            android:gravity="center_vertical|right">


            <com.gc.materialdesign.views.ButtonFlat
                android:id="@+id/date_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@color/blue"
                android:text="Button"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="تاریخ تکرار"
                android:textColor="@color/midnight_blue"
                android:layout_margin="10dp"
                android:gravity="center_vertical"
                android:drawableRight="@drawable/ic_doll"
                android:textAppearance="?android:attr/textAppearanceMedium" />
        </LinearLayout>


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_marginRight="5dp"
            android:gravity="center_vertical|right">


            <com.gc.materialdesign.views.ButtonFlat
                android:id="@+id/time_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@color/blue"
                android:text="Button"/>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="زمان تکرار"
                android:textColor="@color/midnight_blue"
                android:layout_margin="10dp"
                android:gravity="center_vertical"
                android:drawableRight="@drawable/ic_doll"
                android:textAppearance="?android:attr/textAppearanceMedium" />
        </LinearLayout>






        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_marginBottom="10dp"
            android:layout_marginRight="5dp"
            android:gravity="center_vertical|right">


            <ImageView
                android:id="@+id/doll_pic_IV"
                android:layout_width="150dp"
                android:layout_height="150dp"
                style="@style/AppTheme"
                android:minHeight="25dp"
                android:scaleType="fitXY"
                android:gravity="right"
                android:layout_marginRight="5dp"
                android:layout_marginLeft="10dp"
                android:src="@drawable/ic_image_add_to_photos"
                />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="انتخاب صدا"
                android:textColor="@color/midnight_blue"
                android:layout_margin="10dp"
                android:gravity="center_vertical"
                android:id="@+id/pic_tv"
                android:drawableRight="@drawable/ic_image"
                                                                                   android:textAppearance="?android:attr/textAppearanceMedium" />
        </LinearLayout>

    </LinearLayout>
</ScrollView>

7个回答

10

显然问题并不是重叠的工具栏和滚动视图,而是滚动视图本身的问题。问题出现在ScrollView单一子元素的对齐方式上,因此从ScrollView内的LinearLayout中删除对齐方式,问题得以解决。这对我来说似乎像是一个错误,也许我应该联系谷歌并告诉他们这个问题。


太棒了!非常感谢你的回答,你在Google上开了一个bug吗? - Waza_Be
3
@Waza_Be,不,我太懒了 XD。 - M. Erfan Mowlaei

8

尝试:

<LinearLayout>

    <Toolbar 
        android:layout_width="match_parent"
        android:layout_height="wrap_content" /> 

    <ScrollView 
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

</LinearLayout>

这样LinearLayout 首先会计算工具栏的尺寸,因为它没有 layout_weight 属性,并将其放下。然后将所有剩余的空间分配给ScrollView


尝试从外部LinearLayout中删除“gravity”和“layout_gravity”。 - natario
我猜问题比我说的更严重,ScrollView中LinearLayout的第一个子元素向上移动并超出了ScrollView本身,因此它们没有被显示。 - M. Erfan Mowlaei
在这里找到了答案:https://dev59.com/X2kw5IYBdhLWcg3waZxW 。无论如何,感谢您的回复。 - M. Erfan Mowlaei
请将您找到的解决方案发布在答案中,这会对未来的读者有所帮助。 - natario
好的,我会做。 - M. Erfan Mowlaei

4
我通过在RelativeLayout中放置工具栏和ScrollView来解决了这个问题,然后在ScrollView中加入"android:layout_below="@+id/bar"",就像这样:
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="match_parent">

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

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize" />

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

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/bar"
    android:paddingTop="20dp"
    android:layout_gravity="center">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="match_parent"/></ScrollView></RelativeLayout>

1

我的问题是:我正在将一个活动重建为AppCompatActivity,同时使用一个新的独立工具栏,但它被ScrollView覆盖。我的解决方案是给工具栏增加高度:

<android.support.v7.widget.Toolbar
...
android:elevation="3dp"
.../>

增加滚动视图中顶部项目的边距。
<EditText
...
android:layout_marginTop="80dp"
.../>

0

对于任何遇到与NestedScrollview类似的问题的人,将根组件的marginTop设置为actionBarSize,即:

android:layout_marginTop="?attr/actionBarSize"

0

我通过添加这段代码解决了这个问题,希望能对你有所帮助。

<LinearLayout 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"
android:fillViewport="false"
android:orientation="vertical"
android:padding="@dimen/activity_vertical_margin"
tools:context="tz.co.ikunda.MemberProfileActivity">


<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="56dp"
    android:orientation="vertical">

    <TextView
      android:id="@+id/tvMyView"
      android:layout_width="match_parent"
      android:text="You views goes between this scrollview layout"
      android:layout_height="wrap_content" />
</ScrollView>


0
对我来说,问题很简单地解决了,只需将我的ScrollView放在Frame布局中即可。
  <FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">
         ......

    </ScrollView>
 </FrameLayout>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接