日积月累:布局横竖向滑动需求的实现

在工作过程当中,出现这样一个需求。要求屏幕中的内容分为两个区域,我称为A区域和B区域,要求B区域可以竖直和横向滑动,A区域能随着B区域横向滑动(但不能竖直滑动),并且A区域位于屏幕的底部,B区域位于A区域的上部。效果图如下 

Image

图片1:起始页面 

Image

图片2:B区域下滑,A区域不滑动 

Image

图片3:B区域横滑,A区域随之滑动 

根据需求,我们需要实现横向和竖向的滑动,不由的想起HorizontalScrollView和ScrollView两个对象;并且要求A区域位于屏幕的底部,B区域位于A区域的上部,所以我想使用RelativeLayout来实现。 

布局如下 

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin" 
     android:paddingLeft="@dimen/activity_horizontal_margin" 
     android:paddingRight="@dimen/activity_horizontal_margin" 
     android:paddingTop="@dimen/activity_vertical_margin" 
     tools:context=".MainActivity" > 
     <HorizontalScrollView 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" > 
        <!--注意一:HorizontalScrollView必须以布局作为子元素,由于我们需要控制两个区域的相对位置,所以我们选择RelativeLayout作为子元素--> 
        <RelativeLayout 
            android:layout_width="match_parent" 
            android:layout_height="match_parent" > 
            <!--注意二:由于A区域,B区域相对布局的标准,由于布局的顺序解析过程,必须将A区域放置在B区域的上方--> 
            <!--A区域--> 
               <TextView 
                android:id="@+id/select_bar" 
                android:layout_width="match_parent" 
                android:layout_height="wrap_content" 
                android:layout_alignParentBottom="true" 
                android:background="#12010234" 
                android:text="A区域1A区域2A区域3... …A区域24" 
                android:textSize="50sp" /> 
            <!--注意三:由于A区域不能竖向滑动,则只放在HorizontalScrollView中,B区域能横向、竖向滑动,故包含在HorizontalScrollView和ScrollView中。--> 
            <ScrollView 
                android:id="@+id/ball_bar" 
                android:layout_width="match_parent" 
                android:layout_height="wrap_content" 
                android:layout_above="@id/select_bar" > 
                  <LinearLayout 
                    android:layout_width="match_parent" 
                    android:layout_height="wrap_content" 
                    android:background="#97687876" 
                    android:orientation="vertical" > 
                    <!--B区域--> 
                      <TextView 
                        android:layout_width="match_parent" 
                        android:layout_height="wrap_content" 
                        android:text="B区域1B区域2B区域3... … 区域10B" 
                        android:textSize="50sp" /> 
                       … … … …  
                      <TextView 
                        android:layout_width="match_parent" 
                        android:layout_height="wrap_content" 
                        android:text=B区域90B区域92B区域93... … 区域100B" 
                        android:textSize="50sp" /> 
                   </LinearLayout> 
               </ScrollView> 
         </RelativeLayout> 
     </HorizontalScrollView> 
</RelativeLayout> 
新技术,新未来!欢迎大家关注 “1024工场”微信服务号 ,时刻关注我们的最新的技术讯息! (甭客气!尽情的扫描或者长按!)


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值