对于Android开发来说布局说简单很简单,说难也挺难的,简单总结一些常用的技巧或者方法吧(持续更行)
tip 1: layout_weight的妙用
在有些场景下,比方说
场景一:我们在屏幕下方的一个linearlayout中摆了四个button,我们希望无论什么情况下,这四个button能恰好冲满屏幕,每个button的width都是wrap_content,默认情况下可能四个button只占了屏幕一部分,我们通过设置每个button的layout_weight都等于1就可以使四个button均匀充满屏幕。
场景二:我们在屏幕下方摆了两个控件,其中我们希望控件A就符号自己大小即可,而控件B希望充满其余控件,这样我们只要设置控件B的layout_width为1即可
tip 2:让某个元素位于下方
这个很简单的一个实现方式是父容器为RelativeLayout 设置 android:layout_alignParentBottom="true" 即可,当然可以用上边的layout_weight结合linearlayout实现
tip 3:与某元素进行对齐
在RelativeLayout下有
android:layout_alignTop 本元素的上边缘和某元素的的上边缘对齐
android:layout_alignLeft 本元素的左边缘和某元素的的左边缘对齐
android:layout_alignBottom 本元素的下边缘和某元素的的下边缘对齐
android:layout_alignRight 本元素的右边缘和某元素的的右边缘对齐
后边加入要对齐元素的id即可