RelativeLayout相对布局
RelativeLayout是相对布局控件:以控件之间相对位置或相对父容器位置进行排列。
属性:
第一类:属性值为true或false
android:layout_centerHorizontal 水平居中
android:layout_centerVertical 垂直居中
android:layout_centerInParent 相对于父元素完全居中
android:layout_alignParentBottom 贴紧父元素的下边缘
android:layout_alignParentLeft 贴紧父元素的左边缘
android:layout_alignParentRight 贴紧父元素的右边缘
android:layout_alignParentTop 贴紧父元素的上边缘
android:layout_alignWithParentIfMissing 如果对应的兄弟元素找不到的话就以父元素做参照物
第二类:属性值必须为id的引用名
  android:layout_below 在某元素的下方
  android:layout_above 在某元素的的上方
  android:layout_toLeftOf 在某元素的左边
  android:layout_toRightOf 在某元素的右边
  android:layout_alignTop 本元素的上边缘和某元素的的上边缘对齐
  android:layout_alignLeft 本元素的左边缘和某元素的的左边缘对齐
  android:layout_alignBottom 本元素的下边缘和某元素的的下边缘对齐
  android:layout_alignRight 本元素的右边缘和某元素的的右边缘对齐
第三类:属性值为具体的像素值,如30dip,40px
  android:layout_marginBottom 离某元素底边缘的距离
  android:layout_marginLeft 离某元素左边缘的距离
  android:layout_marginRight 离某元素右边缘的距离
  android:layout_marginTop 离某元素上边缘的距离
以下就是相对布局的一个经典实例:

<?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">    <Button        android:layout_width="66dp"        android:layout_height="66dp"        android:background="#ff0000"        android:text="RED"        android:textSize="30sp"        />    <Button        android:layout_width="66dp"        android:layout_height="66dp"        android:background="#ffff00"        android:text="yello"        android:textSize="20sp"        android:layout_centerHorizontal="true"        />    <Button        android:layout_width="66dp"        android:layout_height="66dp"        android:background="#ff00ff"        android:text="violet"        android:textSize="18sp"        android:layout_alignParentRight="true"        />    <Button        android:layout_width="66dp"        android:layout_height="66dp"        android:background="#0000ff"        android:text="BLUE"        android:textSize="25sp"        android:layout_centerInParent="true"        android:id="@+id/tv_main_blue"        android:layout_margin="10dp"        />    <Button        android:layout_width="66dp"        android:layout_height="66dp"        android:background="#00ffff"        android:text="wathet"        android:textSize="16sp"        android:layout_toLeftOf="@id/tv_main_blue"        android:layout_centerVertical="true"        />    <Button        android:layout_width="66dp"        android:layout_height="66dp"        android:background="#00ff00"        android:text="wathet"        android:textSize="16sp"        android:layout_toRightOf="@id/tv_main_blue"        android:layout_centerVertical="true"        />    <Button        android:layout_width="match_parent"        android:layout_height="66dp"        android:background="#f0f0f0"        android:text="gray"        android:textSize="16sp"        android:layout_alignParentBottom="true"        />RelativeLayout>

Android布局(序章)_第1张图片

3.3-AbsoluteLayout绝对布局(过时)
可以自己随意设置控件位置,不推荐使用,因为屏幕大小变化,其位置也会变化所以已经过时。
常用属性:
layout_x设置控件横坐标
layout_y设置控件纵坐标
实例:

<?xml version="1.0" encoding="utf-8"?><AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent" android:layout_height="match_parent">    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="绝对布局"        android:layout_x="100dp"        android:layout_y="100dp"        />AbsoluteLayout>

Android布局(序章)_第2张图片

RTL布局
从Android 4.2开始,Android SDK支持一种从右到左(RTL,Right-to-Left)UI布局的方式,尽管这种布局方式经常被使用在诸如阿拉伯语、希伯来语等环境中,中国用户很少使用。不过在某些特殊用途中还是很方便的。
所谓RTL,就是指按平常习惯在左的视图都会在右侧,在右侧的视图都会在左侧。例如,在线性布局中第1个子视图默认都是在左上角的,如果采用RTL布局,默认就在右上角。
属性:
android:layoutDirection=”rtl”设置试图模式从右到左
实例:
Android布局(序章)_第3张图片
将上图的两个按钮从右到左的顺序排列

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="horizontal" android:layout_width="match_parent"    android:layout_height="match_parent"    android:layoutDirection="rtl"    >    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="1"        />    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginStart="100dp"        android:text="2"        />LinearLayout>

Android布局(序章)_第4张图片
上图就是布局后的样式

布局包含
在一个布局中调用另一个布局中的内容将两个布局的内容和在一起。
属性:

    <include layout="@布局文件路径">include>引用布局文件

就在上面的AbsoluteLayout绝对布局实例中调用RTL布局中的实例:

<?xml version="1.0" encoding="utf-8"?><AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent" android:layout_height="match_parent">    <Button        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="绝对布局"        android:layout_x="100dp"        android:layout_y="100dp"        />    <include layout="@layout/rtl">include>AbsoluteLayout>

Android布局(序章)_第5张图片
以上就是将绝对布局实例包含RTL布局中的实例的一个实例。

用java代码实现布局
首先找到文件中的MainActivity文件并打开。
Android布局(序章)_第6张图片
先将MainActivity类中方法的setContentView注释掉,自己去编写java代码生成布局以下就是生成了一个 LinearLayout线性布局

public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);//        setContentView(R.layout.activity_main);//设置真机中默认的布局文件        LinearLayout linear=new LinearLayout(this);        Button button=new Button(this);        button.setText("阿辉");        linear.addView(button);        setContentView(linear);    }}

连接真机显示图
Android布局(序章)_第7张图片

以上就是今天给大家带来的内容,感谢大家的阅读,我会努力给大家带来更精彩的内容。

(精彩小编:阿辉)

更多相关文章

  1. 解决Android编辑框在全屏模式下无法检测布局变化的问题
  2. Android系统下如何在程序中对XML里面元素进行赋值
  3. 四.Android六种布局详细讲解
  4. Android ConstraintLayout 约束布局
  5. Android Layout Tricks #3: Optimize by merging(Android 布局技
  6. android一种较为复杂的布局参考(xml文件)
  7. android使用同一个RecyclerView实现两种不同Item布局
  8. Android Studio [相对布局RelativeLayout]
  9. [中英文对照]android Designing for TV(一) ------ Optimizing L

随机推荐

  1. 拐点回归设计RKD概览, 及其开展实证研究
  2. HBase抗战总结|阿里巴巴HBase高可用8年抗
  3. 京东电商推荐系统实践
  4. HBase原理|HBase内存管理之MemStore进化
  5. Xcode12无法安装,提示磁盘空间不足
  6. 什么时候应该使用回归分析?控制变量意味着
  7. 计量院士首次用DID方法分析, 中国封城对
  8. “老司机”成长之路:自动驾驶车辆调试实践
  9. 自动驾驶多传感器感知的探索
  10. 01-开发工具安装配置与HTML文档结构