search
HomeWeb Front-endCSS TutorialCSS: table-layout table layout detailed explanation

CSS: table-layout table layout detailed explanation

May 12, 2018 pm 04:56 PM
csstable-layout

1. Introduction to Tablelayout

The Tablelayout class manages controls in the form of rows and columns. Each row is a TableRow object or a View control. When it is a TableRow object, you can add sub-controls under the TableRow. By default, each sub-control occupies one column. When it is a View, the View will occupy one row.

2. Determination of the number of rows and rows of TableLayout (maximum principle)

The number of rows of TableLayout is directly specified by the developer, that is, how many TableRow objects (or View controls) there are. How many rows are there. The number of columns of TableLayout is equal to the number of columns of TableRow that contains the most child controls. If the first TableRow contains 2 sub-controls, the second TableRow contains 3, and the third TableRow contains 4, then the number of columns of the TableLayout is 4.

3. TableLayout can The set attributes are explained in detail

Overall, the attributes of this TableLayout are similar to the attributes of the Table tag in HTML. His various properties are as follows:

The settable properties of TableLayout include global properties and cell properties.

1. Global attributes, also known as column attributes, have the following three parameters:

android:stretchColumns Set stretchable columns. The column can stretch row-wise, occupying up to an entire row.

android:shrinkColumns Set shrinkable columns. When the content of the sub-control in the column is too much and has been crowded into the row, the content of the sub-control will be displayed in the direction of the column.

android:collapseColumns Set the columns to be hidden.

Example:

android:stretchColumns="0" Column 0 can be stretched

android:shrinkColumns="1,2" Columns 1 and 2 can be shrunk

Android: Collapsecolumns = "*" Hide all lines

:: column can also have StretchColumns and ShrinkColumns at the same time. its content. (This is not a real multi-row, but the system automatically adjusts the layout_height of the row as needed)

2. Cell attributes, there are the following 2 parameters:

android:layout_column Specify the unit In which column the cell is displayed

android:layout_span Specifies the number of columns occupied by the cell (if not specified, it is 1)

Example:

android:layout_column=" 1" This control is displayed in column 1

android:layout_span="2" This control occupies 2 columns

Note: A control can also have these two characteristics at the same time.

                <tablelayout>

                    <tablerow>

                        <textview></textview>

                        <imageview></imageview>
                    </tablerow>
                </tablelayout>

The corresponding renderings of its operation are as follows:

Summary, the layout method of TableLayOut is generally used less, but like the above setting interface, TableLayout The layout is very useful.

1: TableLayout is a table layout. Let’s start with the example:

Define an xml layout file tablelayout.xml:

The content is as follows:

<?xmlversion="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"
  android:gravity="center_horizontal">
  <TableLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:stretchColumns="0,1,2"   
    android:shrinkColumns="1,2"
   >
   <TableRow>       
       <TextView           
           android:layout_column="1"           
           android:text="姓名"           
           android:gravity="center"
           android:background="#BBFFFF"/>       
       <TextView           
           android:text="基本信息"           
           android:gravity="center"
           android:background="#CD00CD"/>   
       <TextView           
           android:text="大大的人人儿呢"           
           android:gravity="center"
           android:background="#A020F0"/>   
    </TableRow>
  </TableLayout>
</LinearLayout>
packagecom.test;
 
importandroid.app.Activity;
importandroid.os.Bundle;
 
publicclass TableLayoutAndRowDemo extendsActivity {
     
    @Override
    protectedvoid onCreate(Bundle savedInstanceState) {
        //
 TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tablelayout);
    }
 
}

?

Effect:

##Content explanation:

TableLayout, Table layout uses rows and columns to manage UI components. TableLayout does not need to explicitly declare how many rows and columns there are. Instead, it controls the number of rows and columns of the table by adding TableRow and other components.

vEvery time you add a TableRow to TableLayout, you are adding a row to the table. TableRow is also a container. You can add components to TableRow. Each time you add a component, you add a column.
vIf you add a component directly to TableLayout, it is considered that this component occupies one row.
vThe width of the columns in the table layout is the width of the widest component in each column.

 android:collapseColumns="1---隐藏该TableLayout里的TableRow的列1,即第2列(从0开始计算),若有多列要隐藏,用“,”隔开。setColumnCollapsed(int,boolean)

android:stretchColumns="0,1,2"----设置列0、1、2为可伸展列。setColumnCollapsed(int,boolean)

