如何在android中将一组按钮对齐到屏幕底部

前端之家收集整理的这篇文章主要介绍了如何在android中将一组按钮对齐到屏幕底部前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在我的布局中,有四个部分分别是标题,可编辑控件,列表和一组按钮.我想将按钮保持在屏幕下方.我在布局上做了太多更改以强制按钮到底.但什么都没发生.请提供说明,以满足我的需求.我也在发布布局
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="wrap_content">
  5.  
  6. <LinearLayout
  7. android:layout_width="match_parent"
  8. android:layout_height="wrap_content"
  9. android:orientation="vertical">
  10.  
  11. <!-- heading for basic settings -->
  12. <TextView
  13. android:id="@+id/setup_macroheading"
  14. style="@style/txtHeading" />
  15.  
  16. <View
  17. android:layout_width="fill_parent"
  18. android:layout_height="20dp"/>
  19.  
  20. <!-- macro name -->
  21. <LinearLayout
  22. android:layout_width="match_parent"
  23. android:layout_height="wrap_content"
  24. android:paddingLeft="20dp"
  25. android:paddingRight="20dp"
  26. android:orientation="horizontal">
  27.  
  28. <TextView
  29. android:layout_width="0dp"
  30. android:layout_height="wrap_content"
  31. android:layout_gravity="left|center_vertical"
  32. android:layout_weight="1"
  33. android:textColor="@color/white"
  34. android:text="Macro Name"/>
  35.  
  36. <EditText
  37. android:id="@+id/setup_macroname"
  38. android:layout_width="0dp"
  39. android:layout_height="wrap_content"
  40. android:layout_gravity="right"
  41. android:layout_weight="1"
  42. android:maxLength="12"
  43. android:inputType="textCapCharacters"
  44. android:singleLine="true"/>
  45. </LinearLayout>
  46.  
  47. <View
  48. android:layout_width="fill_parent"
  49. android:layout_height="20dp"/>
  50.  
  51. <ListView
  52. android:id="@+id/lvMacroList"
  53. android:layout_width="fill_parent"
  54. android:layout_height="wrap_content"
  55. />
  56.  
  57. <!-- Save & Cancel button -->
  58. <LinearLayout
  59. android:layout_width="match_parent"
  60. android:layout_height="wrap_content"
  61. android:paddingLeft="20dp"
  62. android:paddingRight="20dp"
  63. android:layout_gravity="bottom"
  64. android:orientation="horizontal">
  65.  
  66. <Button
  67. android:id="@+id/setup_macroSavebtn"
  68. android:layout_width="0dp"
  69. android:layout_height="wrap_content"
  70. android:layout_gravity="left"
  71. android:layout_weight="1"
  72. android:onClick="onSaveButtonClick"
  73. android:text="Save"/>
  74.  
  75. <Button
  76. android:id="@+id/setup_macroCancelbtn"
  77. android:layout_width="0dp"
  78. android:layout_height="wrap_content"
  79. android:layout_gravity="right"
  80. android:layout_weight="1"
  81. android:onClick="onCancelButtonClick"
  82. android:text="Cancel"/>
  83. </LinearLayout>
  84. </LinearLayout>
  85. </ScrollView>

解决方法

将包含按钮的LinearLayout的Parent设置为relative,并将包含两个按钮的线性布局的属性设置为android:layout_alignParentBottom =“true”.您还可以将“线性布局”的重力设置为“底部”.

试试这个:

  1. <RelativeLayout
  2. android:layout_width="match_parent"
  3. android:layout_height="match_parent"
  4. android:orientation="vertical" >
  5.  
  6. <LinearLayout
  7. android:layout_width="match_parent"
  8. android:layout_height="wrap_content"
  9. android:orientation="vertical">
  10. <!-- heading for basic settings -->
  11. <TextView
  12. android:id="@+id/setup_macroheading"
  13. style="@style/txtHeading" />
  14.  
  15. <View
  16. android:layout_width="fill_parent"
  17. android:layout_height="20dp"/>
  18.  
  19. <!-- macro name -->
  20. <LinearLayout
  21. android:layout_width="match_parent"
  22. android:layout_height="wrap_content"
  23. android:paddingLeft="20dp"
  24. android:paddingRight="20dp"
  25. android:orientation="horizontal">
  26.  
  27. <TextView
  28. android:layout_width="0dp"
  29. android:layout_height="wrap_content"
  30. android:layout_gravity="left|center_vertical"
  31. android:layout_weight="1"
  32. android:textColor="@color/white"
  33. android:text="Macro Name"/>
  34.  
  35. <EditText
  36. android:id="@+id/setup_macroname"
  37. android:layout_width="0dp"
  38. android:layout_height="wrap_content"
  39. android:layout_gravity="right"
  40. android:layout_weight="1"
  41. android:maxLength="12"
  42. android:inputType="textCapCharacters"
  43. android:singleLine="true"/>
  44. </LinearLayout>
  45.  
  46. <View
  47. android:layout_width="fill_parent"
  48. android:layout_height="20dp"/>
  49.  
  50. <ListView
  51. android:id="@+id/lvMacroList"
  52. android:layout_width="fill_parent"
  53. android:layout_height="wrap_content"
  54. />
  55. </LinearLayout>
  56. <!-- Save & Cancel button -->
  57.  
  58. <LinearLayout
  59. android:layout_width="match_parent"
  60. android:layout_height="wrap_content"
  61. android:layout_alignParentBottom="true"
  62. android:layout_gravity="bottom"
  63. android:orientation="horizontal"
  64. android:paddingLeft="20dp"
  65. android:paddingRight="20dp" >
  66.  
  67. <Button
  68. android:id="@+id/setup_macroSavebtn"
  69. android:layout_width="0dp"
  70. android:layout_height="wrap_content"
  71. android:layout_gravity="left"
  72. android:layout_weight="1"
  73. android:onClick="onSaveButtonClick"
  74. android:text="Save"/>
  75.  
  76. <Button
  77. android:id="@+id/setup_macroCancelbtn"
  78. android:layout_width="0dp"
  79. android:layout_height="wrap_content"
  80. android:layout_gravity="right"
  81. android:layout_weight="1"
  82. android:onClick="onCancelButtonClick"
  83. android:text="Cancel"/>
  84. </LinearLayout>
  85. </RelativeLayout>

猜你在找的Android相关文章