简体   繁体   English

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

[英]How to place views equidistant horizontally in relative layout?

How to place views equidistant horizontally in relative layout ? 如何在relative layout中将等距视图水平放置?

I want to place views in relative layout occupying equal space according to the number of views added, similar to weight in Linear Layout 我想根据添加的视图数将视图放置在相对布局中,占据相等的空间,类似于Linear Layout weight

Layout I tried: (For three views) 我尝试的布局:(三个视图)

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

Problem with the above layout is that the first two views are like wrap_content and the third one occupies all remaining space. 上述布局的问题在于,前两个视图就像wrap_content一样,而第三个占据了所有剩余空间。 How to make each view occupy one-third of horizontal space? 如何使每个视图占据水平空间的三分之一?

Note: 注意:
1. Kindly provide solution only in relative layout, not in linear layout or constraint layout . 1.请仅在相对布局中提供解决方案,而不linear layoutconstraint layout提供解决方案。
2. The solution required should not be nested with Linear or other layouts. 2.所需的解决方案不应与Linear或其他布局嵌套。 (Want flat layout) (想要平面布局)

If you want to stick with the relative layout you can do like this, get the the screen width like this.(need to get the pixel value) 如果要坚持使用相对布局,可以这样,获得屏幕宽度(需要获得像素值)。

 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;
}

this is how we can convert dp to px 这就是我们如何将dp转换为px

  public int dpToPx(int dp) {

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

So in your activity set l layoutparams for each and every child views like below, 因此,在您的活动中为每个子视图设置l layoutparams ,如下所示,

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);

This is working and i have tried this. 这正在工作,我已经尝试过了。

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM