如何在相对布局中水平等距放置视图?

How to place views equidistant horizontally in relative layout?

如何在 relative layout 中水平等距放置视图?

我想根据添加的视图数量将视图放置在相对布局中占据相等的space,类似于Linear Layout

中的weight

布局我试过:(三视图)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!--<View
        android:id="@+id/root"
        android:layout_width="64dp"
        android:layout_height="64dp"
        android:layout_centerHorizontal="true"
        android:layout_margin="16dp"
        android:background="@android:color/holo_blue_bright"/>-->

    <RelativeLayout
        android:id="@+id/left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true">

        <View
            android:layout_width="64dp"
            android:layout_height="64dp"
            android:layout_centerHorizontal="true"
            android:layout_margin="16dp"
            android:background="@android:color/holo_blue_bright"/>

    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/middle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toEndOf="@id/left">

        <View
            android:layout_width="64dp"
            android:layout_height="64dp"
            android:layout_centerHorizontal="true"
            android:layout_margin="16dp"
            android:background="@android:color/holo_blue_bright"/>

    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toEndOf="@id/middle"
        android:layout_alignParentEnd="true">

        <View
            android:layout_width="64dp"
            android:layout_height="64dp"
            android:layout_centerHorizontal="true"
            android:layout_margin="16dp"
            android:background="@android:color/holo_blue_bright"/>

    </RelativeLayout>

</RelativeLayout>

上述布局的问题是前两个视图像 wrap_content,第三个占据了所有剩余的 space。如何让每个视图占据横向的三分之一space?

注:
1.请仅提供相对布局的解决方案,而不是linear layoutconstraint layout
2.要求的解决方案不能嵌套Linear或其他布局。 (想要平面布局)

如果你想坚持相对布局,你可以这样做,像这样获取屏幕宽度。(需要获取像素值)

 private int Screendp(){
    DisplayMetrics dm = new DisplayMetrics();

    WindowManager windowManager = (WindowManager) this.getSystemService(WINDOW_SERVICE);
    windowManager.getDefaultDisplay().getMetrics(dm);
    int widthInDP = Math.round(dm.widthPixels / dm.density);
    return widthInDP;
}

这就是我们如何将 dp 转换为 px

  public int dpToPx(int dp) {

    float density = this.getResources()
            .getDisplayMetrics()
            .density;
    return Math.round((float) dp * density);
}

因此,在您的 activity 中,为每个子视图设置 llayoutparams,如下所示,

parent= findViewById(R.id.parent);
    left= findViewById(R.id.left );
    middle= findViewById(R.id.middle);
    right= findViewById(R.id.right );



    RelativeLayout.LayoutParams leftparams = new RelativeLayout.LayoutParams(  dpToPx(64), dpToPx(64));
    leftparams.leftMargin= 0;
    left.setLayoutParams(leftparams);

    RelativeLayout.LayoutParams middleparams = new RelativeLayout.LayoutParams(  dpToPx(64), dpToPx(64));
    middleparams.leftMargin=dpToPx((Screendp()/3));
    middle.setLayoutParams(middleparams);

    RelativeLayout.LayoutParams rightparams = new RelativeLayout.LayoutParams(  dpToPx(64), dpToPx(64));
    rightparams.leftMargin=dpToPx((Screendp()/3)*2);
    right.setLayoutParams(rightparams);

这是有效的,我已经试过了。