您的位置:首页 > 移动开发 > Android开发

Android分割线divider(内含Android虚线分割线失效成实线解决方案)

2015-10-11 13:45 447 查看
Android分割线divider(内含Android虚线分割线失效成实线解决方案)

1,最简单最常见的Android分割线android:attr/listDivider。

最简单最常见的是把Android原生的Android ListView的分割线divider直接拿来作为背景衬托做成分割线divider的效果。比如代码:

<View
android:layout_width="match_parent"
android:layout_height="10dip"
android:background="?android:attr/listDivider" />

将这个View放置在不同view之间,比如一个垂直方向的线性布局子view之间。

2,直接将一个图片ImageView作为Android分割线divider。

和1中的类似,只是把View该写成ImageView,效果相同,注意高度和背景颜色,比如可以这样写代码:

<ImageView
android:layout_width="match_parent"
android:layout_height="10dip"
android:background="@android:color/black" />更复杂的写法可以在此ImageView的background中再次定制和改造。

3,在一个线性布局LinearLayoutCompat中直接设置分割线。

最新版本的LinearLayoutCompat支持在LinearLayoutCompat中配置分割线要素,我之前写过一篇文章介绍过,《Android Material Design :LinearLayoutCompat添加分割线divider》文章链接地址:http://blog.csdn.net/zhangphil/article/details/48899585

4,重点说一说Android虚线分割线divider。

通常为了做一个Android的虚线分割线divider,通用的方法和代码是:

(第一步)先在drawable目录下创建一个线line的shape文件,比如此dash_line.xml文件代码:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line" >

<!--线宽为dashWith,线之间空隙dashGap,dashGap=0dp时,是实线 -->
<stroke
android:dashGap="15dip"
android:dashWidth="30dip"
android:width="1dip"
android:color="@android:color/black" />

<!-- 虚线高度 -->
<size android:height="1dip" />

</shape>
(第二步)然后在自己的布局文件中添加一个LinearLayout作为不同view的分割线,比如:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:orientation="vertical" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="csdn zhangphil" />

<LinearLayout
android:id="@+id/dashLine"
android:layout_width="match_parent"
android:layout_height="2dip"
android:background="@drawable/dash_line"
android:orientation="horizontal" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="csdn zhangphil" />

</LinearLayout>
这是目前网上一搜一大堆互相抄来抄去、实现Android虚线分割线divider的方法和代码(奇怪的是很多人在互相抄来抄去的时候也不检验有效性和时效性),这是很早以前也许有效的方法,遗憾的是,上述的方法和代码也许在Android 3.0以下是可以画出虚线的,但在Android 3.0以上最新Android SDK设备上,代码在真机上跑起来,根本画不出虚线,画出的而是一条实线!

究其根本原因,是因为在Android 3.0以上,Android系统在众多绘图操作时候默认开启了硬件加速,因此导致在最新的高版本Android系统上画dash gap line失效,这一问题在Android官方问题报告页面(https://code.google.com/p/android/issues/detail?id=29944)有问题报告反馈以及给出的各种五花八门的解决方案。总结起来,相对比较有效、且简单的解决方案主要有三个:

解决方案A:setLayerType(View.LAYER_TYPE_SOFTWARE, null)

保持如本案例中第一步和第二步的代码原封不动,只需要在Java代码中做一次判断:

LinearLayout dashLine=(LinearLayout) findViewById(R.id.dashLine);
if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
dashLine.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}当Android在3.0及以上版本时候setLayerType(View.LAYER_TYPE_SOFTWARE, null);即可顺利画出虚线分割线。

解决方案B:android:layerType="software"

其实就是解决方案A的Java代码转移到xml中做配置,保持本案例中第一步代码原封不动,仅仅在第二步的代码中增加设置一个属性android:layerType="software" 改进成这样:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:orientation="vertical" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="csdn zhangphil" />

<LinearLayout
android:id="@+id/dashLine"
android:layout_width="match_parent"
android:layout_height="2dip"
android:background="@drawable/dash_line"
android:layerType="software"
android:orientation="horizontal" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="csdn zhangphil" />

</LinearLayout>
解决方案C:修改AndroidManifest.xml中的application属性,设置:android:hardwareAccelerated="false"。

此解决方案其实就是告知Android系统关闭硬件加速,这样也可以顺利画出虚线分割线。但是这样关闭硬件加速的影响是全局的,将导致整体代码运行性能急剧降低,如果仅仅为了画一个虚线而关闭整个APP的硬件加速,实在是得不偿失,丢了大西瓜捡了个小芝麻,因此除非万不得已,我个人不建议采用解决方案C,我个人建议采取解决方案B。

5,ListView在代码运行时设置分割线divider。

通常会在一个布局中先写好、配置好ListView的样式如分割线,我以前写过文章专门介绍过:《Android基础小技术点:Android ListView设置背景图片及分割线、周边距 》文章链接:http://blog.csdn.net/zhangphil/article/details/48948217 。

不过,ListView的分割线也可以在Java代码运行时动态设置,设置代码,比如:

mListView.setDivider(getResources().getDrawable(android.R.drawable.arrow_down_float));


(备注:我写作这篇文章的时间是2015年10月11日。不排除也许在未来的Android版本迭代中,Android官方SDK调整画虚线分割线的代码,使得画虚线分割线更自然。)


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: