Android布局(序章)

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>

这里写图片描述

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>

这里写图片描述

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

<?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>

这里写图片描述
上图就是布局后的样式

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

    <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>

这里写图片描述
以上就是将绝对布局实例包含RTL布局中的实例的一个实例。

用java代码实现布局
首先找到文件中的MainActivity文件并打开。
这里写图片描述
先将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);
    }
}

连接真机显示图
这里写图片描述

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

(精彩小编:阿辉)

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值