android:shrinkColumns="1,2"-----设置列1、2为可收缩列。当LayoutRow里面的控件还没有布满布局时,shrinkColumns不起作用 setColumnCollapsed(int,boolean)

android:background="@drawable/picture_name"----本例中没有涉及此属性,它是要设置当前view 的背景图片,图片文件应该放在res文件夹下。

android:layout_column="1":表示控件放在标号为1的列上,标号是从0开始的 
android:layout_span表示一个控件占几列空间 
可以用”*”来表示所有列,同一列可以同时设置为shrinkable和stretchable

 函数:

publicTableLayout (Context context);
//为给定的上下文创建表格布局。
//参数  
 context  应用程序上下文
publicTableLayout (Context context, AttributeSet attrs)
//使用指定的属性集合为给定的上下文创建表格布局。
//参数   
 context  应用程序上下文     attrs        属性集合
publicvoid addView (View child)
publicvoid addView (View child, intindex)
publicvoid addView (View child, intindex, ViewGroup.LayoutParams params)
//添加子视图。如果子视图没有设置布局参数,则使用视图组(ViewGroup)的布局参数为//该视图布局。index 
 子视图加入的位置索引
publicvoid addView (View child, ViewGroup.LayoutParams params)
//使用指定的布局参数添加子视图。
//参数   
 child         添加的子视图   params  设置到子视图上的布局参数
publicTableLayout.LayoutParams generateLayoutParams (AttributeSet attrs)
//返回一组基于提供的属性集合的布局参数集合。
//参数
//attrs       
 用于生成布局参数的属性集
//返回值       
 ViewGroup.LayoutParams或其子类的实例

? 

publicboolean isColumnCollapsed (intcolumnIndex)
//返回指定列的折叠状态。
//参数  
 columnIndex  列索引
//返回值    
 折叠时为true;否则为false 
 
publicboolean isColumnShrinkable (intcolumnIndex)
//返回指定的列是否可收缩。
//参数     
 columnIndex  列索引
//返回值   
 如果列可以收缩,返回true;否则返回false
publicboolean isColumnStretchable (intcolumnIndex)
//返回指定的列是否可拉伸。
//参数      
 columnIndex  列索引
//返回值    
 如果列可以拉伸,返回true;否则返回false

?

publicboolean isShrinkAllColumns ()
//指示是否所有的列都是可收缩的。
//返回值
//
 如果所有列都可收缩,返回true;否则返回false
publicboolean isStretchAllColumns ()
//指示是否所有的列都是可拉伸的。
//返回值   
 如果所有列都可拉伸,返回true;否则返回false
publicvoid requestLayout ()
//当某些变更导致视图的布局失效时调用该方法。该方法按照视图树的顺序调用。
 
  
publicvoid setColumnCollapsed (intcolumnIndex, booleanisCollapsed)
 
  //折叠或恢复给定列。折叠时,列从屏幕上消失,其空间由其它列占用。
 当列属于 TableRow 时才可以进行折叠/恢复操作。
 
  //调用该方法会请求布局操作。
相关XML属性   
 android:collapseColumns
参数   
 columnIndex  列索引
isCollapsed    
 折叠时为true;否则为false

?

publicvoid setColumnShrinkable (intcolumnIndex, booleanisShrinkable)
//设置指定列是否可收缩。当行太宽时,表格可以收缩该列以提供更多空间。
//调用该方法会请求布局操作。
//相关XML属性   
 android:shrinkColumns
//参数   
 columnIndex  列索引
//        
 isShrinkable    如果列可以收缩,设为真;否则设为假。默认是假。
publicvoid setColumnStretchable (intcolumnIndex, booleanisStretchable)
//设置指定列是否可拉伸。可拉伸时,列会尽可能多的占用行中的可用空间。
//调用该方法会请求布局操作。
//相关XML属性   
 android:stretchColumns
//参数   
 columnIndex  列索引
//        
 isStretchable 如果列可以拉伸,设为真;否则设为假.默认是假
publicvoid setOnHierarchyChangeListener (ViewGroup.OnHierarchyChangeListener listener)
注册当从视图中添加或移除子视图时发生的回调函数。
参数
 
  listener 
 层次结构变更时执行的回调函数
