Android UI 中如何利用表格进行布局

本文介绍了如何在Android应用中使用表格布局创建界面。虽然不如线性布局灵活,但表格布局能实现更对称、整洁的界面设计。通过分析布局需求,创建一个四行两列的表格布局,背景为灰色,内容区域为白色。文中详细讲解了布局步骤,包括XML文件的创建、样式定义,以及如何实现语言切换功能,提供了完整的代码示例。
摘要由CSDN通过智能技术生成

         对于控件布局中的表格布局,我感觉没有线性布局灵活。在表格布局中只能通过 TableRow 属性来控制它的行而列的话里面有几个控件就是几列(一般情况)。我下面就用表格布局操作完成下面图例。


一、首先要先要先创建一个表格布局的文件。创建成功后进入程序页面,然后点击下图中所示位置文件,对内容进行编写。


  对于表格布局,在编写开始之前要先分析如何进行布局才能使页面更加美观整洁,表格布局的一个好处就是对于空间的利用可以更加对称,更容易控制。

  在分析好如何布局后,我们开始进行布局。首先,这个案例的背景是一个灰色的,整个页面充斥着屏幕空间,一共是四行两列,分析完这些后,我们就可以作出一个大体的布局。

<TableLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/darker_gray"   /**背景颜色为灰色
    android:orientation="vertical"                    /**使表格垂直排列
    android:stretchColumns="*"
    tools:context=".MainActivity">
<TableRow
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"                  /**使每一行中的控件水平排列
    android:layout_weight="1"></TableRow>
<TableRow
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:layout_weight="1"></TableRow>
<TableRow
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:layout_weight="1"></TableRow>

<TableRow
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:layout_weight="1"></TableRow>


以下是对上面一些代码的解释:
android:layout_weight="1"    //这一句是对每一行所占空间的大小进行设定,每个所占的比重都是“1”,所以平均分布。
android:stretchColumns="*"  //这句话是为了让 table 铺满纵向屏幕。android:stretchColumns字段是为了 让 table 自动铺满屏幕android:s
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值