RelativeLayout在ConstraintLayout中定位不好。

huangapple go评论96阅读模式
英文:

RelativeLayout doesn't locate well in ConstraintLayout

问题

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <androidx.constraintlayout.widget.ConstraintLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="wrap_content"
  7. android:id="@+id/relativeLayout"
  8. xmlns:app="http://schemas.android.com/apk/res-auto"
  9. android:paddingTop="25dp"
  10. android:paddingBottom="25dp"
  11. android:paddingLeft="10dp"
  12. android:paddingRight="10dp">
  13. <RelativeLayout
  14. android:id="@+id/container_main"
  15. android:paddingBottom="16dp"
  16. android:background="@drawable/card_bg"
  17. android:layout_width="0dp"
  18. android:layout_height="match_parent"
  19. android:layout_margin="20dp"
  20. app:layout_constraintBottom_toBottomOf="parent"
  21. app:layout_constraintEnd_toEndOf="parent"
  22. app:layout_constraintStart_toStartOf="parent">
  23. <!-- ... Your TextView and other views within this RelativeLayout ... -->
  24. </RelativeLayout>
  25. <RelativeLayout
  26. android:id="@+id/LeftRight"
  27. android:layout_width="match_parent"
  28. android:layout_height="wrap_content"
  29. app:layout_constraintTop_toBottomOf="@+id/container_main"
  30. app:layout_constraintBottom_toTopOf="@+id/adView">
  31. <!-- ... Your ImageView views within this RelativeLayout ... -->
  32. </RelativeLayout>
  33. <com.google.android.gms.ads.AdView
  34. xmlns:ads="http://schemas.android.com/apk/res-auto"
  35. android:id="@+id/adView"
  36. android:layout_width="wrap_content"
  37. android:layout_height="wrap_content"
  38. android:layout_centerHorizontal="true"
  39. ads:layout_constraintBottom_toBottomOf="parent"
  40. ads:layout_constraintTop_toBottomOf="@id/LeftRight"
  41. ads:layout_constraintRight_toRightOf="parent"
  42. ads:layout_constraintLeft_toLeftOf="parent"
  43. ads:adSize="BANNER"
  44. ads:adUnitId="ca-app-pub-3940256099942544/6300978111">
  45. </com.google.android.gms.ads.AdView>
  46. </androidx.constraintlayout.widget.ConstraintLayout>

Please note that I have only provided the translated XML code as requested. If you have any questions or need further assistance, feel free to ask.

英文:

I think I did everything true but the RelativeLayout in ConstraintLayout don't do what i command. When i add something under the RelativeLayout it supposed to be smaller and make room to other items but it doesn't matter what i do, it always push them out of the screen.

RelativeLayout在ConstraintLayout中定位不好。

