`
endual
  • 浏览: 3572707 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Android 使用 TableLayout 布局拉伸宽度(转)

 
阅读更多

android中tablelayout是挺好用的布局

我自己在学的过程中,特别是和listview合用。

 

有几个属性要注意下

android
:layout_span
="4"

就是说把4个列合并在一起(??)



Android 使用 TableLayout 布局拉伸宽度

布局文件

<?xml version="1.0
" encoding="utf-8
"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android
"
	android:orientation="vertical
" android:layout_width="fill_parent
"
	android:layout_height="fill_parent
">

	<TableLayout android:layout_width="match_parent
"
		android:layout_height="match_parent
">
		<TableRow>
			<EditText android:id="@+id/et_led
" android:layout_width="fill_parent
"
				android:digits="1234567890.+-*/=%\n\t()
" android:text=""
				android:layout_span="4
" />
		</TableRow>
		<TableRow>
			<Button android:text="(
" android:id="@+id/btn_LeftParenthesis
"></Button>
			<Button android:text=")
" android:id="@+id/btn_RightParenthesis
"></Button>
			<Button android:text="*
" android:id="@+id/btn_x
"></Button>
			<Button android:text="back
" android:id="@+id/btn_back
"></Button>
		</TableRow>
		<TableRow>
			<Button android:text="7
" android:id="@+id/btn_7
"></Button>
			<Button android:text="8
" android:id="@+id/btn_8
"></Button>
			<Button android:text="9
" android:id="@+id/btn_9
"></Button>
			<Button android:text="%
" android:id="@+id/btn_mod
"></Button>
		</TableRow>
		<TableRow>
			<Button android:text="4
" android:id="@+id/btn_4
"></Button>
			<Button android:text="5
" android:id="@+id/btn_5
"></Button>
			<Button android:text="6
" android:id="@+id/btn_6
"></Button>
			<Button android:text="/
" android:id="@+id/btn_div
"></Button>
		</TableRow>
		<TableRow>
			<Button android:text="1
" android:id="@+id/btn_1
"></Button>
			<Button android:text="2
" android:id="@+id/btn_2
"></Button>
			<Button android:text="3
" android:id="@+id/btn_3
"></Button>
			<Button android:text="-
" android:id="@+id/btn_sub
"></Button>
		</TableRow>
		<TableRow>
			<Button android:text="0
" android:id="@+id/btn_0
"></Button>
			<Button android:text=".
" android:id="@+id/btn_dot
"></Button>
			<Button android:text="+
" android:id="@+id/btn_plus
"></Button>
			<Button android:text="=
" android:id="@+id/btn_equal
"></Button>
		</TableRow>
	</TableLayout>

</LinearLayout>

布局效果:

image

显然这不能满足我们的期望,下面我们演示 使用 android:stretchColumns 来自动拉伸

我们这里简单的给 TableLayout 增加一个属性 android:stretchColumns="*" 表示所有列都要自动拉伸,以便适应屏幕宽度。

布局效果

image

 

 

它的值即可以是数字,也可以是*,注意数字是从0开始的,即:android:stretchColumns="1" 是设置 TableLayout所有行的第二列为扩展列。

上面我们会看到 第四列的按钮比其他列的按钮要宽,如果我们想都一样宽如何办呢?

一个简单办法:

在自动拉伸的基础上,把每一列的宽度都设置成一样,比如下面的代码:

<?
xml version="1.0" encoding="utf-8"?>

<
LinearLayout
 xmlns
:android
="http://schemas.android.com/apk/res/android"

	android
:orientation
="vertical"
 android
:layout_width
="fill_parent"

	android
:layout_height
="fill_parent"
>


	<
TableLayout
 android
:layout_width
="match_parent"

		android
:layout_height
="match_parent"
 		android
:stretchColumns
="*"
>

		<
TableRow
>

			<
EditText
 android
:id
="@+id/et_led"
 android
:layout_width
="fill_parent"

				android
:digits
="1234567890.+-*/=%\n\t()"
 android
:text
=""

				android
:layout_span
="4"
 />

		</
TableRow
>

		<
TableRow
>

			<
Button
 android
:text
="("
 android
:id
="@+id/btn_LeftParenthesis"

				android
:layout_width
="1dip"

>
</
Button
>

			<
Button
 android
:text
=")"
 android
:id
="@+id/btn_RightParenthesis"

				android
:layout_width
="1dip"
>
</
Button
>

			<
Button
 android
:text
="*"
 android
:id
="@+id/btn_x"

				android
:layout_width
="1dip"
>
</
Button
>

			<
Button
 android
:text
="back"
 android
:id
="@+id/btn_back"

				android
:layout_width
="1dip"
>
</
Button
>

		</
TableRow
>

		<
TableRow
>

			<
Button
 android
:text
="7"
 android
:id
="@+id/btn_7"
>
</
Button
>

			<
Button
 android
:text
="8"
 android
:id
="@+id/btn_8"
>
</
Button
>

			<
Button
 android
:text
="9"
 android
:id
="@+id/btn_9"
>
</
Button
>

			<
Button
 android
:text
="%"
 android
:id
="@+id/btn_mod"
>
</
Button
>

		</
TableRow
>

		<
TableRow
>

			<
Button
 android
:text
="4"
 android
:id
="@+id/btn_4"
>
</
Button
>

			<
Button
 android
:text
="5"
 android
:id
="@+id/btn_5"
>
</
Button
>

			<
Button
 android
:text
="6"
 android
:id
="@+id/btn_6"
>
</
Button
>

			<
Button
 android
:text
="/"
 android
:id
="@+id/btn_div"
>
</
Button
>

		</
TableRow
>

		<
TableRow
>

			<
Button
 android
:text
="1"
 android
:id
="@+id/btn_1"
>
</
Button
>

			<
Button
 android
:text
="2"
 android
:id
="@+id/btn_2"
>
</
Button
>

			<
Button
 android
:text
="3"
 android
:id
="@+id/btn_3"
>
</
Button
>

			<
Button
 android
:text
="-"
 android
:id
="@+id/btn_sub"
>
</
Button
>

		</
TableRow
>

		<
TableRow
>

			<
Button
 android
:text
="0"
 android
:id
="@+id/btn_0"
>
</
Button
>

			<
Button
 android
:text
="."
 android
:id
="@+id/btn_dot"
>
</
Button
>

			<
Button
 android
:text
="+"
 android
:id
="@+id/btn_plus"
>
</
Button
>

			<
Button
 android
:text
="="
 android
:id
="@+id/btn_equal"
>
</
Button
>

		</
TableRow
>

	</
TableLayout
>


</
LinearLayout
>

这时候的效果就成了:

注意比起最初的就多了2个设置

android :layout_width ="1dip" 和  android :stretchColumns ="*"

image

分享到:
评论

相关推荐

    Android中使用TableLayout布局设计办公电话一览表的代码清单.pdf

    这篇文档主要探讨如何利用TableLayout布局来设计一个办公电话一览表。以下是对给定内容的详细解读: 1. **TableLayout基本介绍** TableLayout是LinearLayout的扩展,它允许你创建类似HTML表格的布局。它包含多个...

    Android-TableLayout-Example

    在Android开发中,TableLayout是布局管理器的一种,它允许开发者创建类似电子表格的布局,包含行和列来展示数据。这个"Android-TableLayout-Example"项目显然是一个示例,用于演示如何在Android应用中有效地使用...

    Android移动应用开发表格布局TableLayout的特点.pdf

    总之,Android的TableLayout布局以其灵活的行和列管理,以及自适应的列宽和伸缩属性,为移动应用的界面设计提供了强大支持。理解并熟练掌握TableLayout的特性,可以帮助开发者构建出更加高效和用户友好的应用程序。

    Android学习笔记13:表格布局管理器TableLayout

    1. `android:stretchColumns`:指定哪些列应该拉伸以填满TableLayout的宽度。值是一个用逗号分隔的列索引列表。 2. `android:shrinkColumns`:指定哪些列可以缩小,以适应TableLayout的宽度。同样,值是一个列索引...

    Android布局之表格布局TableLayout详解

    Android 布局之表格布局 TableLayout 详解 Android 中的表格布局 TableLayout 是一种常用的布局方式,它可以按照行列的形式管理子控件,每一行为一个 TableRow 对象。下面将对 TableLayout 的用法和属性进行详细的...

    Android布局之TableLayout表格布局

    TableLayout是Android布局系统中的一个组件,用于组织和展示内容以表格的形式,它按照行列的方式来管理子视图。在Android应用开发中,TableLayout常用于创建具有结构化的数据展示或者复杂的用户界面,尤其是在需要对...

    android中的布局

    本文将深入探讨Android中的布局机制,尤其是`TableLayout`的使用和实现细节。 首先,Android提供了多种布局管理器,如LinearLayout、RelativeLayout、FrameLayout和GridLayout等,每种都有其特定的用途。`...

    TableLayout背景

    TableLayout是Android开发中一种布局管理器,它允许开发者以表格的形式组织UI元素。在Android应用设计中,TableLayout常用于展示数据或者创建有规则排列的界面,比如设置界面、菜单选项等。以下是对TableLayout的...

    Android 表格布局TableLayout示例详解

    总结来说,Android的TableLayout布局提供了一种灵活的方式来构建二维的UI结构,通过调整不同的属性,可以实现各种复杂的表格布局效果。通过理解并熟练运用这些属性,开发者可以更好地满足用户界面的需求,创建出清晰...

    详解Android TableLayout表格布局

    在Android开发中,TableLayout是一种常用的布局方式,它允许开发者以表格的形式组织用户界面元素。TableLayout继承自LinearLayout,因此它的基本布局特性是线性的,但增加了表格的特性,使得内容可以按照行列的方式...

    详解Android TableLayout中stretchColumns、shrinkColumns的用法

    例如,如果你有一个三列的表格,第一列固定宽度,第三列内容自适应宽度,那么设置`android:stretchColumns="1"`将让第二列根据需要扩展,以填满整个TableLayout的宽度。可以设置多个列号,用逗号分隔,如`android:...

    TableLayout使用方法

    需要注意的是,尽管 TableLayout 在早期版本的 Android 应用中较为常用,但对于复杂的数据展示,建议考虑使用更现代的布局方案,如 RecyclerView 配合 GridLayoutManager 或 GridLayoutManager 来实现类似的效果,以...

    TableLayout数据显示

    TableLayout是Android开发中一种布局管理器,它用于在屏幕上以表格形式排列视图组件。在Android应用设计中,TableLayout常被用来构建有结构的数据展示或者创建复杂的用户界面。以下是对TableLayout的详细说明: 1. ...

    TableLayout_Project

    《TableLayout_Project——深入解析Android TableLayout布局》 在Android开发中,TableLayout是一个非常实用的布局工具,它允许开发者创建表格样式的用户界面。这个名为"TableLayout_Project"的项目,显然着重于...

    tablelayout实现表格效果

    在Android开发中,TableLayout是布局管理器的一种,它允许开发者创建类似电子表格的布局,非常适合展示数据或者组织内容。本篇文章将详细讲解如何利用TableLayout在Android中实现表格效果。 **1. TableLayout基本...

    Android开发之TableLayout表格布局

    在Android开发中,TableLayout是布局管理器的一种,它允许开发者以表格的形式组织UI组件。这种布局方式对于创建类似表格的用户界面非常有用,比如展示数据列表或者进行设置选项的排列。下面我们将详细探讨...

    用TableLayout瀑布效果(参差不齐的排版)

    1. **设置TableLayout**:在XML布局文件中,首先声明`&lt;TableLayout&gt;`,并可以设置其属性,如`android:stretchColumns`,这将指定哪些列应该被拉伸以填充可用空间。 ```xml &lt;TableLayout android:id="@+id/table_...

    Android 六大布局

    `android:stretchColumns`属性可以拉伸指定列以填满表格宽度,而`android:shrinkColumns`则允许列收缩以适应内容。 这六大布局各有特点,开发者应根据实际需求选择合适的布局。在现代Android开发中,常常混合使用...

Global site tag (gtag.js) - Google Analytics