Android RelativeLayout示例

在Android中, RelativeLayout可让您根据附近(相对或同级)组件的位置来放置组件。 这是最灵活的布局,可让您将组件放置在所需的任何位置(如果您知道如何“相对”放置)。

RelativeLayout ,可以使用“ ”来排列组件位置,例如,在“ button2”下显示“ button1”,或在“ button1”的右边显示“ button3”。

注意
RelativeLayout非常灵活,但是很难掌握。 建议您使用Eclipse IDE拖动组件,然后查看研究Eclipse生成的XML布局代码,以了解如何编写“相对”组件。

在本教程中,我们向您展示如何通过“ RelativeLayout ”来排列/定位buttontextvieweditbox

PS此项目在Eclipse 3.7中开发,并通过Android 2.3.3进行了测试。

1. RelativeLayout

打开“ res / layout / main.xml ”文件,添加组件并通过“ RelativeLayout放置它。 阅读下面的XML代码,它很冗长,可以告诉您组件的显示位置。

文件:res / layout / main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <Button
        android:id="@+id/btnButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 1"/>
    
    <Button
        android:id="@+id/btnButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 2"
        android:layout_toRightOf="@+id/btnButton1"/>

     <Button
        android:id="@+id/btnButton3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 3"
        android:layout_below="@+id/btnButton1"/>

     <TextView
         android:id="@+id/textView1"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_below="@+id/btnButton3"
         android:layout_marginTop="94dp"
         android:text="User :"
         android:textAppearance="?android:attr/textAppearanceLarge" />

     <EditText
         android:id="@+id/editText1"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignParentRight="true"
         android:layout_alignTop="@+id/textView1"
         android:layout_toRightOf="@+id/btnButton3" />

     <Button
         android:id="@+id/btnSubmit"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignParentRight="true"
         android:layout_below="@+id/editText1"
         android:text="Submit" />
     
</RelativeLayout>

2.演示

参见结果,以上XML代码将生成以下输出。

android relativelayout demo

下载源代码

下载它– Android-RelativeLayout-Example.zip (15 KB)

参考文献

  1. Android RelativeLayout示例
  2. Android RelativeLayout JavaDoc

翻译自: https://mkyong.com/android/android-relativelayout-example/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值