Android:如何使RelativeLayout元素具有相同的尺寸和全宽

发布于 2025-01-15 17:42:17 字数 6478 浏览 2 评论 0原文

我使用相对布局来删除约束布局之间的“粗体”栏(星星所在的位置),但我无法定位这些视图,以便它们占据整个屏幕并且大小相同。 如果有其他方法可以去除油腻的条纹,那就太好了。

但我不会拒绝寻找元素大小的解决方案

我的代码:

<?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>

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

莫相离 2025-01-22 17:42:17

如果您知道确切的单元格数量,则可以对每个元素使用 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 example

Linear 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文