【问题标题】:static buttons inside a scrollView androidscrollView android中的静态按钮
【发布时间】:2018-05-07 18:58:17
【问题描述】:

我正在制作一个 xml 布局文件,由 3 部分组成:

  • 第 1 部分:关于某个地点的一些信息和图片。

  • 第 2 部分:两个水平的Buttons。

  • 第 3 部分:RecyclerView 一些人的评论。

这是我的布局文件:

<ScrollView 
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:name="com.example.abdo.foodproject.OneItemFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.abdo.foodproject.OneItemFragment"
tools:layout="@layout/activity_item"
>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/oneitem_background_theme"
    android:orientation="vertical"
    android:weightSum="13"
    >
    <LinearLayout>
    (...) <!--part 1-->
    </LinearLayout>
    <LinearLayout  <!--part 2-->
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
            android:id="@+id/review_list"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="@string/reviews"
            android:background="@color/colorPrimaryDark"

            />
        <Button
            android:id="@+id/bestMeal_list"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimaryLight"
            android:text="@string/best_meal"
            />
    </LinearLayout>


     <android.support.v7.widget.RecyclerView  <!--part 3-->
        android:id="@+id/recyclerViewItem"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </android.support.v7.widget.RecyclerView>

</LinearLayout>

我想让按钮固定在我的屏幕上,在窗口的顶部,而不会因为滚动而失去它们的视图。

有人知道怎么做吗?

【问题讨论】:

  • 显示你的布局文件
  • 更新了代码
  • 好的,到底是什么不起作用?您的预期结果是什么?
  • 我希望当滚动视图工作时,第 1 部分消失,这两个按钮只是粘在我的窗口顶部,不会向上移动并消失。
  • 您希望它们在哪里保持静态?在底部还是顶部?

标签: android xml scrollview


【解决方案1】:

您可以使用 CoordinatorLayout 来实现这种效果。 CoordinatorLayout 有 2 个孩子,其中一个也有 2 个孩子,如您所愿

  1. AppBarLayout:是一种垂直方向的线性布局。滚动完成后,根据"app:layout_collapseMode"属性,其内容可以消失或固定。

    • 1.1。 CollapsingToolbarLayout:您可以将“第 1 部分”视图放置在此位置,app:layout_collapseMode="parallax" 在消失时变为视差。

    • 1.2。固定视图。 LinearLayout:随意替换为您的“第 2 部分”视图。滚动完成后,此部分将被固定。

  2. 可滚动视图:在您的示例中为 RecyclerView。

最终代码应该类似:

<?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:background="@android:color/transparent"
        app:elevation="0dp">

        <android.support.design.widget.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:contentScrim="@android:color/transparent"
            app:layout_scrollFlags="scroll|enterAlwaysCollapsed">

            <!--part 1-->
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_collapseMode="parallax">
            </LinearLayout>
        </android.support.design.widget.CollapsingToolbarLayout>

        <!--part 2-->
        <LinearLayout 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:weight_sum="2">
            <Button
                android:id="@+id/review_list"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:text="@string/reviews"
                android:background="@color/colorPrimaryDark"
                />
            <Button
                android:id="@+id/bestMeal_list"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content"
                android:background="@color/colorPrimaryLight"
                android:text="@string/best_meal"
                />
        </LinearLayout>
    </android.support.design.widget.AppBarLayout>

    <!--part 3-->
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerViewItem"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
</android.support.design.widget.CoordinatorLayout>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-04
    • 1970-01-01
    • 1970-01-01
    • 2021-07-02
    • 1970-01-01
    • 1970-01-01
    • 2012-03-08
    • 1970-01-01
    相关资源
    最近更新 更多