站长之家网站排行榜,番禺开发网站费用,南京建设银行公积金查询网站,国内比百度好的搜索引擎虽然线性布局既能在水平方向排列#xff0c;也能在垂直方向排列#xff0c;但它不支持多行多列的布局方式#xff0c;只支持单行#xff08;水平排列#xff09;或单列#xff08;垂直排列#xff09;的布局方式。若要实现类似表格那样的多行多列形式#xff0c;可采用…虽然线性布局既能在水平方向排列也能在垂直方向排列但它不支持多行多列的布局方式只支持单行水平排列或单列垂直排列的布局方式。若要实现类似表格那样的多行多列形式可采用网格 布局GridLayout网格布局默认从左往右、从上到下排列它先从第一行从左往右放置下级视图塞满之后另起一行放置 其余的下级视图如此循环往复直至所有下级视图都放置完毕。为了判断能够容纳几行几列网格布局 新增了android:columnCount与android:rowCount两个属性
一columnCount指定了网格的列数 即每行能放多少个视图
二rowCount指定了网格的行数即每列能放多少个视图。
下面是运用网格布局的XML布局样例它规定了一个两行两列的网格布局且内部容纳四个文本视图。
XML文件内容如下所示
!-- 根布局为两行两列的网格布局其中列数由columnCount指定行数由rowCount指定 --GridLayout xmlns:androidhttp://schemas.android.com/apk/res/androidandroid:layout_widthmatch_parentandroid:layout_heightmatch_parentandroid:columnCount2android:rowCount2TextViewandroid:layout_width180dpandroid:layout_height60dpandroid:gravitycenterandroid:background#ffccccandroid:text浅红色android:textColor#000000android:textSize17sp /TextViewandroid:layout_width180dpandroid:layout_height60dpandroid:gravitycenterandroid:background#ffaa00android:text橙色android:textColor#000000android:textSize17sp /TextViewandroid:layout_width180dpandroid:layout_height60dpandroid:gravitycenterandroid:background#00ff00android:text绿色android:textColor#000000android:textSize17sp /TextViewandroid:layout_width180dpandroid:layout_height60dpandroid:gravitycenterandroid:background#660066android:text深紫色android:textColor#000000android:textSize17sp /
/GridLayout
在一个新建的活动页面加载上述布局运行App观察到的界面如下图所示 由上图可见App界面的第一行分布着浅红色背景与橙色背景的文本视图第二行分布着绿色背景与 深紫色背景的文本视图说明利用网格布局实现了多行多列的效果。
运行前记得修改清单文件
完整代码
Java
package com.example.chapter03;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;public class GridLayoutActivity extends AppCompatActivity {Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_grid_layout);}
}
布局文件
?xml version1.0 encodingutf-8?
GridLayout xmlns:androidhttp://schemas.android.com/apk/res/androidandroid:layout_widthmatch_parentandroid:layout_heightmatch_parentandroid:columnCount2android:rowCount2TextViewandroid:layout_width0dpandroid:layout_columnWeight1android:layout_height60dpandroid:background#ffccccandroid:text浅红色android:gravitycenterandroid:textColor#000000android:textSize17dp/TextViewandroid:layout_width0dpandroid:layout_height60dpandroid:layout_columnWeight1android:background#ffaa00android:text橙色android:gravitycenterandroid:textColor#000000android:textSize17dp/TextViewandroid:layout_width0dpandroid:layout_height60dpandroid:layout_columnWeight1android:background#00ff00android:text绿色android:gravitycenterandroid:textColor#000000android:textSize17dp/TextViewandroid:layout_width0dpandroid:layout_columnWeight1android:layout_height60dpandroid:background#660066android:text深紫色android:gravitycenterandroid:textColor#000000android:textSize17dp//GridLayout
感谢观看