3.2.2 RelativeLayout(相对布局)
标签: StudyNote
本文声明:
本文由Coder-pig编写,想了解其他内容,可见CoderPig’s Android Study Note——目录
尊重作者劳动成果,未经本人授权,禁止转载!违者必究!
目录源地址:http://blog.csdn.net/coder_pig/article/details/51348769
1.相关属性:
2.根据父容器定位与兄弟组件定位:
兄弟组件是处于同一层次容器的控件,比如组件1,2;另外根据兄弟组件定位的属性,
如果参考的对象不是兄弟组件,是会报错的,比如:组件3设置:
android:layout_toLeftOf=”组件1”,就不行啦。
3.margin与padding的区别
区分下:
margin代表的是偏移,比如marginLeft=”5dp”,表示组件离容器左边缘偏移5dp;
而padding代表的则是填充,而填充的对象针对的是组件中的元素,比如TextView中的文字
比如为TextView设置paddingLeft=”5dp”,则是在组件里的元素的左边填充5dp的空间!
margin针对的是容器中的组件,而padding针对的是组件中的元素,要区分开来!
还有一点很容易被遗忘的是:margin可以设置成负数!比如下面这种进入弹广告的页面,
右上角的退出按钮的定位就可以用到负数的margin
简单的布局示例代码如下:
<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"
tools:context="com.jay.example.relativelayoutdemo.MainActivity"
android:background="#00CCCCFF">
<ImageView
android:id="@+id/img_back"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_centerInParent="true"
android:background="@drawable/myicon" />
<ImageView
android:id="@+id/img_cancle"
android:layout_width="28dp"
android:layout_height="28dp"
android:layout_alignRight="@id/img_back"
android:layout_alignTop="@id/img_back"
android:background="@drawable/cancel"
android:layout_marginTop="-15dp"
android:layout_marginRight="-10dp" />
</RelativeLayout>
常用的大概就这些,觉得有遗漏的欢迎反馈,谢谢~