您的位置:首页 > 移动开发 > Android开发

从零开始学android<Tablelayout表格布局.十五.>

2014-08-13 17:02 721 查看
TableLayout就是将手机的屏幕分为一行行的形式进行数据的显示,并且一行可以多个控件

并且可以设置控件的对齐方式,和是否为可收缩行

下面通过一行图和一个简单的例子来看看Tablelayout布局的使用



…………………………………………………………毫无美感的分割线…………………………………………………………

单独使用xml文件进行配置

<?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:shrinkColumns="3">//第三行允许行合并

<TableRow>

<TextView
android:id="@+id/text01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户名" />

<EditText
android:id="@+id/edit01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10" >

<requestFocus />
</EditText>

</TableRow>

<TableRow>
<TextView
android:id="@+id/text02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密  码" />

<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPassword" />

</TableRow>

<TableRow android:gravity="center">

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登录系统" />

</TableRow>
</TableLayout>
运行效果图



从上面的运行效果大家可以很清楚地看到,第一行第二行分别显示了两个组件,第三行显示了一个组件,并设置了剧中对齐的效果。

…………………………………………………………毫无美感的分割线…………………………………………………………

动态布局的方式实现。

package com.example.tablelayout;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.tablelayout);

TableLayout layout = new TableLayout(this);//创建Tablelayout对象
//		设置Tablelayout的对齐方式
TableLayout.LayoutParams tableTLayoutParams = new TableLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,

ViewGroup.LayoutParams.WRAP_CONTENT);
TableRow tableRow1=new TableRow(this);//创建第一行
TableRow tableRow2=new TableRow(this);//创建第二行
TableRow tableRow3=new TableRow(this);//创建第三行

TextView textView1=new TextView(this);//创建Textview对象
textView1.setText("用户名");
EditText editText1=new EditText(this);//创建EditText对象
tableRow1.addView(textView1);//TextView设置到第一行
tableRow1.addView(editText1);//EditText设置到第一行

TextView textView2=new TextView(this);//创建Textview对象
textView2.setText("密   码");
EditText editText2=new EditText(this);//创建EditText对象
tableRow2.addView(textView2);//TextView设置到第一行
tableRow2.addView(editText2);//EditText设置到第二行

Button button=new Button(this);
button.setText("登录系统");
tableRow3.setGravity(Gravity.CENTER);//设置第三行九中对齐
tableRow3.addView(button);

layout.addView(tableRow1);//将第一行数据添加到布局当中
layout.addView(tableRow2);//将第二行数据添加到布局当中
layout.addView(tableRow3);//将第三行数据添加到布局当中
super.setContentView(layout,tableTLayoutParams);
}

}




动态布局同样可以实现xml布局实现的效果

下节预报:相对布局RelativeLayout
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: