
上QQ阅读APP看书,第一时间看更新
2.1.3 相对布局
它是通过控件之间的相对位置来进行布局,比较灵活。但要注意的是,例如在指定A控件要根据B控件的位置来排列,那么要先把控件B及其id定义在A控件之前,否则会报错:

可以看到,在这里分别给3个TextView定义了id,然后TextView2和TextView3分别通过属性android:layout_below和android:layout_toRightof来以TextView1作为参照物来排列,也就是它们都是相对TextView1来排列的,布局效果如图2.3所示。

图2.3 RelativeLayout的布局效果
所以可以明显看到TextView2排在TextView1的下面,而TextView3则排在TextView1的右边。除了属性android:layout_below一类和android:layout_toRightof一类外,还有属性android:layout_aliginParentLeft一类也是可以设置放置的位置,该属性是相对父布局来排列的。