Android:如何使RelativeLayout元素具有相同的尺寸和全宽
我使用相对布局来删除约束布局之间的“粗体”栏(星星所在的位置),但我无法定位这些视图,以便它们占据整个屏幕并且大小相同。 如果有其他方法可以去除油腻的条纹,那就太好了。
但我不会拒绝寻找元素大小的解决方案
我的代码:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<RelativeLayout
android:id="@+id/relative_layout_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/first_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:background="@drawable/rate_registration_background_unselected_view_shape_first"
>
<ImageView
android:id="@+id/first_star"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="20dp"
android:layout_marginBottom="10dp"
android:src="@drawable/rate_registration_star_svg"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/second_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="-2dp"
android:layout_toEndOf="@+id/first_item"
android:background="@drawable/rate_registration_background_unselected_view_shape_middle"
>
<ImageView
android:id="@+id/second_star"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="20dp"
android:layout_marginBottom="10dp"
android:src="@drawable/rate_registration_star_svg"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/third_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="-2dp"
android:layout_toEndOf="@+id/second_item"
android:background="@drawable/rate_registration_background_unselected_view_shape_middle"
>
<ImageView
android:id="@+id/third_star"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="20dp"
android:layout_marginBottom="10dp"
android:src="@drawable/rate_registration_star_svg"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/fourth_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="-2dp"
android:layout_toEndOf="@+id/third_item"
android:background="@drawable/rate_registration_background_unselected_view_shape_middle"
>
<ImageView
android:id="@+id/fourth_star"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="20dp"
android:layout_marginBottom="10dp"
android:src="@drawable/rate_registration_star_svg"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/fifth_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="-2dp"
android:layout_toEndOf="@+id/fourth_item"
android:background="@drawable/rate_registration_background_unselected_view_shape_last"
>
<ImageView
android:id="@+id/fifth_star"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="20dp"
android:layout_marginBottom="10dp"
android:src="@drawable/rate_registration_star_svg"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
I'm using relativelayout to remove the "bold" bar between the constraintlayout (where the stars are), but I can't position these views so that they take up the whole screen and are the same size.
If there are other options on how to get rid of the greasy streak, that would be great.
But I will not refuse in the search for a solution to the size of the elements
My code:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<RelativeLayout
android:id="@+id/relative_layout_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/first_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:background="@drawable/rate_registration_background_unselected_view_shape_first"
>
<ImageView
android:id="@+id/first_star"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="20dp"
android:layout_marginBottom="10dp"
android:src="@drawable/rate_registration_star_svg"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/second_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="-2dp"
android:layout_toEndOf="@+id/first_item"
android:background="@drawable/rate_registration_background_unselected_view_shape_middle"
>
<ImageView
android:id="@+id/second_star"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="20dp"
android:layout_marginBottom="10dp"
android:src="@drawable/rate_registration_star_svg"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/third_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="-2dp"
android:layout_toEndOf="@+id/second_item"
android:background="@drawable/rate_registration_background_unselected_view_shape_middle"
>
<ImageView
android:id="@+id/third_star"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="20dp"
android:layout_marginBottom="10dp"
android:src="@drawable/rate_registration_star_svg"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/fourth_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="-2dp"
android:layout_toEndOf="@+id/third_item"
android:background="@drawable/rate_registration_background_unselected_view_shape_middle"
>
<ImageView
android:id="@+id/fourth_star"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="20dp"
android:layout_marginBottom="10dp"
android:src="@drawable/rate_registration_star_svg"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/fifth_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="-2dp"
android:layout_toEndOf="@+id/fourth_item"
android:background="@drawable/rate_registration_background_unselected_view_shape_last"
>
<ImageView
android:id="@+id/fifth_star"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="20dp"
android:layout_marginBottom="10dp"
android:src="@drawable/rate_registration_star_svg"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您知道确切的单元格数量,则可以对每个元素使用
android:layout_weight
。这里有一个例子Android 中的线性布局和权重
否则我建议使用这个很棒的库,有很多灵活的示例
https://github.com/google/flexbox-layout
You may use
android:layout_weight
for each of your element if you know exact number of cells. Here an exampleLinear Layout and weight in Android
Otherwise I recommend to use this great library which has a lot of flexible examples
https://github.com/google/flexbox-layout