Android布局之RelativeLayout点滴

继续学习布局中的RelativeLayout,相对布局

此布局中的VIEW位置都是相对的,可以相对于父VIEW,也可以相对于其他同级VIEW。

网上摘抄一下属性如下:

  a)、第一类:属性值为true或false
  android:layout_centerHrizontal 水平居中
  android:layout_centerVertical 垂直居中
  android:layout_centerInparent 相对于父元素完全居中
  android:layout_alignParentBottom 贴紧父元素的下边缘
  android:layout_alignParentLeft 贴紧父元素的左边缘
  android:layout_alignParentRight 贴紧父元素的右边缘
  android:layout_alignParentTop 贴紧父元素的上边缘  

  b)、第二类:属性值必须为id的引用名“@id/id-name
  android:layout_below 在某元素的下方
  android:layout_above 在某元素的的上方
  android:layout_toLeftOf 在某元素的左边
  android:layout_toRightOf 在某元素的右边
  android:layout_alignTop 本元素的上边缘和某元素的的上边缘对齐
  android:layout_alignLeft 本元素的左边缘和某元素的的左边缘对齐
  android:layout_alignBottom 本元素的下边缘和某元素的的下边缘对齐
  android:layout_alignRight 本元素的右边缘和某元素的的右边缘对齐

  c)、第三类:属性值为具体的像素值,如30dip,40px
  android:layout_marginBottom 离某元素底边缘的距离
  android:layout_marginLeft 离某元素左边缘的距离
  android:layout_marginRight 离某元素右边缘的距离
  android:layout_marginTop 离某元素上边缘的距离


还是先简单一个示例

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/common_title_container"
    android:layout_width="fill_parent"
    android:layout_height="48dp"
    android:background="#ffffff"
    android:gravity="center_vertical" 
    >
    
    
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="button1" />
    <Button 
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="button2" />
    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="button3" />
    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="button4" />
    <Button
        android:id="@+id/button5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="button5" />
</RelativeLayout>    
    


新建5个按钮,未进行任何属性设置,我们可以清楚看到5个按钮都在重叠在左上角,这就是相对布局,必须对相对布局中的每个VIEW设置相对属性才能准确控制每个VIEW的位置,达到灵活的效果。

我们使用几个属性试试

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/common_title_container"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffffff"
    android:gravity="center_vertical" 
    >
    
    
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="button1"
        android:layout_centerInParent="true" />
    <Button 
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="button2"
        android:layout_above="@id/button1"
        android:layout_alignLeft="@id/button1" />
    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="button3"
        android:layout_below="@id/button1" />
    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/button1"
        android:layout_above="@id/button1"
        android:text="button4" />
    <Button
        android:id="@+id/button5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/button1"
        android:layout_below="@id/button1"
        android:text="button5" />
</RelativeLayout>    


button1:我们使用了android:layout_centerInParent="true" ,代表在父VIEW的中间显示

button2:android:layout_above="@id/button1"和android:layout_alignLeft="@id/button1",代表在button1的上边,且左边缘和button1对齐

button3:andoird:layout_below="@id/button1",代表在button1的下边,且默认居左对齐

button4:android:layout_toRightOf="@id/button1"和android:layout_above="@id/button1",代表在button1的上边,且右边缘和button1对齐

button5:android:layout_toRightOf="@id/button1"和android:layout_below="@id/button1",代表在button1的下边,且右边缘和button1对齐

其他属性自行尝试效果吧。


最终示例,我们见很多app标题栏为左右个一个按钮,首页、返回,中间为app title,我们使用相对布局尝试下该效果。

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/common_title_container"
    android:layout_width="fill_parent"
    android:layout_height="50dip"
    android:background="#ff00ff"
    android:gravity="center_vertical" >
    
    <TextView
        android:id="@+id/tvBack"
        android:layout_width="wrap_content"
        android:layout_height="20dip"
        android:layout_alignParentLeft="true"
        android:layout_marginTop="5dip"
        android:layout_marginLeft="5dip"
        android:text="back"
        android:layout_centerVertical="true" />
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/tvBack"
        android:layout_toLeftOf="@+id/tvHome"
        android:gravity="center_horizontal"
        android:layout_marginTop="5dip">
        
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="this is title"
        android:textColor="#ffff00"
        android:textSize="30dip" />
    </LinearLayout>
    

    <TextView
        android:id="@+id/tvHome"
        android:text="Home"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:layout_alignParentRight="true"
        android:layout_marginRight="5dip"
        android:layout_marginTop="5dip"
        android:layout_centerVertical="true"/>
        
</RelativeLayout>

效果如下:


Python网络爬虫与推荐算法新闻推荐平台:网络爬虫:通过Python实现新浪新闻的爬取,可爬取新闻页面上的标题、文本、图片、视频链接(保留排版) 推荐算法:权重衰减+标签推荐+区域推荐+热点推荐.zip项目工程资源经过严格测试可直接运行成功且功能正常的情况才上传,可轻松复刻,拿到资料包后可轻松复现出一样的项目,本人系统开发经验充足(全领域),有任何使用问题欢迎随时与我联系,我会及时为您解惑,提供帮助。 【资源内容】:包含完整源码+工程文件+说明(如有)等。答辩评审平均分达到96分,放心下载使用!可轻松复现,设计报告也可借鉴此项目,该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的。 【提供帮助】:有任何使用问题欢迎随时与我联系,我会及时解答解惑,提供帮助 【附带帮助】:若还需要相关开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步 【项目价值】:可用在相关项目设计中,皆可应用在项目、毕业设计、课程设计、期末/期中/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面,可借鉴此优质项目实现复刻,设计报告也可借鉴此项目,也可基于此项目来扩展开发出更多功能 下载后请首先打开README文件(如有),项目工程可直接复现复刻,如果基础还行,也可在此程序基础上进行修改,以实现其它功能。供开源学习/技术交流/学习参考,勿用于商业用途。质量优质,放心下载使用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值