当前位置: 首页 > 科技观察

HarmonyOSHarmonyOSJavaUITableLayout实例

时间:2023-03-14 09:53:20 科技观察

更多内容请访问:Harmonyos技术社区https://harmonyos.51cto.com/#zzTableLayout简介TableLayout是表格布局的意思,也可以叫网格布局,可以让我们进行排列以表格的方式,即以行和列的方式显示组件。TableLayout提供了一个用于在表格中对齐和排列组件的界面。您可以配置表格的排列方式、行数和列数以及组件的位置。常用的属性ohos:alignment_type表示设置网格布局中的对齐方式。默认是align_contents(表示页边距对齐),还有一个参数是align_edges(表示边框对齐)。xmlns:ohos="http://schemas.huawei.com/res/ohos"ohos:id="$+id:tableLayout"ohos:height="match_parent"ohos:width="match_parent"ohos:alignment_type="align_contents"ohos:background_element="#8AA7AA">ohos:row_count表示设置网格布局的行数,ohos:column_count表示设置网格布局的列数。如果没有为子组件设置值,则使用父组件的默认行数和列数。如果子组件的个数超过网格布局中设置的列数,则自动添加行数。比如下面的代码,我们设置了一行两列,但是三个子组件,我们监听其中一个按钮的点击事件,在文本组件中显示行数和列数。xmlns:ohos="http://schemas.huawei.com/res/ohos"ohos:id="$+id:tableLayout"ohos:height="match_content"ohos:width="match_content"ohos:row_count="1"ohos:column_count="2"ohos:background_element="#8AA7AA">ohos:id="$+id:tableTxt"ohos:height="match_content"ohos:width="match_content"ohos:text="我是文字组件"ohos:text_size="20fp"/>ohos:id="$+id:button"ohos:height="match_content"ohos:width="match_content"ohos:text="我是第一个按钮"ohos:background_element="#5C6E71"ohos:text_color="#FFFFFF"ohos:text_size="20fp"/>ohos:id="$+id:btn"ohos:height="match_content"ohos:width="match_content"ohos:文本="我是第二个按钮"ohos:background_element="#5C6E71"ohos:text_color="#FFFFFF"ohos:text_size="20fp"/>按钮点击事件未触发时的页面显示效果。触发按钮点击事件时的页面显示效果。ohos:orientation意思是设置表格中组件的排列方式,水平(垂直)和垂直(水平)。如果我们设置了行为1,列2,三个子组件,并且设置了水平显示,那么我们的列就会失效,自动增加一列。xmlns:ohos="http://schemas.huawei.com/res/ohos"ohos:id="$+id:tableLayout"ohos:height="match_parent"ohos:width="match_parent"ohos:orientation="水平"ohos:background_element="#8AA7AA">xmlns:ohos="http://schemas.huawei.com/res/ohos"ohos:id="$+id:tableLayout"ohos:height="match_parent"ohos:width="match_parent"ohos:orientation="vertical"ohos:background_element="#8AA7AA">示例xmlns:ohos="http://schemas.huawei.com/res/ohos"ohos:id="$+id:tableLayout"ohos:height="match_parent"ohos:width="match_parent"ohos:row_count="3"ohos:column_count="5"ohos:background_element="#8AA7AA">ohos:height="match_content"ohos:width="match_content"ohos:text="我是第1个按键"ohos:background_element="#07CCFF"ohos:text_color="#FFFFFF"ohos:text_size="20fp"ohos:padding="10vp"/>...更多内容请访问:与华为官方共建的鸿蒙技术社区https://harmonyos.51cto.com/#zz