美文网首页
Android布局基础

Android布局基础

作者: MacroZH | 来源:发表于2018-05-02 21:53 被阅读0次

Android在xml文件中编写布局信息,标识出所用的控件以及控件属性,控件之间的关系等等,每个控件都是一个View,View的继承结构如下


image.png

总体上可以分为两部分:个体View(如TextView,Button等)与ViewGroup(LinearLayout、RelativeLayout等)。
顾名思义ViewGroup内部可以嵌套其他的View(可以是个体View,也可以是ViewGroup)。
LinearLayout、RelativeLayout、FrameLayout是最常用的布局。

1 LinearLayout线性布局

在数据中需要声明控件的摆放方向是垂直或者是水平,

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

指定方向后控件就会按照顺序依次排放

<?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="vertical"
    >
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button1"
        android:textSize="25sp"/>
    <Button

        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button2"
        android:textSize="25sp"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button3"
        android:textSize="25sp"/>
</LinearLayout>

显示效果


image.png

LinearLayout常用属性:
gravity :确定子控件的位置,取值:

  1. center_vertical 垂直居中
  2. center_horizontal 水平居中
  3. center 居中
  4. right 子类控件位于当前布局的右边
  5. left 子类控件位于当前布局的左边
  6. bottom 子类控件位于当前布局的下面

layout_gravity: 子类控件使用的属性,表明自己在LinearLayout中的位置。取值同上。

layout_weight:子类控件使用的属性,表明自己在LinearLayout中的比重。
例如

<?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"
    >
    <Button
        android:id="@+id/button1"
        android:layout_width="0dp"
        android:layout_weight="2"
        android:layout_height="wrap_content"
        android:text="Button1"
        android:textSize="25sp"/>
    <Button
        android:id="@+id/button2"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:text="Button2"
        android:textSize="25sp"/>
    <Button
        android:id="@+id/button3"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Button3"
        android:textSize="25sp"/>
</LinearLayout>

这里设置Button1,2,3的比重为2:1:1


image.png

2 RelativeLayout相对布局

通过控件的属性来声明控件之间的相对摆放关系。例如将上面的Button2摆放在Button1下面,Button3摆放在Button1的右面。


image.png

对应的xml布局文件为:

<?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:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button1"
        android:textSize="25sp"/>
    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button2"
        android:layout_below="@id/button1"
        android:textSize="25sp"/>
    <Button
        android:id="@+id/button3"
        android:layout_toRightOf="@id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button3"
        android:textSize="25sp"/>
</RelativeLayout>

Relative常用的属性为:
第一类:boolean值

android:layout_centerHrizontal 水平居中  
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 本元素的右边缘和某元素的的右边缘对齐 

第三类:像素值(dp dip px)

android:layout_marginBottom 离某元素底边缘的距离  
android:layout_marginLeft 离某元素左边缘的距离  
android:layout_marginRight 离某元素右边缘的距离  
android:layout_marginTop 离某元素上边缘的距离

3 FrameLayout 帧布局

这种布局所有的控件都会摆放在左上角。一般配合Fragment使用,或者自定义View。

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

    >
    <TextView
        android:id="@+id/view1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="left"
        android:text="view1"
        android:textSize="25sp"/>
    <TextView
        android:id="@+id/view2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="view2"
        android:gravity="center_horizontal"
        android:textSize="25sp"/>
    <TextView
        android:id="@+id/view3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="view3"
        android:gravity="right"
        android:textSize="25sp"/>
</FrameLayout>

这里设置三个TextView的字体gravity分别为左中右。如图可以看出三个控件重叠摆放。


image.png

相关文章

网友评论

      本文标题:Android布局基础

      本文链接:https://www.haomeiwen.com/subject/wehhrftx.html