Android Studio GridLayout & ScrollView
GridLayout是网格布局 感觉在设计表格的时候会有一些用处
知识点只有两个
columnCount属性,指定了网格的列数,即每行放多少个
rowCount属性,指定行数,即每行放多少个
要注意的是一开始采用网格布局后,textview中的文字或许不居中,可以用gravity属性进行修改
如果比例有一些问题可以用weight属性进行比重的修改
copy
<?xml version="1.0" encoding="utf-8"?> <GridLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:columnCount="2" android:rowCount="2" > <TextView android:layout_width="0dp" android:layout_height="60dp" android:layout_columnWeight="1" android:background="#ffcccc" android:gravity="center" android:text="浅红色" android:textColor="@color/black" android:textSize="17sp" /> <TextView android:layout_width="0dp" android:layout_height="60dp" android:layout_columnWeight="1" android:background="#ffaa00" android:text="橙色" android:gravity="center" android:textColor="@color/black" android:textSize="17sp"/> <TextView android:layout_width="0dp" android:layout_height="60dp" android:layout_columnWeight="1" android:background="#00ff00" android:text="绿色" android:gravity="center" android:textColor="@color/black" android:textSize="17sp"/> <TextView android:layout_width="0dp" android:layout_height="60dp" android:layout_columnWeight="1" android:background="#660066" android:text="深紫色" android:gravity="center" android:textColor="@color/black" android:textSize="17sp"/> </GridLayout>
ScrollView是滚动视图
主要分为两类
1.ScrollView 垂直方向滚动视图
注意 使用时要把layout_width属性设置为match_parent
layout_height属性设置为wrap_content
2.HorizontalScrollView 水平方向滚动视图
注意 使用时要把layout_width属性设置为wrap_content
layout_height属性设置为match_content
copy
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".ScrollViewActivity" android:orientation="vertical"> <HorizontalScrollView android:layout_width="wrap_content" android:layout_height="200dp"> <!-- 水平方向的线性布局,两个子视图的颜色分别为青色和黄色 --> <LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:orientation="horizontal"> <View android:layout_width="300dp" android:layout_height="match_parent" android:background="#aaffff"/> <View android:layout_width="300dp" android:layout_height="match_parent" android:background="#ffff00"/> </LinearLayout> </HorizontalScrollView> <ScrollView android:layout_width="match_parent" android:layout_height="wrap_content"> <!-- 垂直方向的线性布局,两个子视图的颜色分别为绿色和橙色 --> <LinearLayout android:layout_width="wrap_content" android:layout_height="match_parent" android:orientation="vertical"> <View android:layout_width="match_parent" android:layout_height="400dp" android:background="#00ff00"/> <View android:layout_width="match_parent" android:layout_height="400dp" android:background="#ffffaa"/> </LinearLayout> </ScrollView> </LinearLayout>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构