android – 如何为包含的布局设置权重?

前端之家收集整理的这篇文章主要介绍了android – 如何为包含的布局设置权重?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有两个xml文件,页眉和页脚,如下所示

header.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="wrap_content"
  5. android:orientation="vertical" >
  6.  
  7. <LinearLayout
  8. android:layout_width="match_parent"
  9. android:layout_height="wrap_content"
  10. android:background="#9d1246" >
  11.  
  12. <Button
  13. android:id="@+id/button_backfromreg"
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:layout_marginBottom="5dp"
  17. android:layout_marginLeft="20dp"
  18. android:layout_marginTop="10dp"
  19. android:background="@drawable/backbtn"
  20. android:visibility="visible" />
  21. </LinearLayout>
  22.  
  23. </LinearLayout>

footer.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="wrap_content"
  5. android:orientation="vertical" >
  6.  
  7. <LinearLayout
  8. android:layout_width="match_parent"
  9. android:layout_height="wrap_content"
  10. android:background="#9d1246" >
  11. </LinearLayout>
  12.  
  13. </LinearLayout>

我在mainlayout中包含了上面两个布局,如图所示

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:background="#eaedf2"
  6. android:orientation="vertical" >
  7.  
  8. <include
  9. android:layout_weight="1"
  10. layout="@layout/header" />
  11.  
  12. <LinearLayout
  13. android:layout_width="match_parent"
  14. android:layout_height="0dp"
  15. android:layout_margin="30dp"
  16. android:layout_weight="1"
  17. android:orientation="vertical" >
  18.  
  19. <EditText
  20. android:id="@+id/editText1"
  21. android:layout_width="match_parent"
  22. android:layout_height="wrap_content"
  23. android:editable="false"
  24. android:ems="10"
  25. android:hint="Country"
  26. android:textColor="#655e5e"
  27. android:textSize="15sp" >
  28.  
  29. <requestFocus />
  30. </EditText>
  31.  
  32. <EditText
  33. android:id="@+id/editText2"
  34. android:layout_width="match_parent"
  35. android:layout_height="wrap_content"
  36. android:editable="false"
  37. android:ems="10"
  38. android:hint="State"
  39. android:textColor="#655e5e"
  40. android:textSize="15sp" />
  41.  
  42. <EditText
  43. android:id="@+id/editText3"
  44. android:layout_width="match_parent"
  45. android:layout_height="wrap_content"
  46. android:editable="false"
  47. android:ems="10"
  48. android:hint="Place Keyword"
  49. android:textColor="#655e5e"
  50. android:textSize="15sp" />
  51. </LinearLayout>
  52.  
  53. <include
  54. android:layout_height="0dp"
  55. android:layout_weight="1"
  56. layout="@layout/footer" />
  57.  
  58. </LinearLayout>

我的问题是,使用上面的布局我看不到页脚.有人请帮助我.
提前致谢.

解决方法

将包含的布局保留在容器中,并仅将权重属性设置为该容器.

这是您更新的布局

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:background="#eaedf2"
  6. android:orientation="vertical" >
  7.  
  8. <LinearLayout
  9. android:layout_width="match_parent"
  10. android:layout_height="0dp"
  11. android:layout_weight="1" >
  12.  
  13. <include
  14.  
  15. android:layout_height="match_parent"
  16. android:layout_width="match_parent"
  17. layout="@layout/header" />
  18. </LinearLayout>
  19.  
  20. <LinearLayout
  21. android:layout_width="match_parent"
  22. android:layout_height="0dp"
  23. android:layout_margin="30dp"
  24. android:layout_weight="1"
  25. android:orientation="vertical" >
  26.  
  27. <EditText
  28. android:id="@+id/editText1"
  29. android:layout_width="match_parent"
  30. android:layout_height="wrap_content"
  31. android:editable="false"
  32. android:ems="10"
  33. android:hint="Country"
  34. android:textColor="#655e5e"
  35. android:textSize="15sp" >
  36.  
  37. <requestFocus />
  38. </EditText>
  39.  
  40. <EditText
  41. android:id="@+id/editText2"
  42. android:layout_width="match_parent"
  43. android:layout_height="wrap_content"
  44. android:editable="false"
  45. android:ems="10"
  46. android:hint="State"
  47. android:textColor="#655e5e"
  48. android:textSize="15sp" />
  49.  
  50. <EditText
  51. android:id="@+id/editText3"
  52. android:layout_width="match_parent"
  53. android:layout_height="wrap_content"
  54. android:editable="false"
  55. android:ems="10"
  56. android:hint="Place Keyword"
  57. android:textColor="#655e5e"
  58. android:textSize="15sp" />
  59. </LinearLayout>
  60.  
  61. <LinearLayout
  62. android:layout_width="match_parent"
  63. android:layout_height="0dp"
  64. android:layout_weight="1" >
  65.  
  66. <include
  67. android:layout_height="match_parent"
  68. android:layout_width="match_parent"
  69. layout="@layout/footer" />
  70. </LinearLayout>
  71.  
  72. </LinearLayout>

猜你在找的Android相关文章