The left and right arrows should be below of RelativeLayout and adView should be below of arrows.

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  2. &lt;androidx.constraintlayout.widget.ConstraintLayout
  3. xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  4. xmlns:tools=&quot;http://schemas.android.com/tools&quot;
  5. android:layout_width=&quot;match_parent&quot;
  6. android:layout_height=&quot;wrap_content&quot;
  7. android:id=&quot;@+id/relativeLayout&quot;
  8. xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
  9. android:paddingTop=&quot;25dp&quot;
  10. android:paddingBottom=&quot;25dp&quot;
  11. android:paddingLeft=&quot;10dp&quot;
  12. android:paddingRight=&quot;10dp&quot;&gt;
  13. &lt;RelativeLayout
  14. android:id=&quot;@+id/container_main&quot;
  15. android:paddingBottom=&quot;16dp&quot;
  16. android:background=&quot;@drawable/card_bg&quot;
  17. android:layout_width=&quot;0dp&quot;
  18. android:layout_height=&quot;match_parent&quot;
  19. android:layout_margin=&quot;20dp&quot;
  20. app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
  21. app:layout_constraintEnd_toEndOf=&quot;parent&quot;
  22. app:layout_constraintStart_toStartOf=&quot;parent&quot;&gt;
  23. &lt;TextView
  24. android:textStyle=&quot;bold&quot;
  25. android:layout_width=&quot;wrap_content&quot;
  26. android:layout_height=&quot;wrap_content&quot;
  27. android:textColor=&quot;@color/title_text_color&quot;
  28. android:id=&quot;@+id/activity_main_text_view_id&quot;
  29. android:layout_alignParentStart=&quot;true&quot;
  30. android:layout_alignParentLeft=&quot;true&quot;
  31. android:layout_alignParentTop=&quot;true&quot;
  32. android:layout_marginLeft=&quot;40dp&quot;
  33. android:layout_marginStart=&quot;40dp&quot;
  34. android:layout_marginTop=&quot;20dp&quot;
  35. android:text=&quot;simple Title Text&quot;/&gt;
  36. &lt;TextView
  37. android:padding=&quot;10dp&quot;
  38. android:gravity=&quot;center&quot;
  39. android:textColor=&quot;#000000&quot;
  40. android:lineSpacingExtra=&quot;8dp&quot;
  41. android:layout_width=&quot;match_parent&quot;
  42. android:layout_height=&quot;wrap_content&quot;
  43. android:id=&quot;@+id/factTextView&quot;
  44. android:textSize=&quot;18dp&quot;
  45. android:layout_alignStart=&quot;@id/activity_main_text_view_id&quot;
  46. android:layout_alignLeft=&quot;@id/activity_main_text_view_id&quot;
  47. android:layout_alignParentLeft=&quot;true&quot;
  48. android:layout_alignParentRight=&quot;true&quot;
  49. android:layout_alignParentEnd=&quot;true&quot;
  50. android:layout_alignParentStart=&quot;true&quot;
  51. android:layout_alignParentTop=&quot;true&quot;
  52. android:layout_alignParentBottom=&quot;true&quot;
  53. android:layout_marginTop=&quot;8dp&quot;
  54. android:text=&quot;Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry&#39;s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.&quot;/&gt;
  55. &lt;TextView
  56. android:layout_width=&quot;wrap_content&quot;
  57. android:layout_height=&quot;wrap_content&quot;
  58. android:textColor=&quot;#000000&quot;
  59. android:id=&quot;@+id/activity_main_did_u_know&quot;
  60. android:layout_alignParentEnd=&quot;true&quot;
  61. android:layout_alignParentRight=&quot;true&quot;
  62. android:layout_marginEnd=&quot;16dp&quot;
  63. android:layout_marginRight=&quot;16dp&quot;
  64. android:text=&quot;Category&quot;
  65. android:layout_marginTop=&quot;20dp&quot;/&gt;
  66. &lt;ImageView
  67. android:layout_marginLeft=&quot;25dp&quot;
  68. android:id=&quot;@+id/activity_main_image_view&quot;
  69. android:layout_width=&quot;30dp&quot;
  70. android:layout_height=&quot;40dp&quot;
  71. android:layout_alignParentBottom=&quot;true&quot;
  72. android:clickable=&quot;true&quot;
  73. android:src=&quot;@drawable/ic_share_black_24dp&quot; /&gt;
  74. &lt;ImageView
  75. android:layout_marginRight=&quot;25dp&quot;
  76. android:id=&quot;@+id/activity_main_fav_button&quot;
  77. android:layout_width=&quot;30dp&quot;
  78. android:layout_height=&quot;40dp&quot;
  79. android:layout_alignParentRight=&quot;true&quot;
  80. android:layout_alignParentBottom=&quot;true&quot;
  81. android:clickable=&quot;true&quot;
  82. android:src=&quot;@drawable/ic_favorite_border_black_24dp&quot;/&gt;
  83. &lt;/RelativeLayout&gt;
  84. &lt;RelativeLayout
  85. android:id=&quot;@+id/LeftRight&quot;
  86. android:layout_width=&quot;match_parent&quot;
  87. android:layout_height=&quot;wrap_content&quot;
  88. app:layout_constraintTop_toBottomOf=&quot;@+id/container_main&quot;
  89. app:layout_constraintBottom_toTopOf=&quot;@+id/adView&quot;
  90. &gt;
  91. &lt;ImageView
  92. android:layout_marginLeft=&quot;25dp&quot;
  93. android:id=&quot;@+id/activity_main_left_button&quot;
  94. android:layout_width=&quot;30dp&quot;
  95. android:layout_height=&quot;40dp&quot;
  96. android:layout_alignParentBottom=&quot;true&quot;
  97. android:clickable=&quot;true&quot;
  98. app:layout_constraintRight_toLeftOf=&quot;@+id/activity_main_right_button&quot;
  99. android:src=&quot;@drawable/ic_left&quot; /&gt;
  100. &lt;ImageView
  101. android:id=&quot;@+id/activity_main_right_button&quot;
  102. android:layout_width=&quot;30dp&quot;
  103. android:layout_height=&quot;40dp&quot;
  104. android:layout_alignParentRight=&quot;true&quot;
  105. android:layout_alignParentBottom=&quot;true&quot;
  106. android:layout_marginRight=&quot;25dp&quot;
  107. android:clickable=&quot;true&quot;
  108. android:src=&quot;@drawable/ic_right&quot;
  109. app:layout_constraintLeft_toRightOf=&quot;@+id/activity_main_left_button&quot; /&gt;
  110. &lt;/RelativeLayout&gt;
  111. &lt;com.google.android.gms.ads.AdView
  112. xmlns:ads=&quot;http://schemas.android.com/apk/res-auto&quot;
  113. android:id=&quot;@+id/adView&quot;
  114. android:layout_width=&quot;wrap_content&quot;
  115. android:layout_height=&quot;wrap_content&quot;
  116. android:layout_centerHorizontal=&quot;true&quot;
  117. ads:layout_constraintBottom_toBottomOf=&quot;parent&quot;
  118. ads:layout_constraintTop_toBottomOf=&quot;@id/LeftRight&quot;
  119. ads:layout_constraintRight_toRightOf=&quot;parent&quot;
  120. ads:layout_constraintLeft_toLeftOf=&quot;parent&quot;
  121. ads:adSize=&quot;BANNER&quot;
  122. ads:adUnitId=&quot;ca-app-pub-3940256099942544/6300978111&quot;&gt;
  123. &lt;/com.google.android.gms.ads.AdView&gt;
  124. &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

