LinearLayout、RelativeLayout、FrameLayout简单使用

1.LinearLayout:又称为线性布局,是一种非常常用的布局。

   linearlayout 设置方向:android:orientation属性指定排列方向vertical(垂直方向)、horizontal(水平方向)

android:orientation="vertical" //垂直方向
android:orientation="horizontal" //水平方向

  layout_gravity 与 gravity 属性之间的区别?
 1)对齐方式:gravity:用于指定文字在控件中的对齐方式layout_gravity:用于指定控件在布局中的对齐方式
 2)可选值:gravity与layout_gravity的用法差不多。但是当设置方向为vertical(垂直方向),只有水平方向上的对齐方式才会生效;相反,使用horizontal(水平方向)
                    只有在垂直方向上才会生效,因为此时水平方向上的长度不固定,每添加一个控件,水平方向上的长度会改变,因此无法指定该水平方向的对齐
 还有一个重要的属性:layout:weight:允许使用比例的方式来指定布局控件的大小,它在手机屏幕的适配性方面会起到非常重要的作用。

  

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:background="#ffffff">

    <EditText
        android:id="@+id/editText2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:ems="10"
        android:inputType="textEmailAddress" />

    <Button
        android:id="@+id/editText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:ems="10"
        android:hint="send"
        android:inputType="textPassword" />
</LinearLayout>

  edittext和button都将android:layout_weight属性设置为1,这表示将在水平方向平分宽度,相反,垂直方向也可以设置,当前提是要知道高度。


2.RelativeLayout:又称为相对布局,它通过相对定位的方法让控件出现在布局的任何位置。

属性总结有以下:
  相对于兄弟元素
    android:layout_below="@id/aaa":在指定View的下方
    android:layout_above="@id/xxx":在指定View的上方
    android:layout_toLeftOf="@id/bbb":在指定View的左边
    android:layout_toRightOf="@id/cccc":在指定View的右边相对于兄弟元素
    android:layout_below="@id/aaa":在指定View的下方
    android:layout_above="@id/xxx":在指定View的上方
    android:layout_toLeftOf="@id/bbb":在指定View的左边
    android:layout_toRightOf="@id/cccc":在指定View的右边
  相对于父元素
   android:layout_alignParentLeft="true":在父元素内左边
   android:layout_alignParentRight="true":在父元素内右边
   android:layout_alignParentTop="true":在父元素内顶部
   android:layout_alignParentBottom="true":在父元素内底部
  对齐方式
  android:layout_centerInParent="true":居中布局
  android:layout_centerVertical="true":水平居中布局
  android:layout_centerHorizontal="true":垂直居中布局
  android:layout_alignTop="@id/xxx":与指定View的上边界一致
  android:layout_alignBottom="@id/xxx":与指定View下边界一致
  android:layout_alignLeft="@id/xxx":与指定View的左边界一致
  android:layout_alignRight="@id/xxx":与指定View的右边界一致
  间隔
  android:layout_marginBottom=""; 离某元素底边缘的距离
  android:layout_marginLeft=""; 离某元素左边缘的距离
  android:layout_marginRight ="";离某元素右边缘的距离
  android:layout_marginTop=""; 离某元素上边缘的距离
  android:layout_paddingBottom=""; 离父元素底边缘的距离
  android:layout_paddingLeft=""; 离父元素左边缘的距离
  android:layout_paddingRight ="";离父元素右边缘的距离
  android:layout_paddingTop=""; 离父元素上边缘的距离


3.FrameLayout:又称为帧布局这种布局没有方便的定位方式,所有的控件都会默认的摆放在布局的左上角。
 属性:layout_gravity来指定控件在布局中的对齐方式  

  • android:foreground:*设置改帧布局容器的前景图像
  • android:foregroundGravity:设置前景图像显示的位置




  










评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值