使用divider和showDivider属性为你的部局设置分隔线
					 分类:Android, Java
阅读 (4,157)
							 Add comments
					
		6月 062017
	博文背景:博主要实现app的左侧导航按钮功能,因为要实现某个导航按钮被选中的效果,所以没有使用ListView,而是用的RadioGoup来实现.好,RadioGroup嵌套多个RadioButton很简单的实现了导航列表的功能.另外一个需求是给导航按钮们增加分隔线,博主首先使用RadioButton的drawableTop,分隔线不在最顶部,而且水平不居中,这个属性肯定是不行。然后比较笨的方法就是插入多个ImageView,设置图片为分隔线,但是……导航按钮多的话,这样做真的很麻烦。
于是博主开始研究RadioGroup的属性,发现有一个divider属性,这在ListView里是分隔线的意思,于是给RadioGroup设置这个属性,运行程序,分隔线不显示,心想RadioGroup应该不支持这个属性,毕竟通常情况下divider是对List形式组件才有效的。再想想要加n个ImageView实在不甘心,度娘了一下,原来要再加个showDivider属性。
showDivider有三个可选项,beginning、middle、end,分别对应最开始的分隔线,各RadioButton中间的分隔线,最结尾的分隔线。我们可以如下设置:
| 1 | android:showDividers="beginning|middle|end" | 
不是true或者false哦 ;-)
下面是一个完整的Activity部局的xml代码,在你的res/drawable中放一个叫timg.png的图片,然后把下面代码粘贴到你的Activity的xml文件中,运行一下就可以看到效果了。
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | <?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"     android:orientation="vertical"     android:divider="@mipmap/timg"     android:showDividers="beginning|middle|end"     tools:context="com.chinatsp.dividertestapp.MainActivity">     <CheckBox         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_weight="1"         android:visibility="gone"         android:text="CheckBox"/>     <RadioButton         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:layout_weight="1"         android:visibility="visible"         android:text="RadioButton"/>     <Button         android:id="@+id/button"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_weight="1"         android:text="Button"/> </LinearLayout> | 
注意事项:
- LinearLayout同样支持此属性,可能类似组件都支持
- 如果最开始或者最结尾的组件的visiblity为gone,则showDivider的beginning和end的选项无效

 
                 微信扫一扫,打赏作者吧~
微信扫一扫,打赏作者吧~