(^o^)/~嗨喽,咸鱼的我又来献丑了,今天跟大家分享的是Android布局中的相对布局(relativelayout);这是一个很好用的布局,相对与其他的布局来说,这个很值得用得熟练O(∩_∩)O哈哈~ 言归正传 今天的题目就是这个了,用相对布局来完成, 首先定义一个5个小Button,设置其内容加ID
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="1" android:id="@+id/b_re_1" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="2" android:id="@+id/b_re_2" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="3" android:id="@+id/b_re_3" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="4" android:id="@+id/b_re_4" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="5" android:id="@+id/b_re_5" />加图
设置第二个的属性
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/b_re_1" android:layout_below="@id/b_re_1" android:text="2" android:id="@+id/b_re_2" />通过layout_toRightOf属性来设置它在id为b_re_1的右边,然后再通过layout_below来设置b_re_2控件顶部在b_re_1控件的下面 咳咳,应为其他控件跟b_re_1控件重叠在一起了,所以看的是这个样子, 然后我们来处理第三个b_re_3控件,相对与b_re_3控件来说,他在b_re_2控件的左边的上面然后让我们来写
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/b_re_2" android:layout_above="@id/b_re_2" android:text="3" android:id="@+id/b_re_3" />还是通过layout_toRightOf属性来设置把他放到b_re_2控件左边,因为他是默认在第一排,但是我在这写了layout_above属性,没不要,写了和没写没区别
好了,第三个也排好了排第四个 通过b_re_2控件设置b_re_4控件layout_toLeftOf与layout_below属性,从而将它设置在b_re_2控件的左下角
代码
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toLeftOf="@id/b_re_2" android:layout_below="@id/b_re_2" android:text="4" android:id="@+id/b_re_4" />效果图 然后我们来处理我们的最后一个控件,b_re_5控件我们到现在还是没动,现在就要动他了,还是主要的还是b_re_2控件,根据b_re_2控件 代码
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@id/b_re_2" android:layout_below="@id/b_re_2" android:text="5" android:id="@+id/b_re_5" />效果图 这就是这道简单题目的答题过程了,O(∩_∩)O哈哈~
通过上面的解题,在扔一个练习题