【问题标题】:ScrollView Overlaps ToolbarScrollView 重叠工具栏
【发布时间】:2015-05-22 21:12:08
【问题描述】:

我正在尝试使用 ScrollView 和 Toolbar 作为 LinearLayout 的唯一子级,但它们彼此重叠。搜索了一下,但没有找到任何有用的东西,知道如​​何解决这个问题而不是给 ScrollView 提供填充/边距?

<?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>

【问题讨论】:

    标签: android android-layout android-scrollview


    【解决方案1】:

    显然问题不在于 Toolbar 与 ScrollView 重叠,而在于 ScrollView 本身。问题是由 ScrollView 上唯一的孩子的重力引起的,因此从 ScrollView 内的 LinearLayout 中移除重力,问题就解决了。这对我来说似乎是一个错误,也许我应该联系谷歌并让他们知道这件事。

    【讨论】:

    • 太棒了!非常感谢您的回答,您是否在 Google 上打开过错误?
    • @Waza_Be nah 我好懒XD
    【解决方案2】:

    试试:

    <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”。
    • 我想问题比我说的更严重,ScrollView 的 LinearLayout 中的第一个孩子正在顶部和 Scrollview 本身之外,因此它们没有被显示
    • 在这里找到我的答案:stackoverflow.com/questions/9916317/…。无论如何感谢您的回复。
    • 发布自己找到的解决方案的答案,它将帮助未来的读者。
    • 好的好的先生,我会做的
    【解决方案3】:

    我通过在相对布局中放置工具栏和滚动视图来解决这个问题,然后在滚动视图中给出“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>
    

    【讨论】:

      【解决方案4】:

      对于我的问题:我正在将一个活动重建到 appCompatActivity 中,同时使用一个被滚动视图重叠的新独立工具栏。我的解决方案是提供工具栏高度:

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

      并增加我的滚动视图中顶部项目的边距。

      <EditText
      ...
      android:layout_marginTop="80dp"
      .../>
      

      【讨论】:

        【解决方案5】:

        我通过添加此代码解决了这个问题,我希望它会有所帮助

        <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>
        

        【讨论】:

          【解决方案6】:

          对我来说,只需将 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>
          

          【讨论】:

            猜你喜欢
            • 2023-03-09
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-02-20
            • 2015-05-02
            • 2016-03-03
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多