我們在做一個表單界面的時候,表單項很多,會用ScrollView滾動條來滾動,但是這時候的Button會跟着滾動條一起滾動,如果我們希望Button固定在底部,滾動條只滾動表單,有兩種方法:
1.用權重,即android:layout_weight,具體代碼如下
1 <ScrollView 2 android:layout_height="0dp" 3 android:id="@+id/scrollView1" 4 android:layout_width="match_parent" 5 android:layout_weight="8" 6 >
注意第二行,android:layout_height="0dp" 這種寫法可以保證豎直方向按權重來布局,然后看Button
1 </ScrollView> 2 <LinearLayout 3 android:layout_height="0dp" 5 android:layout_width="match_parent" 6 android:orientation="vertical" 7 android:background="@color/black" 8 android:layout_weight="1" 9 > 10 <Button 11 android:id="@+id/btregist" 12 android:layout_height="wrap_content" 13 android:layout_width="match_parent" 14 android:background="@drawable/regist_button" 15 android:layout_gravity="bottom" 16 ></Button> 17 </LinearLayout>
第八行,android:layout_weight="1",這樣滾動條 和 Button 就以8:1的比例顯示了,當然,我省略了滾動條內的部分。
2.第二種方法,相對布局,直接把button放下面,scrollView放在Button上面,這辦法討巧了
1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 android:layout_width="fill_parent" 5 android:layout_height="fill_parent" 6 7 > 8 <ScrollView 9 android:layout_height="fill_parent" 10 android:id="@+id/scrollView1" 11 android:layout_width="match_parent" 12 android:layout_above="@+id/llybuttom" 13 > 14 ………………此處省略一萬字 15 </ScrollView> 16 <LinearLayout 17 android:layout_height="wrap_content" 18 android:id="@+id/llybuttom" 19 android:layout_width="match_parent" 20 android:orientation="vertical" 21 android:layout_alignParentBottom="true" 22 > 23 <Button 24 android:id="@+id/btregist" 25 android:layout_height="wrap_content" 26 android:layout_width="match_parent" 27 android:background="@drawable/regist_button" 28 android:layout_gravity="bottom" 29 ></Button> 30 </LinearLayout> 31 </RelativeLayout>
這兩種方法都可以用哦~