publicvoid setShrinkAllColumns (booleanshrinkAllColumns)
 
  标记所有列为可收缩的便利的方法。
 
  相关XML属性  
 android:shrinkColumns
  参数  
 shrinkAllColumns    如果标记所有列为可收缩时为true
 
 publicvoid setStretchAllColumns (booleanstretchAllColumns)


标记所有列为可拉伸的便利的方法。
相关XML属性  
 android:stretchColumns
参数   
 stretchAllColumns           如果标记所有列为可拉伸时为true

?


?

 三:TableLayout实现边框

  为了醒目,需要给TableLayout设定边框来区分不同的表格中的信息:

主要是通过设定TableLayout、TableRow 、View颜色反衬出边框的颜色。

例如TableLayout的android:layout_margin="2dip"设置为这个数字 ,在指定一个背景色android:background="#00ff00",它里面的颜色也是这样子的设置,就可以呈现出带边框的效果了。

 (2)关于分割线: 
我们可以通过在两个TableRow之间添加一个,并设置它的layout_height="1dip"和background来解决。但这样就多产生了一个View,有点浪费。 
其实还可以设置TableRow的bottomMargin=1来产生一条分割线。 
  3.滚动:

关于TableLayout和HorizontalScrollView一起使用时的宽度问题 
我有一个TableLayout,它的内容是动态生成的。我遇到了下面的问题: 
当动态生成的一行的内容太长时,靠右边的内容会被遮住了。于是我想要这个TableLayout在横向上可以滚动。 
解决的办法是,用HorizontalScrollView包装TableLayout,这样,当内容很长时,就会出现横向滚动条。 
像这样: 

?>
<HorizontalScrollView 
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent"> 
  <TableLayout 
  android:id="@+id/tl_forcast_result" 
  android:layout_width="fill_parent" 
  android:layout_height="wrap_content" 
  android:padding="10dp">   
  </TableLayout> 
 </HorizontalScrollView>

?

  但此时又出现了另一个问题,加上HorizontalScrollView后,虽然我已经设了TableLayout的宽度是fill_parent。但当内容较少时,TableLayout还是根据内容自适应宽度,不能满屏。 
此时,需要设置一个属性就能解决问题了。设置HorizontalScrollView的android:fillViewport="true"。也就是设置是否将HorizontalScrollView的内容宽度拉伸以适应视口(viewport)

The above is the detailed content of CSS: table-layout table layout detailed explanation. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
What is CSS Grid?What is CSS Grid?Apr 30, 2025 pm 03:21 PM

CSS Grid is a powerful tool for creating complex, responsive web layouts. It simplifies design, improves accessibility, and offers more control than older methods.

What is CSS flexbox?What is CSS flexbox?Apr 30, 2025 pm 03:20 PM

Article discusses CSS Flexbox, a layout method for efficient alignment and distribution of space in responsive designs. It explains Flexbox usage, compares it with CSS Grid, and details browser support.

How can we make our website responsive using CSS?How can we make our website responsive using CSS?Apr 30, 2025 pm 03:19 PM

The article discusses techniques for creating responsive websites using CSS, including viewport meta tags, flexible grids, fluid media, media queries, and relative units. It also covers using CSS Grid and Flexbox together and recommends CSS framework

What does the CSS box-sizing property do?What does the CSS box-sizing property do?Apr 30, 2025 pm 03:18 PM

The article discusses the CSS box-sizing property, which controls how element dimensions are calculated. It explains values like content-box, border-box, and padding-box, and their impact on layout design and form alignment.

How can we animate using CSS?How can we animate using CSS?Apr 30, 2025 pm 03:17 PM

Article discusses creating animations using CSS, key properties, and combining with JavaScript. Main issue is browser compatibility.

Can we add 3D transformations to our project using CSS?Can we add 3D transformations to our project using CSS?Apr 30, 2025 pm 03:16 PM

Article discusses using CSS for 3D transformations, key properties, browser compatibility, and performance considerations for web projects.(Character count: 159)

How can we add gradients in CSS?How can we add gradients in CSS?Apr 30, 2025 pm 03:15 PM

The article discusses using CSS gradients (linear, radial, repeating) to enhance website visuals, adding depth, focus, and modern aesthetics.

What are pseudo-elements in CSS?What are pseudo-elements in CSS?Apr 30, 2025 pm 03:14 PM

Article discusses pseudo-elements in CSS, their use in enhancing HTML styling, and differences from pseudo-classes. Provides practical examples.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools