Android布局管理器本身就是一个UI组件,所有的布局管理器都是ViewGroup的子类。
2.2.1 线性布局LinearLayout
Android的线性布局不会换行,当组件一个挨着一个地排列到头之后,剩下的组件将不会被显示出来。
LinearLayout常用XML属性 android:gravity 设置布局管理器内组件的对齐方式 ( 例:android:gravity = " bottom|center_horizontal" //所有组件对齐到容器底部并且水平居中)
2.2.2表格布局TableLayout
TableLayout 继承了LinearLayout。 采用行,列的形式来管理UI组件,TableLayout并不需要明确的声明包括多少行,多少列,而是通过添加TableRow,其他组件来控制表格的行数和列数。 1. TableRow就是一个表格行,TableRow也是容器,因此它也可以不断的添加组件,每添加一个组件该表格就增加一列 2. 如果直接向TableLayout中添加组件,每添加一个组件该表格就增加一列
单元格的3种行为方式: Shrinkable:可收缩的。如果某个列被设为Shrinkable,那么该列的所有单元格的宽度可以被收缩,以保证该表格能适应父容器的宽度。 Stretchable:可拉伸的。 如果某个列被设为 Stretchable ,那么该列的所有单元格的宽度可以被拉伸,以保证组件能完全填满表格空余空间。 Collapsed: 如果某个列被设为 Collapsed ,那么该列的所有单元格会被隐藏
TableLayout常用XML属性
android:shrinkColumns //收缩 android:strectchColumns //拉伸 android:collapseColumns //隐藏
例:测试隐藏属性
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
//android:collapseColumns="0,2" //隐藏第一列和第三列单元格
>
<TableRow>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="隐藏1"
/>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="隐藏2"
/>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="隐藏3"
/>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="隐藏4"
/>
</TableRow>
</TableLayout>
注释掉 android :collapseColumns= "0,2" 时效果如下
去掉注释后 效果如下
可以看出隐藏第几列其实就是把第几列去掉,后面的列往前占据隐藏列的位置 和 android:visibility="invisible" 不一样 visibility只是设置组件是否可见,如果设置为不可见,也会为当前组件保留原来的位置
把第二个Button设置为不可见 效果如下
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="invisible"
android:text="隐藏2"
/>
2.2.3帧布局 FrameLayout FrameLayout直接继承了ViewGroup组件。
帧布局容器为每个加入其中的组件创建一个空白的区域(称为一帧),每个子组件占据一帧,这些帧都会根据gravity属性自动对齐。 FrameLayout的效果是把组件一个个的地叠加在一起。
FrameLayout常用XML属性 android:foreground 设置该帧布局的前景图像 android:foregroundGravity 定义绘制前景图像的gravity属性
FrameLayout 包含的子元素也受FrameLayout.LayoutParams控制,因此它所包含的子元素也可指定android:layout_gravity属性,该属性控制该子元素在FrameLayout中的对齐方式。
例:6个TextView叠加在一起 ,上面的TextView遮住下面的TextView 这6个TextView的高度,宽度逐渐减少————这样可以保证最先添加的TextView不会完全被遮挡。并且设置这6个TextView的背景色不一样,对齐方式都是按中心对齐
<?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/view01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="240pt"
android:height="240pt"
android:layout_gravity="center"
android:background="#F00"
/>
<TextView
android:id="@+id/view02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="200pt"
android:height="200pt"
android:layout_gravity="center"
android:background="#0f0"
/>
<TextView
android:id="@+id/view03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="180pt"
android:height="180pt"
android:layout_gravity="center"
android:background="#00f"
/>
<TextView
android:id="@+id/view04"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="140pt"
android:height="140pt"
android:layout_gravity="center"
android:background="#Ff0"
/>
<TextView
android:id="@+id/view05"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="100pt"
android:height="100pt"
android:layout_gravity="center"
android:background="#F0f"
/>
<TextView
android:id="@+id/view06"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="60pt"
android:height="60pt"
android:layout_gravity="center"
android:background="#0ff"
/>
</FrameLayout>
布局效果
实例:霓虹灯效果 Tip: 1.Handler
复写Handler中处理消息的方法 void handlerMessage(Message msg) 2.Timer,TimeTask Timer对象可调度TimerTask对象,TimerTask对象的本质就是启动一条新线程,由于Android不允许在新线程中访问Activity里的界面组件,因此程序只能在新线程里发送一条消息,通知系统更新View Timer的常用方法: (1)public void schedule(TimerTask task, long delay)//调度一个task,经过delay毫秒后开始调度,并且只调度一次 (2)public void schedule(TimerTask task, long delay, long period) // 调度一个task,经过delay毫秒后开始调度, 每次调度完后,最少等待period(ms)后才再次开始调度
1.将这6个 TextView的背景色放入color.xml中
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
<color name="color01">#F00</color>
<color name="color02">#0f0</color>
<color name="color03">#00f</color>
<color name="color04">#Ff0</color>
<color name="color05">#F0f</color>
<color name="color06">#0ff</color>
</resources>
2.更改TableLayout中背景色的引用 @color/color01....
<?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/view01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="240pt"
android:height="240pt"
android:layout_gravity="center"
android:background="@color/color01"
/>
<TextView
android:id="@+id/view02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="200pt"
android:height="200pt"
android:layout_gravity="center"
android:background="@color/color02"
/>
<TextView
android:id="@+id/view03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="180pt"
android:height="180pt"
android:layout_gravity="center"
android:background="@color/color03"
/>
<TextView
android:id="@+id/view04"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="140pt"
android:height="140pt"
android:layout_gravity="center"
android:background="@color/color04"
/>
<TextView
android:id="@+id/view05"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="100pt"
android:height="100pt"
android:layout_gravity="center"
android:background="@color/color05"
/>
<TextView
android:id="@+id/view06"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="60pt"
android:height="60pt"
android:layout_gravity="center"
android:background="@color/color06"
/>
</FrameLayout>
3.MainActivity中复写handleMessage( Message msg ),oncreate()中调用定时器发送消息
package com.example.administrator.drawview;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutCompat;
import android.util.Log;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends AppCompatActivity {
private int currentTime = 0;
final int My_Message = 1234;
//定义一个颜色数组
final int colors[] = {R.color.color01,R.color.color02,R.color.color03,
R.color.color04,R.color.color05,R.color.color06};
final int textViewId [] ={R.id.view01,R.id.view02,R.id.view03,
R.id.view04,R.id.view05,R.id.view06};
TextView textView[] = new TextView[textViewId.length];
Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
if(msg.what == My_Message){
for (int i =0;i<textViewId.length;i++)
{
textView[i].setBackgroundResource(colors[(i + currentTime)%textViewId.length]);
}
currentTime++;
}
super.handleMessage(msg);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
for(int i = 0;i<textViewId.length;i++)
textView[i] = (TextView) findViewById(textViewId[i]);
new Timer().schedule(new TimerTask() {
@Override
public void run() {
//发送一条空消息通知系统改变6个TextView的背景色
handler.sendEmptyMessage(My_Message);
}
}, 0, 1000);
}
}