【问题标题】:Add LinearLayout before ScrollView在 ScrollView 之前添加 LinearLayout
【发布时间】:2021-01-22 16:16:06
【问题描述】:

我有一个滚动视图,但我想要一个带有 3 个按钮的线性布局,这些按钮在滚动时保持在顶部。所以它的功能应该有点像贴在顶部的导航栏。 当前代码:

<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <androidx.gridlayout.widget.GridLayout
        android:id="@+id/gridRoot"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:columnCount="2"
        app:rowCount="32">
    </androidx.gridlayout.widget.GridLayout>
</ScrollView>

【问题讨论】:

    标签: android xml layout android-linearlayout android-scrollview


    【解决方案1】:

    ScrollView 放在一个垂直的LinearLayout 中,并在ScrollView 上方添加这三个按钮。

    <LinearLayout android:orientation="vertical">
      <YourButtonsHere/>
      <ScrollView/>
    </LinearLayout>
    

    【讨论】:

    • 这样,你的3个按钮在堆积。然后你 ScrollView
    【解决方案2】:

    有几种方法可以实现这一点,但最简单的一种是将垂直 LinearLayout 作为父级。然后,您将按钮放在水平线性布局中,以便它们位于顶部并充当“条”。 然后你只需要添加滚动视图。 这样用户可以滚动滚动视图,并且 3 个按钮保持在滚动视图的顶部。

    <!-- vertical linear layout as parent -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
            
        <!-- Place buttons in a horizontal linearlayout, so that you have a bar on the 
         top-->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
                
            <!-- buttons width=0 + same layout_weight, that way the width of the 
             linear layout is equally distributed between the 3 buttons--> 
            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"/>
    
            <Button
                android:layout_width="0dp"
                android:layout_height=""
                android:layout_weight="1"/>
    
            <Button
                android:layout_width="0dp"
                android:layout_height=""
                android:layout_weight="1"/>
    
        </LinearLayout>
    
        <!-- your scroll view here-->
        <ScrollView
            android:layout_width=""
            android:layout_height="">
                
        </ScrollView>
    
    </LinearLayout>
    

    【讨论】:

      【解决方案3】:

      XML:

          <androidx.appcompat.widget.LinearLayoutCompat
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:orientation="vertical"
          android:weightSum="10">
      
          <androidx.appcompat.widget.LinearLayoutCompat
              android:layout_width="match_parent"
              android:layout_height="0dp"
              android:gravity="center"
              android:layout_weight="1"
              android:orientation="horizontal">
      
              <com.sharedcode.widgets.CustomButton
                  style="@style/FlatButton"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text="btn1"
                  android:layout_margin="@dimen/spacing_6" />
      
              <com.sharedcode.widgets.CustomButton
                  style="@style/FlatButton"
                  android:layout_width="wrap_content"
                  android:text="btn2"
                  android:layout_height="wrap_content"
                  android:layout_margin="@dimen/spacing_6" />
      
              <com.sharedcode.widgets.CustomButton
                  style="@style/FlatButton"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:text="btn3"
                  android:layout_margin="@dimen/spacing_6" />
      
          </androidx.appcompat.widget.LinearLayoutCompat>
      
          <ScrollView
              android:layout_width="match_parent"
              android:layout_height="0dp"
              android:layout_weight="9"
              tools:context=".MainActivity">
      
              //in your case this is GridView
              <androidx.recyclerview.widget.RecyclerView 
                  android:id="@+id/gridRoot"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  app:columnCount="2"
                  app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
                  app:rowCount="32"
                  app:spanCount="2"
                  tools:listitem="@layout/row_exam" />
          </ScrollView>
      </androidx.appcompat.widget.LinearLayoutCompat>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多