ScrollView 与 layout_weight

本文介绍如何在Android应用中实现上下各有固定宽高的LinearLayout与中间可滚动的ScrollView布局,确保底部LinearLayout始终可见,不受ScrollView内容长度影响。

对如下布局:

 

----------------------------------------

|        LinearLayout                      |

----------------------------------------

|                                                |

|                                                |

|                                                |

|               ScrollView                  |

|                                                |

|                                                |

|                                                |

|                                                |

|                                                |

----------------------------------------

|        LinearLayout                      |

----------------------------------------

 

想达到的效果是上下各有一固定宽高的LinearLayout, 中间是一个ScrollView,ScrollView随着其中内容的多少可长可短.

 

这布局的特殊性是底部有一LinearLayout要始终显示出来,而不是在长长的ScrollView的后面(如果ScrollView较长的话,在第一屏就会一眼看不到底部的Linearlayout)。

 

如何实现呢?

 

其实很简单,给ScrollView加上以下属性

 

android:fillViewPort="true"

android:layout_weight="1"

 

android:fillViewport   Defines whether the scrollview should stretch its content to fill the viewport.

该属性决定当其内容宽高不足以填满可以区域时,是否将ScrollView将其内容区域拉伸以至填满可见区域。

 

android:layout_weight="1" 而上下LinearLayout不设置android:layout_weight属性,即默认为0,这样,ScrollView即老填满占据上下LinearLayout以外的中间空间。

 

 

 

 

 

