前面大概了解了兩個常用的布局,線性布局和相對布局,接下來看看其他布局
一、TableLayout
TableLayout繼承LinearLayout,其有個特殊的子節點TableRow,這個TableRow相當于一個orientation屬性值為horizontal的LinearLayout,然後添加多個TableRow排成多行,形成一個類似表格的界面。
TableLayout常用的屬性:
android:shrinkColumns 設定允許被收縮的列的序列号
android:stretchColumns設定允許被拉伸的列的序列号
android:collapseColumns設定需要被隐藏的序列号
序列号指TableRow裡的子元件的從左至右的順序,并且從0開始,選擇多個序列号時可以用逗号隔開
基本使用:
<?xml version="1.0" encoding="utf-8"?>
<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"
android:stretchColumns="1,2"
android:collapseColumns="0"
tools:context="com.example.yougel.otherlayoutdemo.TableLayoutActivity">
<TableRow>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按鈕一"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按鈕二"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按鈕三"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按鈕四"
/>
</TableRow>
<TableRow>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按鈕五"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按鈕六"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按鈕七"
/>
</TableRow>
<TableRow>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按鈕八"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按鈕九"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按鈕十"
/>
</TableRow>
</TableLayout>
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsISM4gDN0YDMzIjMxkDM2EDMy8CX0Vmbu4GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.jpg)
二、FrameLayout
FrameLayout(幀布局)也有的叫層布局,層布局的概念布局好了解,是以我就用層的概念來總結一下FrameLayout
我們從手機來看的FrameLayout:
其實它是層層布局的,從3D角度看:
(當然樓主畫工差就不過多自槽)
基本使用:
<?xml version="1.0" encoding="utf-8"?>
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.yougel.otherlayoutdemo.FrameLayoutActivity">
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@color/color1"
android:width="320dp"
android:height="320dp"
/>
<TextView
android:id="@+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@color/color2"
android:width="280dp"
android:height="280dp"
/>
<TextView
android:id="@+id/text3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@color/color3"
android:width="240dp"
android:height="240dp"
/>
<TextView
android:id="@+id/text4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@color/color4"
android:width="200dp"
android:height="200dp"
/>
<TextView
android:id="@+id/text5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@color/color5"
android:width="160dp"
android:height="160dp"
/>
<TextView
android:id="@+id/text6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@color/color6"
android:width="120dp"
android:height="120dp"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/start"
android:layout_weight="1"
android:text="開始"/>
<Button
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/stop"
android:text="停止"/>
</LinearLayout>
</FrameLayout>
Activity:
package com.example.yougel.otherlayoutdemo;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.Timer;
import java.util.TimerTask;
public class FrameLayoutActivity extends AppCompatActivity {
Button start,stop;
//設定顔色數組
int[] colors={
R.color.color1,
R.color.color2,
R.color.color3,
R.color.color4,
R.color.color5,
R.color.color6
};
int changeColor=;
//設定textView數組
int[] names={
R.id.text1,
R.id.text2,
R.id.text3,
R.id.text4,
R.id.text5,
R.id.text6
};
boolean isPause;
TextView[] views=new TextView[names.length];
//設定handler
Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
if(msg.what==&&isPause==false){
for(int i=;i<names.length;i++){
views[i].setBackgroundResource(colors[(i+changeColor)%colors.length]);
}
changeColor++;
}
super.handleMessage(msg);
}
};
Timer timer=new Timer();
//設定timer
public void setTimer(){
isPause=false;
TimerTask timerTask=new TimerTask() {
@Override
public void run() {
handler.sendEmptyMessage();
}
};
timer.schedule(timerTask,,);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_frame_layout);
for(int i=;i<names.length;i++){
views[i]= (TextView) findViewById(names[i]);
}
start= (Button) findViewById(R.id.start);
stop=(Button)findViewById(R.id.stop);
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
setTimer();
}
});
stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
isPause=true;
}
});
}
}
上面代碼通過設定一個定時器Timer類來定時更改FrameLayout中每個TextView的顔色,形成類似霓虹燈的效果。
在在這裡有個常識就是Android中子線程是無法修改UI主線程界面的,由于Timer的使用需要建立子線程,這裡就采用Handle的發送消息的方式對主線程界面進行修改。
至于AbsoluteLayout(絕對布局)就不多說了,我們可以将子元件放到布局中指定的坐标處,但不利于相容不同尺寸的裝置,是以在高版本的API是棄用的。