zoukankan      html  css  js  c++  java
  • 一手遮天 Android

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

    一手遮天 Android - view(布局类): GridLayout 网格布局

    示例如下:

    /view/layout/GridLayoutDemo1.java

    /**
     * GridLayout - 网格布局控件
     */
    
    package com.webabcd.androiddemo.view.layout;
    
    import androidx.appcompat.app.AppCompatActivity;
    import android.os.Bundle;
    import android.widget.GridLayout;
    import android.widget.TextView;
    
    import com.webabcd.androiddemo.R;
    
    public class GridLayoutDemo1 extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_view_layout_gridlayoutdemo1);
    
            // 演示如何在 java 中控制 GridLayout 布局,仅代码演示,没有对应的显示效果
            sample();
        }
    
        private void sample() {
            GridLayout gridLayout = new GridLayout(this);
            // 对应 xml 中的 orientation
            gridLayout.setOrientation(GridLayout.VERTICAL);
            // 对应 xml 中的 columnCount
            gridLayout.setColumnCount(10);
            // 对应 xml 中的 rowCount
            gridLayout.setRowCount(10);
    
            TextView textView = new TextView(this);
            // 第 1 个参数对应 xml 中的 layout_row
            // 第 2 个参数对应 xml 中的 layout_rowSpan
            // 第 3 个参数对应 xml 中的 layout_gravity
            // 第 4 个参数对应 xml 中的 layout_rowWeight
            GridLayout.Spec rowSpec = GridLayout.spec(0, 2, GridLayout.FILL,1.0f);
            // 第 1 个参数对应 xml 中的 layout_column
            // 第 2 个参数对应 xml 中的 layout_columnSpan
            // 第 3 个参数对应 xml 中的 layout_gravity
            // 第 4 个参数对应 xml 中的 layout_columnWeight
            GridLayout.Spec columnSpec = GridLayout.spec(0, 2, GridLayout.FILL, GridLayout.UNDEFINED);
            GridLayout.LayoutParams layoutParams = new GridLayout.LayoutParams(rowSpec, columnSpec);
            textView.setLayoutParams(layoutParams);
    
            gridLayout.addView(textView);
        }
    }
    
    

    /layout/activity_view_layout_gridlayoutdemo1.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">
    
        <!--
            GridLayout - 网格布局控件
                orientation - 控件的排列方向(horizontal 或 vertical)
                columnCount - 最大列数,实际列数会小于等于此值(会取一个保证横向能显示得下的值),当排列方向为 horizontal 时此值有用(行数不限制)
                rowCount - 最大行数,实际行数会小于等于此值(会取一个保证竖向能显示得下的值),当排列方向为 vertical 时此值有用(列数不限制)
    
                layout_rowSpan - 设置该控件的行合并数(如果需要拉伸控件则将 layout_gravity 设置为 fill 即可)
                layout_columnSpan - 设置该控件的列合并数(如果需要拉伸控件则将 layout_gravity 设置为 fill 即可)
                layout_row - 设置该控件位于指定行
                layout_column - 设置该控件位于指定列
    
            注:GridLayout 继承自 ViewGroup,其会把自身划分为“行数 * 列数”个网格
        -->
        <GridLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:columnCount="3">
    
            <Button android:text="1"
                android:layout_columnSpan="2"
                android:layout_gravity="fill" />
            <Button android:text="2" />
            <Button android:text="3"
                android:layout_rowSpan="2"
                android:layout_gravity="fill" />
            <Button android:text="4" />
            <Button android:text="5" />
            <Button android:text="6"
                android:layout_row="2"
                android:layout_column="2"/>
            <Button android:text="7" />
            <Button android:text="8" />
            <Button android:text="9" />
    
        </GridLayout>
    
    
        <!--
            GridLayout - 网格布局控件
                layout_rowWeight - 控件在行方向(垂直方向)上所占空间的比重
                layout_columnWeight - 控件在列方向(水平方向)上所占空间的比重
        -->
        <GridLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:rowCount="3">
    
            <Button android:text="1" android:layout_columnWeight="1" android:layout_rowWeight="1" />
            <Button android:text="2" android:layout_columnWeight="1" android:layout_rowWeight="2" />
            <Button android:text="3" android:layout_columnWeight="1" android:layout_rowWeight="1" />
            <Button android:text="4" android:layout_columnWeight="2" android:layout_rowWeight="1" />
            <Button android:text="5" android:layout_columnWeight="2" android:layout_rowWeight="2" />
            <Button android:text="6" android:layout_columnWeight="2" android:layout_rowWeight="1" />
    
        </GridLayout>
    
    </LinearLayout>
    

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

  • 相关阅读:
    POJ3094 UVALive3594 HDU2734 ZOJ2812 Quicksum【进制】
    UVALive5583 UVA562 Dividing coins
    POJ1979 HDU1312 Red and Black【DFS】
    POJ1979 HDU1312 Red and Black【DFS】
    POJ2386 Lake Counting【DFS】
    POJ2386 Lake Counting【DFS】
    HDU4394 Digital Square
    HDU4394 Digital Square
    UVA213 UVALive5152 Message Decoding
    UVA213 UVALive5152 Message Decoding
  • 原文地址:https://www.cnblogs.com/webabcd/p/android_view_layout_GridLayoutDemo1.html
Copyright © 2011-2022 走看看