<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" app:behavior_peekHeight="130dp" app:layout_behavior="@string/bottom_sheet_behavior"> <ScrollView android:layout_width="wrap_content" android:layout_height="wrap_content"> <LinearLayout android:id="@+id/ll_bottom_sheet" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" > <ImageView android:id="@+id/pull" android:layout_width="match_parent" android:layout_height="30dp" android:background="@color/white" android:clickable="false" android:focusable="false" android:gravity="center" android:paddingTop="15dp" android:src="@drawable/pullselector" /> <LinearLayout android:layout_width="match_parent" android:layout_height="60dp" android:layout_gravity="center_horizontal" android:background="@color/white" android:paddingStart="10dp" android:paddingTop="10dp" android:paddingEnd="10dp" android:weightSum="4"> <ImageButton android:id="@+id/button_mode" android:layout_width="0dp" android:layout_height="60dp" android:layout_weight="1" android:background="@android:color/transparent" android:scaleType="centerInside" android:src="@drawable/mode" /> <ImageButton android:id="@+id/button_schedule" android:layout_width="0dp" android:layout_height="60dp" android:layout_weight="1" android:background="@android:color/transparent" android:scaleType="centerInside" android:src="@drawable/schedule" /> <ImageButton android:id="@+id/button_away" android:layout_width="0dp" android:layout_height="60dp" android:layout_weight="1" android:background="@android:color/transparent" android:scaleType="centerInside" android:src="@drawable/away" /> <ImageButton android:id="@+id/button_timer" android:layout_width="0dp" android:layout_height="60dp" android:layout_weight="1" android:background="@android:color/transparent" android:scaleType="centerInside" android:src="@drawable/timer" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="42dp" android:layout_gravity="center_horizontal" android:background="@color/white" android:paddingStart="10dp" android:paddingEnd="10dp" android:weightSum="4"> <TextView android:layout_width="0dp" android:layout_height="42dp" android:layout_weight="1" android:gravity="center" android:text="Mode" /> <TextView android:layout_width="0dp" android:layout_height="42dp" android:layout_weight="1" android:gravity="center" android:text="Schedule" /> <TextView android:layout_width="0dp" android:layout_height="42dp" android:layout_weight="1" android:gravity="center" android:text="Away" /> <TextView android:layout_width="0dp" android:layout_height="42dp" android:layout_weight="1" android:gravity="center" android:text="Timer" /> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="10.5dp" android:background="@color/background_gray" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/white"> <TextView android:layout_width="113dp" android:layout_height="41dp" android:gravity="center" android:text="Today" android:textColor="@color/text_gray" android:textSize="13dp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="70dp" android:background="@color/white" android:paddingStart="35dp" android:paddingEnd="35dp" android:weightSum="3"> <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="@color/white" android:orientation="vertical"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="12.1" android:textSize="23dp" android:textStyle="bold" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="4dp" android:layout_weight="1" android:text="h" android:textSize="12dp" /> </LinearLayout> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="top" android:text="Runtime" android:textColor="@color/text_gray" android:textSize="14dp" /> </LinearLayout> <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="@color/white" android:orientation="vertical"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="125.1" android:textSize="23dp" android:textStyle="bold" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="4dp" android:layout_weight="1" android:text="Kmh" android:textSize="12dp" /> </LinearLayout> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="top" android:text="Usage" android:textColor="@color/text_gray" android:textSize="14dp" /> </LinearLayout> <LinearLayout android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginStart="11dp" android:layout_weight="1" android:background="@color/white" android:orientation="vertical"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="992.3" android:textSize="23dp" android:textStyle="bold" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="4dp" android:layout_weight="1" android:text="Kmh" android:textSize="12dp" /> </LinearLayout> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="top" android:text="Saving" android:textColor="@color/text_gray" android:textSize="14dp" /> </LinearLayout> </LinearLayout> <View android:layout_width="match_parent" android:layout_height="10.5dp" android:background="@color/background_gray" /> <LinearLayout android:layout_width="match_parent" android:layout_height="56dp" android:background="@color/white" android:gravity="center_vertical" android:paddingLeft="20dp" android:paddingRight="20dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Favorite" android:textSize="16dp" /> <!-- 占位 View,用来推动 ImageButton 到右边 --> <View android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="1" /> <ImageButton android:id="@+id/button_favorite" android:layout_width="42dp" android:layout_height="28dp" android:background="@android:color/transparent" android:scaleType="centerInside" android:src="@drawable/favorite_on" /> </LinearLayout> <FrameLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/white"> <View android:layout_width="320dp" android:layout_height="0.2dp" android:layout_gravity="center_horizontal" android:background="@color/text_gray" /> </FrameLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="56dp" android:background="@color/white" android:gravity="center_vertical" android:paddingLeft="20dp" android:paddingRight="20dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Device Sharing" android:textSize="16dp" /> <!-- 占位 View,用来推动 ImageButton 到右边 --> <View android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="1" /> <ImageView android:layout_width="16dp" android:layout_height="16dp" android:background="@android:color/transparent" android:scaleType="centerInside" android:src="@drawable/sharing" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="2" android:textColor="@color/text_gray" /> <ImageButton android:layout_width="24dp" android:layout_height="24dp" android:background="@android:color/transparent" android:scaleType="centerInside" android:src="@drawable/more" /> </LinearLayout> <FrameLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/white"> <View android:layout_width="320dp" android:layout_height="0.2dp" android:layout_gravity="center_horizontal" android:background="@color/text_gray" /> </FrameLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="56dp" android:background="@color/white" android:gravity="center_vertical" android:paddingLeft="20dp" android:paddingRight="20dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="FAQ & Feedback" android:textSize="16dp" /> <!-- 占位 View,用来推动 ImageButton 到右边 --> <View android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="1" /> <ImageButton android:layout_width="24dp" android:layout_height="24dp" android:background="@android:color/transparent" android:scaleType="centerInside" android:src="@drawable/more" /> </LinearLayout> <FrameLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/white"> <View android:layout_width="320dp" android:layout_height="0.2dp" android:layout_gravity="center_horizontal" android:background="@color/text_gray" /> </FrameLayout> <View android:layout_width="match_parent" android:layout_height="280dp" android:background="@color/white" /> </LinearLayout> </ScrollView> </LinearLayout>问题出在哪里
08-22
<? xml version = "1.0"encoding = " utf-8" ? > android : layout_width = " match_parent" <LinearLayout xmlns: android = " http://schemas.android.com/apk/res/android" android :layout_height = " match_parent" android: orientation = " vertical" ><HorizontalScrollView android: layout_width = " wrap_content" android:layout_height = " 0dp" android:layout_weight ="1" android:scrollbars = " none" >< LinearLayout android:layout_width = " wrap_content" android:layout_height = " match_parent" android :orientation = " horizontal" ><View android: layout_width = "300dp" android :layout_height = " match_parent" android:background = "@color/red"/ ><View android:layout_width = "300dp" android ;layout_height = " match_parent" android:background = " @color/green"/ ><View android :layout_width ="300dp" android :layout_height = " match_parent" android:background = " @color/blue"/></LinearLayout ></HorizontalScrollView ><ScrollView android :layout_width = " match_parent"android :layout_height ="0dp" android :layout_weight ="1" android : scrollbars = " none" ><LinearLayout android :layout_width = " match_parent" android :layout_height = " wrap_content" android :orientation =" vertical" ><View android :layout_width = " match_parent" android :layout_height ="200dp" android:background =" @color/yellow"/ ><View android:layout_width = " match_parent" android :layout_height ="200dp" android;background =" @color/cyan"/ ><View android :layout_width = " match_parent" android:layout_height ="200dp" android :background = " @color/blue"/> </LinearLayout ></ScrollView>/LinearLayout >修正补全
09-18
<?xml version=“1.0” encoding=“utf-8”?> <LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android” android:layout_width=“match_parent” android:layout_height=“match_parent” android:orientation=“vertical” android:padding=“16dp” android:background=“#F5F5F5”> <!-- 连接按钮 --> <Button android:id=“@+id/btnConnect” android:layout_width=“match_parent” android:layout_height=“213dp” android:layout_marginBottom=“16dp” android:layout_weight=“1” android:background=“@drawable/btn_primary_shape” android:text=“连接设备” android:textColor=“#FFFFFF” android:textSize=“20sp” /> <!-- 状态显示 --> <TextView android:id=“@+id/tvStatus” android:layout_width=“match_parent” android:layout_height=“63dp” android:layout_marginBottom=“16dp” android:text=“未连接” android:gravity=“center” android:textSize=“25sp” android:layout_weight=“1” android:textColor=“#333333” android:background=“@drawable/status_background”/> <!-- 第一行指令 --> <LinearLayout android:layout_width=“match_parent” android:layout_height=“wrap_content” android:layout_marginBottom=“8dp” android:layout_weight=“1” android:orientation=“horizontal”> <Button android:id="@+id/btnCmd1" android:layout_width="0dp" android:layout_height="100dp" android:layout_marginEnd="4dp" android:layout_weight="1" android:background="@drawable/btn_primary_shape" android:text="暂停" android:textColor="#FFFFFF" android:textSize="20sp" /> <Button android:id="@+id/btnCmd2" android:layout_width="0dp" android:layout_height="100dp" android:layout_marginStart="4dp" android:layout_weight="1" android:background="@drawable/btn_secondary_shape" android:text="继续" android:textColor="#FFFFFF" android:textSize="20sp" /> </LinearLayout> <!-- 第二行指令 --> <LinearLayout android:layout_width=“match_parent” android:layout_height=“wrap_content” android:layout_marginBottom=“8dp” android:layout_weight=“1” android:orientation=“horizontal”> <Button android:id="@+id/btnCmd3" android:layout_width="0dp" android:layout_height="100dp" android:layout_marginEnd="4dp" android:layout_weight="1" android:background="@drawable/btn_secondary_shape" android:text="时间周期(1S)" android:textColor="#FFFaFF" android:textSize="20sp" /> <Button android:id="@+id/btnCmd4" android:layout_width="0dp" android:layout_height="100dp" android:layout_marginStart="4dp" android:layout_weight="1" android:background="@drawable/btn_secondary_shape" android:text="时间周期(2S)" android:textColor="#FFFFFF" android:textSize="20sp" /> </LinearLayout> <!-- 第三行指令 --> <LinearLayout android:layout_width=“match_parent” android:layout_height=“wrap_content” android:layout_weight=“1” android:orientation="horizontal"> <Button android:id="@+id/btnCmd5" android:layout_width="0dp" android:layout_height="100dp" android:layout_marginEnd="4dp" android:layout_weight="1" android:background="@drawable/btn_secondary_shape" android:text="时间周期(5S)" android:textColor="#FFFFFF" android:textSize="20sp" /> <Button android:id="@+id/btnCmd6" android:layout_width="0dp" android:layout_height="100dp" android:layout_marginStart="4dp" android:layout_weight="1" android:background="@drawable/btn_secondary_shape" android:text="时间周期(10S)" android:textColor="#FFFFFF" android:textSize="20sp" /> </LinearLayout>布局用这这个,添加下拉框
03-20
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值