I am open the any kind of advice. Thanks.

答案1

得分: 1

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:padding="16dp">
  7. <androidx.constraintlayout.widget.ConstraintLayout
  8. android:id="@+id/container_main"
  9. android:layout_width="match_parent"
  10. android:layout_height="wrap_content"
  11. android:layout_margin="16dp"
  12. android:background="@drawable/card_bg"
  13. app:layout_constraintEnd_toEndOf="parent"
  14. app:layout_constraintStart_toStartOf="parent"
  15. app:layout_constraintTop_toTopOf="parent">
  16. <TextView
  17. android:id="@+id/activity_main_text_view_id"
  18. android:layout_width="wrap_content"
  19. android:layout_height="wrap_content"
  20. android:layout_margin="16dp"
  21. android:text="simple Title Text"
  22. android:textColor="@color/title_text_color"
  23. android:textStyle="bold"
  24. app:layout_constraintStart_toStartOf="parent"
  25. app:layout_constraintTop_toTopOf="parent" />
  26. <TextView
  27. android:id="@+id/activity_main_did_u_know"
  28. android:layout_width="wrap_content"
  29. android:layout_height="wrap_content"
  30. android:layout_margin="16dp"
  31. android:text="Category"
  32. android:textColor="#000000"
  33. app:layout_constraintEnd_toEndOf="parent"
  34. app:layout_constraintTop_toTopOf="parent" />
  35. <TextView
  36. android:id="@+id/factTextView"
  37. android:layout_width="match_parent"
  38. android:layout_height="wrap_content"
  39. android:layout_marginTop="8dp"
  40. android:lineSpacingExtra="8dp"
  41. android:padding="10dp"
  42. android:text="Lorem Ipsum is simply dummy text..."
  43. android:textColor="#000000"
  44. android:textSize="18dp"
  45. app:layout_constraintBottom_toTopOf="@id/activity_main_image_view"
  46. app:layout_constraintEnd_toEndOf="parent"
  47. app:layout_constraintStart_toStartOf="parent"
  48. app:layout_constraintTop_toBottomOf="@id/activity_main_did_u_know" />
  49. <ImageView
  50. android:id="@+id/activity_main_image_view"
  51. android:layout_width="30dp"
  52. android:layout_height="40dp"
  53. android:layout_margin="16dp"
  54. android:clickable="true"
  55. android:src="@drawable/ic_share_black_24dp"
  56. app:layout_constraintBottom_toBottomOf="parent"
  57. app:layout_constraintStart_toStartOf="parent"
  58. app:layout_constraintTop_toBottomOf="@id/factTextView" />
  59. <ImageView
  60. android:id="@+id/activity_main_fav_button"
  61. android:layout_width="30dp"
  62. android:layout_height="40dp"
  63. android:layout_margin="16dp"
  64. android:clickable="true"
  65. android:src="@drawable/ic_favorite_border_black_24dp"
  66. app:layout_constraintBottom_toBottomOf="parent"
  67. app:layout_constraintEnd_toEndOf="parent"
  68. app:layout_constraintTop_toBottomOf="@id/factTextView" />
  69. </androidx.constraintlayout.widget.ConstraintLayout>
  70. <androidx.constraintlayout.widget.ConstraintLayout
  71. android:id="@+id/LeftRight"
  72. android:layout_width="match_parent"
  73. android:layout_height="wrap_content"
  74. app:layout_constraintBottom_toTopOf="@id/adView"
  75. app:layout_constraintEnd_toEndOf="parent"
  76. app:layout_constraintStart_toStartOf="parent"
  77. app:layout_constraintTop_toBottomOf="@+id/container_main">
  78. <ImageView
  79. android:id="@+id/activity_main_left_button"
  80. android:layout_width="30dp"
  81. android:layout_height="40dp"
  82. android:layout_margin="25dp"
  83. android:clickable="true"
  84. android:src="@drawable/ic_left"
  85. app:layout_constraintBottom_toBottomOf="parent"
  86. app:layout_constraintStart_toStartOf="parent"
  87. app:layout_constraintTop_toTopOf="parent" />
  88. <ImageView
  89. android:id="@+id/activity_main_right_button"
  90. android:layout_width="30dp"
  91. android:layout_height="40dp"
  92. android:layout_margin="25dp"
  93. android:clickable="true"
  94. android:src="@drawable/ic_right"
  95. app:layout_constraintBottom_toBottomOf="parent"
  96. app:layout_constraintEnd_toEndOf="parent"
  97. app:layout_constraintLeft_toRightOf="@+id/activity_main_left_button"
  98. app:layout_constraintTop_toTopOf="parent" />
  99. </androidx.constraintlayout.widget.ConstraintLayout>
  100. <com.google.android.gms.ads.AdView xmlns:ads="http://schemas.android.com/apk/res-auto"
  101. android:id="@+id/adView"
  102. android:layout_width="match_parent"
  103. android:layout_height="wrap_content"
  104. android:layout_centerHorizontal="true"
  105. ads:adSize="BANNER"
  106. ads:adUnitId="ca-app-pub-3940256099942544/6300978111"
  107. ads:layout_constraintBottom_toBottomOf="parent"
  108. ads:layout_constraintLeft_toLeftOf="parent"
  109. ads:layout_constraintRight_toRightOf="parent" />
  110. </androidx.constraintlayout.widget.ConstraintLayout>
