1. 相对布局RelativeLayout
特点:相对布局所有组件可以叠加在一起;各个组件的布局是独立的,互不影响;所有组件的默认位置都是在左上角(顶部、左部对齐)
属性 | 功能描述 |
---|---|
android:layout_toRightOf | 在指定控件的右边 |
android:layout_toLeftOf | 在指定控件的左边 |
android:layout_above | 在指定控件的上边 |
android:layout_below | 在指定控件的下边 |
android:layout_alignBaseline | 跟指定控件水平对齐 |
android:layout_alignLeft | 跟指定控件左对齐 |
android:layout_alignRight | 跟指定控件右对齐 |
android:layout_alignTop | 跟指定控件顶部对齐 |
android:layout_alignBottom | 跟指定控件底部对齐 |
android:layout_alignParentLeft | 是否跟父元素左对齐 |
android:layout_alignParentTop | 是否跟父元素顶部对齐 |
android:layout_alignParentRight | 是否跟父元素右对齐 |
android:layout_alignParentBottom | 是否跟父元素底部对齐 |
android:layout_centerVertical | 在父元素中垂直居中 |
android:layout_centerHorizontal | 在父元素中水平居中 |
android:layout_centerInParent | 在父元素中居中 |
示例1:res\layout\activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<!-- android:layout_alignParentRight="true"表示与父元素右对齐(这里,父元素指的就是RelativeLayout,而RelativeLayout占满了整个屏幕) -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第一个"
android:layout_alignParentRight="true"/>
<!-- android:layout_alignParentBottom="true"表示与父元素底部对齐(这里,父元素指的就是RelativeLayout,而RelativeLayout占满了整个屏幕) -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第二个"
android:layout_alignParentBottom="true"/>
<!-- android:layout_centerVertical="true"表示在父元素中垂直居中(这里,父元素指的就是RelativeLayout,而RelativeLayout占满了整个屏幕) -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第三个"
android:layout_centerVertical="true"
/>
<!-- android:layout_centerHorizontal="true"表示在父元素中水平居中(这里,父元素指的就是RelativeLayout,而RelativeLayout占满了整个屏幕) -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第四个"
android:layout_centerHorizontal="true"
/>
<!-- android:layout_centerInParent="true"表示在父元素中水平垂直都居中(这里,父元素指的就是RelativeLayout,而RelativeLayout占满了整个屏幕) -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第五个"
android:layout_centerInParent="true"
/>
</RelativeLayout>
运行结果:
示例2:res\layout\activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第一一一一个" />
<!-- android:layout_alignRight="true"表示与指定组件右对齐。 -->
<!-- @+id/就是为组件添加id,@id就是通过id引用组件 -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第二个"
android:layout_centerVertical="true"
android:layout_alignRight="@id/tv1"
/>
<!-- 如果同时使用android:layout_alignRight="true",android:layout_alignLeft="true",组件就会被拉伸 -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第三个"
android:layout_alignParentBottom="true"
android:layout_alignRight="@id/tv1"
android:layout_alignLeft="@id/tv1"
/>
</RelativeLayout>
运行结果:
示例3:res\layout\activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第一个"
android:layout_centerHorizontal="true"/>
<!-- android:layout_below="@id/tv1"表示在对应组件的下面 -->
<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第二个"
android:layout_below="@id/tv1"
/>
<!-- android:layout_above="@id/tv2"表示在对应组件的上面 -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第三个"
android:layout_above="@id/tv2"
/>
<!-- android:layout_toRightOf="@id/tv2"表示在对应组件的右边 -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第四个"
android:layout_toRightOf="@id/tv2"
/>
</RelativeLayout>
运行结果:
练习:实现如下效果
代码:res\layout\activity_main.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/center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="中间"
android:layout_centerInParent="true"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="上边"
android:layout_above="@id/center"
android:layout_alignLeft="@id/center"
android:layout_alignRight="@id/center"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下边"
android:layout_below="@id/center"
android:layout_alignLeft="@id/center"
android:layout_alignRight="@id/center"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="左边"
android:layout_toLeftOf="@id/center"
android:layout_alignTop="@id/center"
android:layout_alignBottom="@id/center"
android:layout_alignParentLeft="true"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="右边"
android:layout_toRightOf="@id/center"
android:layout_alignTop="@id/center"
android:layout_alignBottom="@id/center"
android:layout_alignParentRight="true"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="左上"
android:layout_toLeftOf="@id/center"
android:layout_above="@id/center"
android:layout_alignParentLeft="true"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="左下"
android:layout_toLeftOf="@id/center"
android:layout_below="@id/center"
android:layout_alignParentLeft="true"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="右上"
android:layout_toRightOf="@id/center"
android:layout_above="@id/center"
android:layout_alignParentRight="true"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="右下"
android:layout_toRightOf="@id/center"
android:layout_below="@id/center"
android:layout_alignParentRight="true"/>
</RelativeLayout>
运行结果:
2. 帧布局FrameLayout
特点:帧布局和相对布局一样,组件可以重叠;所有组件的默认位置是在左上角(顶部、左部对齐)
示例:res\layout\activity_main.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<!-- 帧布局中,修改组件的位置使用android:layout_gravity -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第一个"
android:layout_gravity="right"
/>
<!-- android:layout_gravity="bottom|right" ,表示组件的位置在右下角 -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第二个"
android:layout_gravity="bottom|right"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第三个"
android:layout_gravity="center"
/>
</FrameLayout>
运行结果:
练习:实现如下效果
代码:res\layout\activity_main.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:layout_width="240dp"
android:layout_height="240dp"
android:background="#ff0000"
android:layout_gravity="center"
/>
<TextView
android:layout_width="200dp"
android:layout_height="200dp"
android:background="#00ff00"
android:layout_gravity="center"
/>
<TextView
android:layout_width="160dp"
android:layout_height="160dp"
android:background="#0000ff"
android:layout_gravity="center"
/>
<TextView
android:layout_width="120dp"
android:layout_height="120dp"
android:background="#ffff00"
android:layout_gravity="center"
/>
<TextView
android:layout_width="80dp"
android:layout_height="80dp"
android:background="#ff00ff"
android:layout_gravity="center"
/>
<TextView
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#ffffff"
android:layout_gravity="center"
/>
</FrameLayout>
运行结果:
3. 表格布局TableLayout
示例:res\layout\activity_main.xml
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<!-- 表格布局宽高可以不用设置,TableRow的宽度只能是match_parent,高度只能是wrap_content。调正宽度可以通过权重解决。-->
<TableRow>
<TextView
android:text="姓名:"/>
<EditText
android:layout_weight="1"
/>
</TableRow>
<TableRow>
<TextView
android:text="年龄:"/>
<EditText
android:layout_weight="1"
/>
</TableRow>
</TableLayout>*复制代码* 运行结果:
4. 绝对布局AbsoluteLayout
绝对布局是直接使用android:layout_x,android:layout_y定位控件的坐标,做不了屏幕适配,所以不常使用。某些没有必要做屏幕适配的开发可以用绝对布局,例如:电视屏幕固定,做机顶盒开发。
示例:res\layout\activity_main.xml
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="109dp"
android:layout_y="83dp"
android:text="Button" />
</AbsoluteLayout>*复制代码* 运行结果:
4. LogCat
Console只能显示Windows下运行的平台信息,例如:模拟器的运行状态(模拟器是运行在Windows平台上的程序)。但模拟器内的程序运行状态就不能显示在Console上了,因为这些程序不是运行到Windows上,这些信息会在LogCat中显示。
LogCat分为5个等级,依次为:error(错误)、warn(情报)、info(信息)、debug(调试)、verbose(冗余)
示例:为LogCat添加过滤器,便于筛选信息
s