一、从产品经理的“小需求”说起
那天,产品经理端着奶茶笑眯眯地走过来:“哥们儿,咱这个底部分类工具栏,能不能做成五个图标平均分布,点击还要有动画效果?很简单对吧?”
我内心OS:“简单?你知不知道Android布局能玩出多少花样?!”
但表面上依然保持微笑:“没问题,让我研究下哪种布局最适合。”
在Android的世界里,布局管理器就像是装修房子的户型图。选对了布局,后续UI开发事半功倍;选错了,呵呵,等着代码变成一锅粥吧!
今天咱们就重点聊聊表格布局(TableLayout) 和线性布局(LinearLayout) 这对好基友,看它们如何联手打造出既美观又实用的分类工具栏。
二、线性布局:直来直去的“钢铁直男”
2.1 线性布局是个啥?
线性布局,人送外号“直线思考者”,就像排队买奶茶一样,所有视图要么竖着排(vertical),要么横着排(horizontal),绝不拐弯。
<LinearLayout
android:layout_width="match_parent"
android:layout_height="56dp"
android:orientation="horizontal"
android:background="#FFFFFF"
android:padding="8dp">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="首页" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="订单" />
<!-- 更多按钮... -->
</LinearLayout>
看到没?layout_weight属性是线性布局的杀手锏,通过权重分配空间,实现等分效果,完美解决分类工具栏的平均分布需求。
2.2 线性布局实现分类工具栏实战
假设我们要做一个外卖APP的底部工具栏,包含首页、订单、钱包、消息、我的五个入口:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="56dp"
android:orientation="horizontal"
android:background="#FFFFFF"
android:elevation="8dp">
<!-- 首页 -->
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical"
android:gravity="center">
<ImageView
android:layout_width="24dp"
android:layout_height="24dp"
android:src="@drawable/ic_home" />

最低0.47元/天 解锁文章

被折叠的 条评论
为什么被折叠?



