巧用Drawable 实现Android UI 元素间距效果
来源:互联网 发布:vs2017如何编写c语言 编辑:程序博客网 时间:2024/06/06 01:13
设置间距的最佳方案——LinearLayout 的divider
实际上 LinearLayout
已经有一个处理这种元素之间的间距的属性了。这个属性却没怎么被大家发现,一直很低调,但它的效果相当神奇。所以我们说的第三个方案就是使用一个固定高宽的 Drawable
作为 LinearLayout
的 元素分隔线(divider):
1
2
3
4
5
6
7
8
9
10
11
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
shape
xmlns:android
=
"http://schemas.android.com/apk/res/android"
android:shape
=
"rectangle"
>
<
size
android:width
=
"@dimen/spacing_medium"
android:height
=
"@dimen/spacing_medium"
/>
<
solid
android:color
=
"<a href="
http://www.jobbole.com/members/android/"
rel
=
"nofollow"
>@android</
a
>:color/transparent" />
</
shape
>
现在你就可以把这个新创建的 Drawable 设为LinearLayout 的 divider,这样这个Drawable 就能让元素之间产生间距了:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<
LinearLayout
xmlns:android
=
"http://schemas.android.com/apk/res/android"
android:layout_width
=
"match_parent"
android:layout_height
=
"wrap_content"
android:divider
=
"@drawable/spacer_medium"
android:orientation
=
"vertical"
android:padding
=
"@dimen/spacing_medium"
android:showDividers
=
"middle"
>
<!-- TextView -->
<
LinearLayout
android:id
=
"@+id/buttons_container"
android:layout_width
=
"match_parent"
android:layout_height
=
"wrap_content"
android:divider
=
"@drawable/spacer_medium"
android:orientation
=
"horizontal"
android:showDividers
=
"middle"
>
<!-- Buttons -->
</
LinearLayout
>
</
LinearLayout
>
总结
Android 框架里面有许多的特性可以用来实现一些不常见的方案,而且最后效果出其不意。定义 Drawable
就是其中一种途径。如果你能吃透Android 里面的 Drawable
,那么你的代码也可能大大地精简。
注意:文章LinearLayout的divider 属性设置是Android API 11 之后加进去的,这意味着Android API 11之前的设备要使用这个divider需要LinearLayoutCompat。
0 0
- 巧用Drawable 实现Android UI 元素间距效果
- 巧用Drawable 实现Android UI 元素间距效果
- 巧用Drawable 实现Android UI 元素间距效果
- 巧用Drawable 实现Android UI 元素间距效果
- 巧用Drawable 实现Android UI 元素间距效果
- 用Drawable来实现UI元素的间隔
- android drawable实践 xml 实现各种效果
- [Android UI界面] drawable
- Android UI, canvas, drawable
- android通过drawable资源实现常用的自定义效果
- Android UI效果实现 滑动模糊渐变效果实现
- 求这个Android UI效果的实现....
- 求这个Android UI效果的实现....
- Android UI 利用Drawable Shape给控件加边框/立体效果
- 代码实现drawable selector效果
- Android UI效果实现——Activity滑动退出效果
- android字间距实现,textview字间距实现
- Android UI Libs之android-gif-drawable
- android学习网站
- iOS概念之KVO(Key-Value Observing)
- PostgreSQL pg_dump&psql 数据的备份与恢复
- 【转】C# TabConTrol控件背景颜色问题
- maven打包时出现编码GBK的不可映射字符
- 巧用Drawable 实现Android UI 元素间距效果
- 使用 Optim 在 Bluemix 应用程序中建立数据隐私保护
- js中的逻辑与(&&)操作
- JAVA 字节顺序转换
- Hadoop 2.0中Capacity Scheduler与Fair Scheduler对比
- 循环-08. 二分法求多项式单根(20)
- ppt怎么转换成pdf方法介绍
- linux date 显示指定时区的时间 借助TZ 环境变量 export TZ=Asia/Shanghai 或 America/New_York
- 要养成习惯懒加载数据