• 一手遮天 Android


    项目地址 https://github.com/webabcd/AndroidDemo
    作者 webabcd

    一手遮天 Android - view(布局类): TableLayout 表格布局

    示例如下:

    /view/layout/TableLayoutDemo1.java

    /**
     * TableLayout - 表格布局控件
     */
    
    package com.webabcd.androiddemo.view.layout;
    
    import androidx.appcompat.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.ViewGroup;
    import android.widget.TableLayout;
    import android.widget.TableRow;
    import android.widget.TextView;
    
    import com.webabcd.androiddemo.R;
    
    public class TableLayoutDemo1 extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_view_layout_tablelayoutdemo1);
    
            // 演示如何在 java 中控制 TableLayout 布局,仅代码演示,没有对应的显示效果
            sample();
        }
    
        private void sample() {
            TableLayout tableLayout = new TableLayout(this);
            // 对应 xml 中的 stretchColumns
            tableLayout.setColumnStretchable(0, true); // tableLayout.setStretchAllColumns(true);
            // 对应 xml 中的 shrinkColumns
            tableLayout.setColumnShrinkable(0, true); // tableLayout.setShrinkAllColumns(true);
            // 对应 xml 中的 collapseColumns
            tableLayout.setColumnCollapsed(0, true);
    
            TextView textView = new TextView(this);
            // 第 1 个参数对应 xml 中的 layout_width(像素值)
            // 第 2 个参数对应 xml 中的 layout_height(像素值)
            TableRow.LayoutParams layoutParams = new TableRow.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            // 对应 xml 中的 layout_span
            layoutParams.span = 1;
            // 对应 xml 中的 layout_column
            layoutParams.column = 1;
            textView.setLayoutParams(layoutParams);
    
            tableLayout.addView(textView);
        }
    }
    
    

    /layout/activity_view_layout_tablelayoutdemo1.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <!--
            TableLayout - 表格布局控件
                stretchColumns - 需要被拉伸的列,多个用“,”隔开,全部列都需要被拉伸则可以设置为“*”
    
                layout_span - 设置该控件的水平方向上的单元格合并数
                layout_column - 设置该控件位于指定列
    
            注:
            1、TableLayout 是通过 TableRow 来一行一行地显示数据的
            2、TableLayout 和 TableRow 均继承自 LinearLayout(相当于 TableLayout 是竖排的 LinearLayout;TableRow 是横排的 LinearLayout)
        -->
        <TableLayout android:layout_width="match_parent" android:layout_height="wrap_content"
            android:stretchColumns="2">
            <TableRow>
                <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/white"
                    android:text="1111111111" android:background="@color/red" />
                <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/white"
                    android:text="2222222222" android:background="@color/green" />
                <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/white"
                    android:text="3333333333" android:background="@color/blue" />
            </TableRow>
            <TableRow>
                <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/white"
                    android:text="1111111111" android:background="@color/red"
                    android:layout_column="1" />
                <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/white"
                    android:text="2222222222" android:background="@color/green" />
            </TableRow>
            <TableRow>
                <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/white"
                    android:text="1111111111" android:background="@color/red"
                    android:layout_span="2" />
                <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/white"
                    android:text="2222222222" android:background="@color/green" />
            </TableRow>
        </TableLayout>
    
        <!--
            TableLayout - 表格布局控件
                shrinkColumns - 允许被收缩的列,多个用“,”隔开,全部列都需要被收缩则可以设置为“*”
                collapseColumns - 需要隐藏的列,多个用“,”隔开
        -->
        <TableLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp"
            android:collapseColumns="0,2"
            android:shrinkColumns="1">
            <TableRow>
                <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/white"
                    android:text="1111111111" android:background="@color/red" />
                <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/white"
                    android:text="2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222" android:lines="1" android:background="@color/green" />
                <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/white"
                    android:text="3333333333" android:background="@color/blue" />
                <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/white"
                    android:text="4444444444" android:background="@color/orange" />
                <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="@color/white"
                    android:text="5555555555" android:background="@color/purple" />
            </TableRow>
        </TableLayout>
    
    </LinearLayout>
    

    项目地址 https://github.com/webabcd/AndroidDemo
    作者 webabcd

  • 相关阅读:
    vue-cli 打包编译 -webkit-box-orient: vertical 被删除解决办法
    vue静态文件处理
    vue项目关闭eslint检查
    Mac 桌面软件开发基础问答
    Mac App开发
    mac os app 开发
    ffmpeg学习目录收集
    vue中html模板使用绑定的全局函数
    软件版本标识
    shell之ulimit应该注意的事项
  • 原文地址:https://www.cnblogs.com/webabcd/p/android_view_layout_TableLayoutDemo1.html
Copyright © 2020-2023  润新知