android:layout_below
是RelativeLayout的一個(gè)屬性,其官方解釋如下:
—Positions the top edge of this view below the given anchor view ID and must be a reference to another resource, in the form "@[+][package:]type:name"
我理解就是:將當(dāng)前控件的頂部置于給定ID的控件之下,并且兩控件應(yīng)該對(duì)齊。然而事實(shí)證明我又傻傻的天真了T_T...
layout_blew.xml布局文件如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/banner_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignWithParentIfMissing="true"
android:layout_marginBottom="3dp"
android:layout_marginRight="6dp"
android:includeFontPadding="false"
android:text="東航旗艦店"
android:visibility="visible"
android:textSize="15sp"/>
<TextView
android:id="@+id/seat_space"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/banner_text"
android:layout_toRightOf="@+id/banner_text"
android:includeFontPadding="false"
android:textSize="13sp"
android:visibility="visible"
tools:text="經(jīng)濟(jì)艙"/>
<TextView
android:id="@+id/airport_price_fee"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/seat_space"
android:includeFontPadding="false"
android:textSize="12sp"
tools:text="機(jī)建0"/>
</RelativeLayout>
布局的初衷是想在同一個(gè)RelativeLayout中將“機(jī)建”放在第二欄:
剛開(kāi)始以“東航旗艦店”為參照,使用屬性android:layout_below="@+id/banner_text"
,但由于“東航旗艦店”是動(dòng)態(tài)顯示的,有時(shí)候VISIBLE,有時(shí)候GONE(此時(shí)android:layout_below
就找不到參照了,“機(jī)建”就會(huì)與第一欄重合),要實(shí)現(xiàn)正常顯示就只能以“經(jīng)濟(jì)艙”為參照,使用android:layout_below="@+id/seat_space"
。可是基于對(duì)android:layout_below
屬性的理解,用了覺(jué)得結(jié)果會(huì)是下面這樣的(其實(shí)又添加了android:layout_alignLeft
屬性才是下面的效果):
結(jié)果試了才知道真心錯(cuò)了,android:layout_below="@+id/seat_space"
,只要控件seat_space(經(jīng)濟(jì)艙)不設(shè)為GONE,也無(wú)論“經(jīng)濟(jì)艙”位置在哪兒,"機(jī)建"都會(huì)在“經(jīng)濟(jì)艙”的下面一排的最左邊(前提是“機(jī)建”沒(méi)有設(shè)置其他對(duì)齊屬性)。
總結(jié):
-
android:layout_below
屬性會(huì)將當(dāng)前控件的頂部置于給定ID的控件之下,但并不會(huì)與給定ID的控件對(duì)齊,默認(rèn)會(huì)放在父控件的最左邊; - 可通過(guò)
android:layout_alignLeft
、android:layout_alignRight
等對(duì)齊屬性改變當(dāng)前控件設(shè)置android:layout_below
屬性后的默認(rèn)位置,RelativeLayout的具體其他屬性點(diǎn)這里。