Android基础_页面布局_RelativeLayout(相对布局)
来源:互联网 发布:cos圈滥交知乎 编辑:程序博客网 时间:2024/06/06 17:56
Android相对布局应该是应用最为广泛的布局。这种方式下的布局对不同分辨率的屏幕适应性较好。
顾名思义,相对布局就是每个控件的位置都是相对于另一个控件而得来的。就是物理中的我们需要参照物。根据不同的参照物我们可以很灵活地设置控件到自己想要的位置。
不同的参照物
一、相对于给定ID的控件
android:layout_above 将此控件放置到给定ID的控件之上
android:layout_below 将此控件放置到给定ID的控件之下
android:layout_toLeftOf 将此控件的右边缘和给定ID控件的左边缘对齐(在给定ID控件的左侧)
android:layout_toRightOf 将此控件的左边缘和给定ID控件的右边缘对齐(在给定ID控件的右侧)
android:layout_alignBaseline 将此控件的baseline与给定ID的baseline对齐
android:layout_alignTop 将此控件的顶部和给定ID的顶部边缘对齐
android:layout_alignButtom 将此控件的底部边缘和给定ID的底部边缘对齐
android:layout_alignLeft 将此控件的左边缘和给定ID的左边缘对齐
android:layout_alignRight 将此控件的右边缘和给定ID的右边缘对齐
二、相对于父组件
android:layout_alignParentTop 如果为true,将此控件的顶部和父控件顶部对齐
android:layout_alignParentButtom 如果为true,将此控件的底部和父控件底部对齐
android:layout_alignParentLeft 如果为true,将此控件的左边缘和父控件的左边缘对齐
android:layout_alignParentRight 如果为true,将此控件的右边缘和父控件的右边缘对齐
三、设置居中
android:layout_centerHorizontal 如果为true,将此控件设置水平居中
android:layout_centerVertical 如果为true,将此控件设置垂直居中
android:layout_centerInParent 如果为true,将此空间设置为父控件的中央
下面给一个我画的解锁的小demo。
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#00BFFF" android:orientation="vertical" > <!-- 数字5 --> <Button style="@style/PasswordButton" android:id="@+id/xml_button_5" android:layout_alignLeft="@+id/xml_button_2" android:layout_alignTop="@+id/xml_button_6" android:background="@drawable/circle" android:text="@string/number_5" /> <!--数字4 --> <Button style="@style/PasswordButton"android:id="@+id/xml_button_4"android:layout_centerVertical="true"android:layout_toLeftOf="@id/xml_button_5"android:layout_margin="15dp"android:background="@drawable/circle"android:text="@string/number_4"/> <!-- 数字6 --> <Button style="@style/PasswordButton"android:id="@+id/xml_button_6"android:layout_centerVertical="true"android:layout_toRightOf="@id/xml_button_5"android:layout_margin="15dp"android:background="@drawable/circle"android:text="@string/number_6"/> <!-- 数字2 --> <Button style="@style/PasswordButton"android:id="@+id/xml_button_2"android:layout_centerHorizontal="true"android:layout_above="@id/xml_button_5"android:layout_margin="15dp"android:background="@drawable/circle"android:text="@string/number_2"/> <!-- 数字1 --> <Button style="@style/PasswordButton"android:id="@+id/xml_button_1"android:layout_above="@id/xml_button_4"android:layout_alignLeft="@id/xml_button_4"android:background="@drawable/circle"android:text="@string/number_1"/> <!-- 数字3 --> <Button style="@style/PasswordButton"android:id="@+id/xml_button_3"android:layout_above="@id/xml_button_6"android:layout_alignLeft="@id/xml_button_6"android:background="@drawable/circle"android:text="@string/number_3"/> <!-- 数字7 --> <Button style="@style/PasswordButton"android:id="@+id/xml_button_7"android:layout_below="@id/xml_button_4"android:layout_alignLeft="@id/xml_button_4"android:background="@drawable/circle"android:text="@string/number_7"/> <!-- 数字8 --> <Button style="@style/PasswordButton"android:id="@+id/xml_button_8"android:layout_alignLeft="@id/xml_button_5"android:layout_alignTop="@id/xml_button_7"android:background="@drawable/circle"android:text="@string/number_8"/> <!-- 数字9 --> <Button style="@style/PasswordButton"android:id="@+id/xml_button_9"android:layout_below="@id/xml_button_6"android:layout_alignLeft="@id/xml_button_6"android:background="@drawable/circle"android:text="@string/number_9"/> <!-- 数字0 --> <Button style="@style/PasswordButton"android:id="@+id/xml_button_0"android:layout_centerHorizontal="true"android:layout_below="@id/xml_button_8"android:layout_margin="15dp"android:background="@drawable/circle"android:text="@string/number_0"/> <!-- 清空 --> <Button style="@style/PasswordButton"android:id="@+id/xml_clear"android:layout_alignLeft="@id/xml_button_9"android:layout_alignTop="@id/xml_button_0"android:background="@drawable/circle"android:text="@string/clear"/> <!-- 修改密码 --> <Button style="@style/PasswordButton"android:id="@+id/xml_changePassword"android:layout_alignLeft="@id/xml_button_7"android:layout_alignTop="@id/xml_button_0"android:background="@drawable/circle"android:text="@string/changePassword"/> <!-- 密码输入框 --> <EditText android:id="@+id/xml_passwordEdit" android:layout_width="150dp" android:layout_height="40dp" android:layout_centerHorizontal="true" android:layout_above="@id/xml_button_2" android:layout_margin="20dp" android:gravity="center" android:textColor="#FFFFFF" android:enabled="false" android:inputType="numberPassword"/> <!-- 确认输入--> <Button android:id="@+id/xml_button_OK" android:layout_width="wrap_content" android:layout_height="40dp" android:layout_toRightOf="@id/xml_passwordEdit" android:layout_alignTop="@id/xml_passwordEdit" android:background="#00BFFF" android:text="@string/OK"/> <!-- 提示框 --> <TextView android:id="@+id/xml_textView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_above="@id/xml_passwordEdit" android:gravity="center" android:layout_margin="10dp"/> <!-- 请求解锁 --> <Button android:id="@+id/xml_removelock_button" android:layout_width="wrap_content" android:layout_height="40dp" android:layout_below="@id/xml_button_0" android:layout_centerHorizontal="true" android:visibility="gone"/> <!-- 请求重置密码 --> <Button android:id="@+id/xml_removepassword_button" android:layout_width="wrap_content" android:layout_height="40dp" android:layout_below="@id/xml_removelock_button" android:layout_centerHorizontal="true" android:visibility="gone"/> </RelativeLayout>
效果如下:
- Android基础_页面布局_RelativeLayout(相对布局)
- Android布局一_RelativeLayout
- Android布局一_RelativeLayout
- Android基础_页面布局_LinearLayout(线性布局)
- Android基础_页面布局_TableLayout(表格布局)
- Android 基础 RelativeLayout相对布局
- Android基础_页面布局_碎片(Fragment)
- Android 布局:相对布局
- 【Android基础】页面布局
- Android之旅_相对布局
- 04、Android开发基础之相对布局
- android布局之相对布局
- Android简单布局-------相对布局
- android布局学习--相对布局
- Android布局之相对布局
- Android布局(相对布局)
- android相对布局 相对对齐
- android之相对布局
- C++的string实现MFC的CString::GetBuffer
- ACM_归并排序算法
- javascript 切换按钮改变div颜色
- bzoj 2151 种树(贪心+堆)(经典)
- Android Studio安装以及Fetching android sdk component information超时的解决方案
- Android基础_页面布局_RelativeLayout(相对布局)
- 科普:Windows下Netcat使用手册
- 自己收藏的Android开发的知识点各种传送门 持续更新中~~
- 自动化快速实现Parcelable接口序列化
- GRE写作必备句型
- HDU 2874 LCA离线算法 tarjan算法
- POJ 1905 Expanding Rods
- 计算两个日期间的天数
- chrome 一进入调试页面就会自动打断点