RelativeLayout顾名思义,这是一个相对布局。也就是说这个布局里面的元素是按照相对位置来布局的,允许其子元素指定它们相对于其父元素或兄弟元素的位置,这是实际布局中最常用的布局方式之一。比如有两个布局里面有两个TextView,我们可以定义第二个TextView在第一个TextView的右边或者下边。第二过TextView出现的位置,将会取决于第一个TextView的位置。
这种布局的灵活性大,当然属性也多,操作难度也大。不同的属性之间很有可能会产生冲突。
下面我们用相对布局再做一次上面的例子,首先放置一个图片,其它两个文本分别相对上一个元素定位:
<?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="wrap_content"
android:padding="10dip">
<TextView android:id="@+id/lable"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="请输入用户名:"/>
<EditText android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/lable"/>
<Button android:id="@+id/cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/text"
android:layout_alignParentRight="true"
android:layout_marginLeft="10dip"
android:text="取消"/>
<Button android:id="@+id/ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="@id/cancel"
android:layout_alignTop="@id/cancel"
android:text="确定"/>
</RelativeLayout>
下面是分别在手机和平板上面的显示效果。
- 大小: 21.5 KB
- 大小: 4.4 KB
分享到:
相关推荐
点对点 Android_Layout_之_RelativeLayout_代码实现相对布局 使用
www.mars-droid.com/Android开发视频教程 RelativeLayout代码 源码 mars老师讲课 android 视频源码 Layout_03(在此特别感谢mars的无私奉献,此代码为跟随视频边学边做的)
Android Layout多种使用方法源码演示,这个例子中演示了如何使用FrameLayout、如何使用RelativeLayout、如何使用RelativeLayout和LinearLayout以及如何使用TableLayout的例子,都挺实用,平时用的比较多,但是比较...
android:layout_above———-位于给定DI控件之上 android:layout_below ———-位于给定DI控件之下 android:layout_toLeftOf ——-位于给定控件左边 android:layout_toRightOf ——位于给定控件右边 android:...
NULL 博文链接:https://hanllove001.iteye.com/blog/1185546
介绍一下RelativeLayout的一些属性,针对于刚学习android开发的朋友
下面我们举一个例子看看:例子里的机器人图片大小是250X250,可以看到我们使用android:layout_x和android:layout_y来指定子元素的纵横坐标。 <?xml version=”1.0″ encoding=”utf-8″?> <Absolute
<RelativeLayout xmlns:android=http://schemas.android.com/apk/res/android android:layout_width=match_parent android:layout_height=match_parent android:clipChildren=false> <ImageView android:...
android 自定义右上角为关闭按钮X的dialog,其中X是文字按钮。是在网站上不可用的基础上,用RelativeLayout 改的。
android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center" > <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width=...
RelativeLayout使用详解 包括属性方法详解 实现LinearLayout中weight属性 用 RelativeLayout实现计算器界面 具体参见博客 http://blog.csdn.net/a87b01c14/article/details/49466325
想将Button和ListView分别放在屏幕的一左一右。 单纯使用android:gravity和android:layout_gravity不成功。 于是涉及到RelativeLayout。 关键为:android:layout_alignParentRight=”true”, android:layout_...
android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="13.0dip" android:layout_marginTop="2.0dip" android:layout_centerVertical="true" /> </...
android:layout_width="fill_parent" android:layout_height="fill_parent"> <!-- FrameLayout - 层叠式布局。以左上角为起点,将 FrameLayout 内的元素一层覆盖一层地显示 --> <FrameLayout android...
android:layout_width="fill_parent" android:layout_height="fill_parent" zhy:position="left_top" zhy:radius="130dp" > <RelativeLayout android:layout_width="wrap_content" android:layout_height=...
android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > android:layout_width="match_parent" android:layout_height="wrap_content" android:...
《Android高薪之路:Android程序员面试宝典》目录: 第1章 Android入门 1 1 关于Android的非技术问题 1 1 1 为什么看好 Android 1 1 2 以前是否从事过Android的工作 做过哪些工作 1 1 3 你做的最复杂的界面是什么 1 ...
private void addViewByMargins(RelativeLayout layout, View view, int x, int y, int width, int height) { RelativeLayout.LayoutParams layout_params = null; layout_params = new RelativeLayout....
一、什么是相对布局 相对布局是另外一种控件摆放的方式 相对布局是通过指定当前控件与兄弟控件或者父控件之间的相对位置,从而...(3)android:layout_toLeftOf 放在某个存在id控件的左边(也就是当前控件的右边对齐到某
文档介绍了Android学习指南Layout前言,(1) LinearLayout,Android官方LinearLayout Demo,(2) RelativeLayout,官方的RelativeLayout Demo,(3) TableLayout,官方的TableLayout Demo