首页 > 其他分享 >Android 使用 TableLayout 布局拉伸宽度

Android 使用 TableLayout 布局拉伸宽度

时间:2023-06-22 19:32:51浏览次数:39  
标签:拉伸 布局 stretchColumns 宽度 TableLayout Android android

布局文件


<?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>


布局效果:

Android 使用 TableLayout 布局拉伸宽度_xml

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

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

布局效果

Android 使用 TableLayout 布局拉伸宽度_android_02

它的值即可以是数字,也可以是*,注意数字是从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="*"

Android 使用 TableLayout 布局拉伸宽度_android_03

标签:拉伸,布局,stretchColumns,宽度,TableLayout,Android,android
From: https://blog.51cto.com/u_15588078/6535335

相关文章

  • Android的进程,线程模型
    Android包括一个应用程序框架、几个应用程序库和一个基于Dalvik虚拟机的运行时,所有这些都运行在Linux内核之上。通过利用Linux内核的优势,Android得到了大量操作系统服务,包括进程和内存管理、网络堆栈、驱动程序、硬件抽象层、安全性等相关的服务。 有关Java虚拟机跟进程,线......
  • Android入门--写一个最简单的计算器
    这里将写的计算器是借用了下面写的计算器,并把其中需要注意的地方一一补充罗列:http://www.iteye.com/topic/141029需要演示的结果:之前假设已经配置好Android开发环境,如果没有,请参看下面2篇文章:Win7配置Android开发环境ubuntu10.10下安装android2.2开发环境 第一步,新......
  • ubuntu10.10 下安装android 2.2开发环境
    一、安装Java6安装jre/jdksudoapt-getinstallsun-java6-binsun-java6-jresun-java6-jdk使用这个命令安装后,默认是安装在/usr/lib/jvm/ 目录下,以我为例,我这里是/usr/lib/jvm/java-6-sun-1.6.0.24目录,/usr/lib/jvm/下还有一个快捷方式java-6-sun有时候会安装多个jdk,......
  • Kotlin入门|Android Kotlin 初学者学习网站+最新学习资源
    Kotlin是一门可以运行在Java虚拟机、Android和浏览器上的静态语言,它与Java100%兼容。如果你对Java非常熟悉,一般上手Kotlin也会比较容易。在我从事Android开发的多年来,也积累了很多专业的学习网站和宝贵的学习资源,现无偿分享给大家,不求面面俱到,只希能给各位Android开发者和带来......
  • Android 组件化开发的新浪潮,成为了最受欢迎的框架, 有一种架构叫组件化,你还不会?
    有一种架构叫组件化,你还不会?组件化作为客户端项目架构侧一个非常重要的方案,近年来业界也在不断的探索以及各大厂都在不断的实践。每个大厂内部的各个Android开发团队也在尝试和实践不同的组件化方案,并且在组件化通信框架上也有很多高质量的产出。阿里面试最近听一位坚守在阿里的同......
  • Android内存优化分析总结,这一篇就够了
    一、内存优化概念1.1为什么要做内存优化?内存优化一直是一个很重要但却缺乏关注的点,内存作为程序运行最重要的资源之一,需要运行过程中做到合理的资源分配与回收,不合理的内存占用轻则使得用户应用程序运行卡顿、ANR、黑屏,重则导致用户应用程序发生OOM(outofmemory)崩溃。在你认真跟......
  • 最新《Android Framework开发文档》(经典Binder、Handler、AMS等面试题加解析)
    Android架构从从顶层到底层分别为应用程序层、应用程序框架层、运行层(系统Native库和Android运行时环境)和Linux内核层四部分。Framework即应用框架层,是Android架构的关键组成部分,为应用提供各种api和组件来支持开发。如今行业趋于饱和,开发技术越来越卷,Framework也已逐渐成为高薪......
  • 那些年Android UI开发中所遇到的各种坑
    1.软键盘隐藏问题问题描述:Activity按下返回调用finish()方法后,界面已经销毁,但是软键盘依然还留在屏幕上,这让当前正在显示的Activity没有输入框的完全没法看,非常严重的视觉影响。尝试方案:寻找各种方法去隐藏软键盘,网上各种找。思路是在活动退出时,会调用onDestroy()方法。销毁界面,在......
  • Android系统服务 AMS 启动流程
    背景当SystemServer启动的时候,从Zygote进程fork()出SystemServer进程,经过初始化后,会通过反射调用SystemServer.java的mian()方法,其中会启动一系列系统服务。AMS就是其中的一个。一、缘起SystemServer进程SystemServer的main():/***Themainentrypointfromzygote......
  • Android 启动优化实践:将启动时间降低 50%
    前言作为APP体验的重要环节,启动速度是各个技术团队关注的重点。几百毫秒启动耗时的增减都会影响用户的体验,并直接反应在留存上。心遇APP作为一款用于满足中青年市场用户社交诉求的应用,对各个性能层次的手机型号,都要求有良好的启动体验。因此,随着用户量快速增长,启动优化作为一个......