Android无需java代码,仅在xml中实现横向多个控件间隔分布
2016-08-06 17:13
405 查看
实现思路可以理解为插空法。
先看看效果图:
实现思路主要分两步:
step 1 :先将左右两个控件位置确定,我是使用一个framelayout将左右空间分布在 最左和最右,当然如果你希望最左最右也有和中间一样的间隔,也可以实现,等理解了插空法思想,其实都一样
step 2 :将中间剩余空间用一个LinearLayout进行填充,生成三个正方形view,在每个正方形左右,各填充一个weight为1的space空间,即可实现:
实现思路很简单,一开始采用代码控制间距,但这么做难免会对布局进行重绘,降低了应用性能,直接在xml布局文件中进行一次绘制,可以减少这样的弊端!
先看看效果图:
实现思路主要分两步:
step 1 :先将左右两个控件位置确定,我是使用一个framelayout将左右空间分布在 最左和最右,当然如果你希望最左最右也有和中间一样的间隔,也可以实现,等理解了插空法思想,其实都一样
<FrameLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginTop="5dp" android:layout_marginLeft="10dp" android:layout_marginRight="10dp"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> <View android:layout_width="60dp" android:layout_height="60dp" android:background="#9e9e9e"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:layout_gravity="center" android:text="1" android:textSize="10dp" android:layout_marginLeft="1dp"/> </LinearLayout> …… <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right" android:orientation="vertical"> <View android:layout_width="60dp" android:layout_height="60dp" android:layout_gravity="right" android:background="#9e9e9e"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:layout_gravity="center" android:text="4" android:textSize="10dp" android:layout_marginRight="1dp"/> </LinearLayout> </FrameLayout>
step 2 :将中间剩余空间用一个LinearLayout进行填充,生成三个正方形view,在每个正方形左右,各填充一个weight为1的space空间,即可实现:
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="right" android:layout_marginRight="60dp" android:layout_marginLeft="60dp" android:orientation="horizontal"> <Space android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" tools:targetApi="ice_cream_sandwich" /> <LinearLayout android:layout_width="60dp" android:layout_height="wrap_content" android:orientation="vertical"> <View android:layout_width="60dp" android:layout_height="60dp" android:background="#9e9e9e" android:layout_gravity="center"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:text="2" android:textSize="10dp" android:layout_gravity="center"/> </LinearLayout> <Space android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" tools:targetApi="ice_cream_sandwich" /> <LinearLayout android:layout_width="60dp" android:layout_height="wrap_content" android:orientation="vertical"> <View android:layout_width="60dp" android:layout_height="60dp" android:background="#9e9e9e" android:layout_gravity="center"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:text="3" android:textSize="10dp" android:layout_gravity="center"/> </LinearLayout> <Space android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" tools:targetApi="ice_cream_sandwich" /> </LinearLayout>
实现思路很简单,一开始采用代码控制间距,但这么做难免会对布局进行重绘,降低了应用性能,直接在xml布局文件中进行一次绘制,可以减少这样的弊端!
相关文章推荐
- android开发游记:通过xml配置快速实现点击热点效果和渐变动画(不用java代码)
- Android图片加载神器之Fresco-更灵活的设置加载特效[Java代码实现XML效果]
- Android图片加载神器之Fresco-更灵活的设置加载特效[Java代码实现XML效果]
- 【转载】Android在Java代码中实现margin(实现组件间间隔)
- Android(java)学习笔记200:Android中View动画之 XML实现 和 代码实现
- Android图片加载神器之Fresco-更灵活的设置加载特效[Java代码实现XML效果]
- 收集Android里xml配置属性在java代码里的实现
- android 横屏竖屏设置的详解(最好不要用Java代码实现,用xml配置文件吧)
- Android中级篇之Fresco-更灵活的设置加载特效[Java代码实现XML效果]
- Android图片加载神器之Fresco-更灵活的设置加载特效[Java代码实现XML效果]
- Android中级篇之Fresco-更灵活的设置加载特效[Java代码实现XML效果]
- android 系统重启与关机:java 代码实现
- 如何在android的XML和java代码中引用字符串常量
- java代码实现属性android:layout_marginTop的功能
- Android导航菜单横向左右滑动并和下方的控件实现联动
- Android中用Java代码实现zip文件解压缩(二)
- android动态布局:用java代码实现LinearLayout以及TextView
- Android解析XML(PULL方式)和JSON 工作原理和实现过程(详细代码)
- Android APK反编译方法(可以获取APK xml和android Manifest,java代码等内容)
- 200行Java代码实现Android下的视频通话