简体   繁体   中英

How to relate two views of RelativeLayout in Android Studio?

I created a project using RelativeLayout , and I find that most of my views are related to the parent.

All I want is to relate two corresponding views for eg 'rate the TV series below' and the rating bar(along with the parent).

Is it possible in the existing project or should I have to do it all over again?

在此处输入图片说明

You could nest another layout inside of your RelativeLayout like a LinearLayout or another RelativeLayout then put the two sibling views inside of that. Or you could use android:layout_above to link the two together inside of the RelativeLayout.

In order to relate two views in a RelativeLayout you can declare it in the element's layout.

eg in rating's bar XML layout set: android:layout_below="@id/rating_textview"
(rating_textview is the ID of your textview)

More options: https://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams

You can relate and position any view by their id's while using RelativeLayout .

For example:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/tv1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"/>

    <TextView
        android:id="@+id/tv2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_below="@id/tv1"/>

</RelativeLayout>

This sample layout will show two TextView , both centered related to their parent, and placed one below the other.

There are many parameters that can be used to place the views on this layout, as the following examples taken from the docs:

Some of the many layout properties available to views in a RelativeLayout include:

  • android:layout_alignParentTop If "true", makes the top edge of this view match the top edge of the parent.
  • android:layout_centerVertical If "true", centers this child vertically within its parent.
  • android:layout_below Positions the top edge of this view below the view specified with a resource ID.
  • android:layout_toRightOf Positions the left edge of this view to the right of the view specified with a resource ID.

You can read more about those parameters here .

I adivise you to read the docs about RelativeLayout to check how it works.

Now, it's better if you take a look at another layout manager known as ConstraintLayout , it is a bit like the RelativeLayout , but it's way more powerful. Take a look in here to see how it works.

Also, this site shows how you can mimic other layouts behavior by using ConstraintLayout , check it out!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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