【问题标题】:How to set my toolbar fixed while scrolling android如何在滚动android时将我的工具栏设置为固定
【发布时间】:2015-11-03 18:20:02
【问题描述】:

我目前在做电子商务安卓应用,我需要在滚动时修复工具栏。

我附上了下面的截图。滚动操作栏时需要固定,并且正文内容在工具栏后面滚动

这是布局xml:

<LinearLayout
    android:id="@+id/container_toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <include
        android:id="@+id/toolbar_reg"
        layout="@layout/app_bar" />
</LinearLayout>

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:elevation="3dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="?android:attr/actionBarSize"
        android:background="#ffffff"
        android:orientation="vertical">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="150dp"
            android:layout_marginTop="15dp"
            android:src="@drawable/logo" />


        <android.support.design.widget.TextInputLayout
            android:id="@+id/input_layout_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:layout_marginTop="5dp">

            <EditText
                android:id="@+id/input_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"

                android:hint="@string/reg_name"
                android:singleLine="true" />
        </android.support.design.widget.TextInputLayout>

        <android.support.design.widget.TextInputLayout
            android:id="@+id/input_layout_email"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:layout_marginTop="5dp">

            <EditText
                android:id="@+id/input_email"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"

                android:hint="@string/reg_email"
                android:inputType="textEmailAddress" />
        </android.support.design.widget.TextInputLayout>

        <android.support.design.widget.TextInputLayout
            android:id="@+id/input_layout_password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:layout_marginTop="5dp">

            <EditText
                android:id="@+id/input_password"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"

                android:hint="@string/reg_password"
                android:inputType="textPassword" />
        </android.support.design.widget.TextInputLayout>

        <android.support.design.widget.TextInputLayout
            android:id="@+id/input_layout_cpassword"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:layout_marginTop="5dp">

            <EditText
                android:id="@+id/input_cpassword"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"

                android:hint="Confirm Password"
                android:inputType="textPassword" />
        </android.support.design.widget.TextInputLayout>

        <android.support.design.widget.TextInputLayout
            android:id="@+id/input_layout_mobile"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:layout_marginTop="5dp">

            <EditText
                android:id="@+id/input_mobile"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"

                android:hint="Mobile No"
                android:inputType="number" />
        </android.support.design.widget.TextInputLayout>

        <android.support.design.widget.TextInputLayout
            android:id="@+id/input_layout_dob"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:layout_marginTop="5dp">

            <EditText
                android:id="@+id/input_dob"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"

                android:hint="Date of Birth" />
        </android.support.design.widget.TextInputLayout>

        <android.support.design.widget.TextInputLayout
            android:id="@+id/input_layout_address"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:layout_marginTop="5dp">

            <EditText
                android:id="@+id/input_address"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Permanent Address" />
        </android.support.design.widget.TextInputLayout>

        <Button
            android:id="@+id/btn_signup"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:layout_marginTop="25dp"
            android:background="@color/colorPrimary"
            android:text="@string/btn_sign_up"
            android:textColor="@android:color/white" />

        <TextView
            android:id="@+id/link_login"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="150dp"
            android:layout_marginTop="15dp"
            android:gravity="center"
            android:text="@string/req_login"
            android:textSize="16dp" />

    </LinearLayout>
</ScrollView>

【问题讨论】:

  • 发布您的布局和活动/片段代码。这是 android 中的默认行为,内容通常在操作栏后面滚动。因此,如果它在您的操作栏上滚动,那么您就是在布局中做某事。
  • 我已将我的布局附在下面,请检查并尽快通知我
  • 我在您的布局中没有看到根视图。您应该在线性布局中移动所有滚动视图。
  • 在我的线性布局中移动滚动视图后它工作正常

标签: android android-actionbar android-toolbar


【解决方案1】:

您应该像这样构建您的 XML:

Relative layout
|-->Toolbar    (android:id="@+id/toolbar")
|-->ScrollView (android:layout_below="@id/toolbar")
    |-->ViewGroup(For example, CoordinatorLayout)
        |-->Child
        |-->Child
        |-->Child

示例代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <android.support.v7.widget.Toolbar
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:theme="@style/WelcomeActivityTheme.Toolbar"
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        />
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/toolbar">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
        />
    </ScrollView>
</RelativeLayout>

【讨论】:

  • 谢谢,而且你不需要将工具栏高度设置为 56 dp,android:layout_height="match_parent" 也可以。
  • 滚动视图只能有1个孩子不是吗?
【解决方案2】:

您发布的布局似乎没有根视图。

但是要解决滚动问题,您需要将滚动视图移动到同一父级下,如下所示:

<LinearLayout
    android:id="@+id/container_toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <include
        android:id="@+id/toolbar_reg"
        layout="@layout/app_bar" />

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:elevation="3dp">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="?android:attr/actionBarSize"
        android:background="#ffffff"
        android:orientation="vertical">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="150dp"
            android:layout_marginTop="15dp"
            android:src="@drawable/logo" />


        <android.support.design.widget.TextInputLayout
            android:id="@+id/input_layout_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:layout_marginTop="5dp">

            <EditText
                android:id="@+id/input_name"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"

                android:hint="@string/reg_name"
                android:singleLine="true" />
        </android.support.design.widget.TextInputLayout>

        <android.support.design.widget.TextInputLayout
            android:id="@+id/input_layout_email"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:layout_marginTop="5dp">

            <EditText
                android:id="@+id/input_email"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"

                android:hint="@string/reg_email"
                android:inputType="textEmailAddress" />
        </android.support.design.widget.TextInputLayout>

        <android.support.design.widget.TextInputLayout
            android:id="@+id/input_layout_password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:layout_marginTop="5dp">

            <EditText
                android:id="@+id/input_password"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"

                android:hint="@string/reg_password"
                android:inputType="textPassword" />
        </android.support.design.widget.TextInputLayout>

        <android.support.design.widget.TextInputLayout
            android:id="@+id/input_layout_cpassword"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:layout_marginTop="5dp">

            <EditText
                android:id="@+id/input_cpassword"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"

                android:hint="Confirm Password"
                android:inputType="textPassword" />
        </android.support.design.widget.TextInputLayout>

        <android.support.design.widget.TextInputLayout
            android:id="@+id/input_layout_mobile"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:layout_marginTop="5dp">

            <EditText
                android:id="@+id/input_mobile"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"

                android:hint="Mobile No"
                android:inputType="number" />
        </android.support.design.widget.TextInputLayout>

        <android.support.design.widget.TextInputLayout
            android:id="@+id/input_layout_dob"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:layout_marginTop="5dp">

            <EditText
                android:id="@+id/input_dob"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"

                android:hint="Date of Birth" />
        </android.support.design.widget.TextInputLayout>

        <android.support.design.widget.TextInputLayout
            android:id="@+id/input_layout_address"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:layout_marginTop="5dp">

            <EditText
                android:id="@+id/input_address"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Permanent Address" />
        </android.support.design.widget.TextInputLayout>

        <Button
            android:id="@+id/btn_signup"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:layout_marginTop="25dp"
            android:background="@color/colorPrimary"
            android:text="@string/btn_sign_up"
            android:textColor="@android:color/white" />

        <TextView
            android:id="@+id/link_login"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="150dp"
            android:layout_marginTop="15dp"
            android:gravity="center"
            android:text="@string/req_login"
            android:textSize="16dp" />

    </LinearLayout>
</ScrollView>
</LinearLayout>

【讨论】:

    【解决方案3】:

    在您的 xml 中,给定的 Root 视图是 RelativeLayout 你应该在scrollview xml中设置

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/container_toolbar"
        android:elevation="3dp">
    

    并从滚动视图标签下方的线性布局中删除 margin_top。

    【讨论】:

      【解决方案4】:

      我尝试了很多方法来保持toolbar 不变,而不是通过android:windowSoftInputMode="adjustResize" 调整或压缩布局,这是不合适的。

      这只能通过协调器布局实现:

      <android.support.design.widget.CoordinatorLayout 
      xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:card_view="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.design.widget.CollapsingToolbarLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content">
      
              <android.support.v7.widget.Toolbar 
                 xmlns:android="http://schemas.android.com/apk/res/android"
                 xmlns:ripple="http://schemas.android.com/apk/res-auto"
                 android:id="@+id/tv_toolbar"
                 android:layout_width="match_parent"
                 android:layout_height="50dp"
                 android:background="#fff"
                 ripple:contentInsetStart="0dp">
              </android.support.v7.widget.Toolbar>
      
          </android.support.design.widget.CollapsingToolbarLayout>
      </android.support.design.widget.AppBarLayout>
      
      <ScrollView
          android:id="@+id/coordinate_container"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:marginTop="56dp">
      </ScrollView>
      

      保持结构为

         Coordinator layout
        |-->Toolbar    (android:id="@+id/toolbar")
        |-->ScrollView (android:layout_below="@id/toolbar")
         |-->Child
         |-->Child
         |-->Child
      

      【讨论】:

        猜你喜欢
        • 2017-06-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-10-03
        • 1970-01-01
        • 2016-06-09
        • 2022-11-16
        相关资源
        最近更新 更多