Android布局主要分为线性布局、表格布局、帧布局、相对布局、网络布局和绝对布局,下面是表格布局、帧布局和网络布局的示例,线性布局和相对布局在开发中使用较多,绝对布局是由开发者通过X和Y坐标来控制组件的位置,由于手机屏幕大小和分辨率不同可能存在较大差异,所以这个已经过时,下面是代码
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical" >
<!-- 表格布局 -->
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:collapseColumns="1"
android:shrinkColumns="0"
android:stretchColumns="3" >
<!--跑马灯 android:clickable="true"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:singleLine="true"
LinearLayout中设置:
android:focusableInTouchMode="true"
android:orientation="vertical" -->
<Button
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@drawable/bg_border"
android:clickable="true"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:singleLine="true"
android:text="shrink收缩,stretch拉伸,collapse隐藏,,,,,shrink收缩,stretch拉伸,collapse隐藏" />
<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:drawableRight="@drawable/ic_launcher"
android:fontFamily="@android:string/copyUrl"
android:text="第三" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:shadowColor="#00f"
android:shadowDx="12.0"
android:shadowDy="9.0"
android:shadowRadius="5.0"
android:text="第四"
android:textColor="#f00"
android:textSize="10pt" />
</TableRow>
</TableLayout>
</LinearLayout>
<!-- 帧布局 -->
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#f00"
android:height="50pt"
android:width="50pt" />
<TextView
android:id="@+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#0f0"
android:height="40pt"
android:width="40pt" />
<TextView
android:id="@+id/text3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#00f"
android:height="30pt"
android:width="30pt" />
<TextView
android:id="@+id/text4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#ff0"
android:height="20pt"
android:width="20pt" />
<TextView
android:id="@+id/text5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#f0f"
android:height="10pt"
android:width="10pt" />
</FrameLayout>
<!--网格布局 -->
<GridLayout
android:id="@+id/gridl"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:columnCount="4"
android:rowCount="6" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_columnSpan="4" />
</GridLayout>
在帧布局中实现了一个霓虹灯效果
private int currentColor = 0;
final int[] colors = new int[]{
R.color.color1,
R.color.color2,
R.color.color3,
R.color.color4,
R.color.color5
};
final int[] name =new int[]{
R.id.text1,
R.id.text2,
R.id.text3,
R.id.text4,
R.id.text5
};
final
TextView[] views=new TextView[name.length];
private GridLayout gridl;
String[] chars =new String[]{
"7","8","9","+","4","5","6","-","1","2","3","x","0",".","=","÷"/*,"C","CE"*/
};
public class MainActivity extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
for (int i = 0; i < colors.length; i++) {
views[i] = (TextView) view.findViewById(name[i]);
}
//定义一个线程周期性的改变currentColor
new Timer().schedule(new TimerTask() {
@Override
public void run() {
handler.sendEmptyMessage(0x123);
}
}, 0,200);
//霓虹灯效果结束
gridl=(GridLayout) view.findViewById(R.id.gridl);
for (int i = 0; i < chars.length; i++) {
Button button=new Button(getActivity());
button.setText(chars[i]);
//设置字号大小
button.setTextSize(40);
//按钮周围的空间
button.setPadding(5, 35, 5, 35);
//设置组件所在的行
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);
gridl.addView(button,params);
}
}
}