英文:

I prefer to use whole Constraint Layout in your xml. Checkout below xml, hope the same what you want to design.

  1. &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  2. &lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
  3. xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
  4. android:layout_width=&quot;match_parent&quot;
  5. android:layout_height=&quot;match_parent&quot;
  6. android:padding=&quot;16dp&quot;&gt;
  7. &lt;androidx.constraintlayout.widget.ConstraintLayout
  8. android:id=&quot;@+id/container_main&quot;
  9. android:layout_width=&quot;match_parent&quot;
  10. android:layout_height=&quot;wrap_content&quot;
  11. android:layout_margin=&quot;16dp&quot;
  12. android:background=&quot;@drawable/card_bg&quot;
  13. app:layout_constraintEnd_toEndOf=&quot;parent&quot;
  14. app:layout_constraintStart_toStartOf=&quot;parent&quot;
  15. app:layout_constraintTop_toTopOf=&quot;parent&quot;&gt;
  16. &lt;TextView
  17. android:id=&quot;@+id/activity_main_text_view_id&quot;
  18. android:layout_width=&quot;wrap_content&quot;
  19. android:layout_height=&quot;wrap_content&quot;
  20. android:layout_margin=&quot;16dp&quot;
  21. android:text=&quot;simple Title Text&quot;
  22. android:textColor=&quot;@color/title_text_color&quot;
  23. android:textStyle=&quot;bold&quot;
  24. app:layout_constraintStart_toStartOf=&quot;parent&quot;
  25. app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
  26. &lt;TextView
  27. android:id=&quot;@+id/activity_main_did_u_know&quot;
  28. android:layout_width=&quot;wrap_content&quot;
  29. android:layout_height=&quot;wrap_content&quot;
  30. android:layout_margin=&quot;16dp&quot;
  31. android:text=&quot;Category&quot;
  32. android:textColor=&quot;#000000&quot;
  33. app:layout_constraintEnd_toEndOf=&quot;parent&quot;
  34. app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
  35. &lt;TextView
  36. android:id=&quot;@+id/factTextView&quot;
  37. android:layout_width=&quot;match_parent&quot;
  38. android:layout_height=&quot;wrap_content&quot;
  39. android:layout_marginTop=&quot;8dp&quot;
  40. android:lineSpacingExtra=&quot;8dp&quot;
  41. android:padding=&quot;10dp&quot;
  42. android:text=&quot;Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry&#39;s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.&quot;
  43. android:textColor=&quot;#000000&quot;
  44. android:textSize=&quot;18dp&quot;
  45. app:layout_constraintBottom_toTopOf=&quot;@id/activity_main_image_view&quot;
  46. app:layout_constraintEnd_toEndOf=&quot;parent&quot;
  47. app:layout_constraintStart_toStartOf=&quot;parent&quot;
  48. app:layout_constraintTop_toBottomOf=&quot;@id/activity_main_did_u_know&quot; /&gt;
  49. &lt;ImageView
  50. android:id=&quot;@+id/activity_main_image_view&quot;
  51. android:layout_width=&quot;30dp&quot;
  52. android:layout_height=&quot;40dp&quot;
  53. android:layout_margin=&quot;16dp&quot;
  54. android:clickable=&quot;true&quot;
  55. android:src=&quot;@drawable/ic_share_black_24dp&quot;
  56. app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
  57. app:layout_constraintStart_toStartOf=&quot;parent&quot;
  58. app:layout_constraintTop_toBottomOf=&quot;@id/factTextView&quot; /&gt;
  59. &lt;ImageView
  60. android:id=&quot;@+id/activity_main_fav_button&quot;
  61. android:layout_width=&quot;30dp&quot;
  62. android:layout_height=&quot;40dp&quot;
  63. android:layout_margin=&quot;16dp&quot;
  64. android:clickable=&quot;true&quot;
  65. android:src=&quot;@drawable/ic_favorite_border_black_24dp&quot;
  66. app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
  67. app:layout_constraintEnd_toEndOf=&quot;parent&quot;
  68. app:layout_constraintTop_toBottomOf=&quot;@id/factTextView&quot; /&gt;
  69. &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
  70. &lt;androidx.constraintlayout.widget.ConstraintLayout
  71. android:id=&quot;@+id/LeftRight&quot;
  72. android:layout_width=&quot;match_parent&quot;
  73. android:layout_height=&quot;wrap_content&quot;
  74. app:layout_constraintBottom_toTopOf=&quot;@id/adView&quot;
  75. app:layout_constraintEnd_toEndOf=&quot;parent&quot;
  76. app:layout_constraintStart_toStartOf=&quot;parent&quot;
  77. app:layout_constraintTop_toBottomOf=&quot;@+id/container_main&quot;&gt;
  78. &lt;ImageView
  79. android:id=&quot;@+id/activity_main_left_button&quot;
  80. android:layout_width=&quot;30dp&quot;
  81. android:layout_height=&quot;40dp&quot;
  82. android:layout_margin=&quot;25dp&quot;
  83. android:clickable=&quot;true&quot;
  84. android:src=&quot;@drawable/ic_left&quot;
  85. app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
  86. app:layout_constraintStart_toStartOf=&quot;parent&quot;
  87. app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
  88. &lt;ImageView
  89. android:id=&quot;@+id/activity_main_right_button&quot;
  90. android:layout_width=&quot;30dp&quot;
  91. android:layout_height=&quot;40dp&quot;
  92. android:layout_margin=&quot;25dp&quot;
  93. android:clickable=&quot;true&quot;
  94. android:src=&quot;@drawable/ic_right&quot;
  95. app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
  96. app:layout_constraintEnd_toEndOf=&quot;parent&quot;
  97. app:layout_constraintLeft_toRightOf=&quot;@+id/activity_main_left_button&quot;
  98. app:layout_constraintTop_toTopOf=&quot;parent&quot; /&gt;
  99. &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
  100. &lt;com.google.android.gms.ads.AdView xmlns:ads=&quot;http://schemas.android.com/apk/res-auto&quot;
  101. android:id=&quot;@+id/adView&quot;
  102. android:layout_width=&quot;match_parent&quot;
  103. android:layout_height=&quot;wrap_content&quot;
  104. android:layout_centerHorizontal=&quot;true&quot;
  105. ads:adSize=&quot;BANNER&quot;
  106. ads:adUnitId=&quot;ca-app-pub-3940256099942544/6300978111&quot;
  107. ads:layout_constraintBottom_toBottomOf=&quot;parent&quot;
  108. ads:layout_constraintLeft_toLeftOf=&quot;parent&quot;
  109. ads:layout_constraintRight_toRightOf=&quot;parent&quot;/&gt;
  110. &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;

For more see below view .

RelativeLayout在ConstraintLayout中定位不好。

huangapple
  • 本文由 发表于 2020年5月31日 06:41:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/62109504.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定