Relative布局简介
相对布局的组件是由兄弟组件和父组价决定的,因此这种布局被称为相对布局。
属性设置介绍
RelativeLayout.Layoutparam中只能设置为true和false的属性是:
需要依赖其他组件Id设置的属性:
除此之外,RelativeLayout还可以使用viewGroup.MarginLayoutparams的属性。
梅花布局的实例
<LinearLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:orientation="vertical">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/ic_launcher"
android:id="@+id/view01"
android:layout_centerInParent="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/ic_launcher"
android:layout_below="@id/view01"
android:layout_alignLeft="@id/view01"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/ic_launcher"
android:layout_above="@id/view01"
android:layout_alignLeft="@id/view01"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/ic_launcher"
android:layout_toLeftOf="@id/view01"
android:layout_alignTop="@id/view01"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/ic_launcher"
android:layout_toRightOf="@id/view01"
android:layout_alignTop="@id/view01"/>
</RelativeLayout>
</LinearLayout>
表格布局
GridLayout是4.0以后才加入的布局,把这个空间分成rows*columns个格子,一个组件可以设置横跨多少列和多少行。
gridLayout的属性
常用属性:
GridLayout.Layoutparam的属性:
计算器界面的实例
package peng.liu.testview;
import android.annotation.TargetApi;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.widget.Button;
import android.widget.GridLayout;
import android.widget.TextView;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends Activity {
GridLayout grid = null;
String[] chars = new String[]{
"7","8","9","/",
"4","5","6","*",
"1","2","3","-",
".","0","+","+"
};
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
grid = (GridLayout) findViewById(R.id.root);
for (int i=0;i<chars.length;i++){
Button button = new Button(this);
button.setText(chars[i]);
button.setTextSize(40);
GridLayout.Spec rowSpec = GridLayout.spec(i / 4 + 2);
GridLayout.Spec columnSpec = GridLayout.spec(i%4);
GridLayout.LayoutParams params = new GridLayout.LayoutParams(rowSpec,columnSpec);
params.setGravity(Gravity.FILL);
grid.addView(button,params);
}
}
}
<LinearLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:orientation="vertical">
<GridLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/root"
android:rowCount="6"
android:columnCount="4">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_columnSpan="4"
android:layout_marginLeft="4px"
android:layout_marginRight="4px"
android:padding="5px"
android:textSize="50sp"
android:layout_gravity="right"
android:background="#eee"
android:textColor="#000"
android:text="0"
/>
</GridLayout>
</LinearLayout>
效果图
绝对布局
因为绝对布局,需要亲自设置组件的x位置,和y位置,所以一定程度上已经过时了。
属性
android:layout_x:设置组件的z坐标
android:layout_y:设置组件的Y坐标
可以这么说相对布局已经过时了~