Android 分割线实现方法

利用View来实现

这是最简单直接的方法了,也就是说分割线也本身也变成了一个控件,代码如下

<!--分割线-->
<View
     android:layout_width="match_parent"
     android:layout_height="1dp"
     android:layout_marginLeft="10dp"
     android:layout_marginRight="10dp"
     android:background="#666666"/>

只需要根据实际需求调整左右上下间距、颜色、宽高就可以了。

进一步的思考

上述代码讲述了利用View来实现分割线效果。但是,如果现在一个页面有10个分割线,每个分割线都是一样的,ctrl c + ctrl v虽然可以很快地完成代码,但却不利于修改,就像下面这样。

<MyView0/>
<View
     android:layout_width="match_parent"
     android:layout_height="1dp"
     android:layout_marginLeft="10dp"
     android:layout_marginRight="10dp"
     android:background="#666666"/>

<MyView1/>
<View
     android:layout_width="match_parent"
     android:layout_height="1dp"
     android:layout_marginLeft="10dp"
     android:layout_marginRight="10dp"
     android:background="#666666"/>

<MyView1/>
<View
     android:layout_width="match_parent"
     android:layout_height="1dp"
     android:layout_marginLeft="10dp"
     android:layout_marginRight="10dp"
     android:background="#666666"/>

<MyView2/>
<View
     android:layout_width="match_parent"
     android:layout_height="1dp"
     android:layout_marginLeft="10dp"
     android:layout_marginRight="10dp"
     android:background="#666666"/>
...

可以看到分割线产生了大量重复代码,这是十分不利于更改的。
其实可以将分割线封装到style里面去。首先先在styles.xml文件里添加一个关于分割线的style,代码如下

<style name="style_divide_line">
     <item name="android:layout_height">1dp</item>
     <item name="android:layout_width">match_parent</item>
     <item name="android:background">@color/line_color</item> 
     <item name="android:layout_marginLeft">10dp"</item>
     <item name="android:layout_marginRight">10dp"</item>
</style>

为了方便更改分割线的统一颜色,将background的颜色值放在了colors.xml中,代码如下

<color name="line_color">#666666</color>

完成了上述代码编写,View只需调用style即可,代码如下

<!--封装后的分割线-->
<View style="@style/line"/>

分割线的代码就简洁了很多,需要修改分割线参数时只要改刚才的style就行了,减少了很多的工作量。

再改改styles.xml

<style name="style_divide_line_1">
     <item name="android:layout_height">1dp</item>
     <item name="android:layout_width">match_parent</item>
     <item name="android:background">@color/line_color</item> 
     <item name="android:layout_marginLeft">10dp"</item>
     <item name="android:layout_marginRight">10dp"</item>
</style>

<style name="style_divide_line_2">
     <item name="android:layout_height">2dp</item>
     <item name="android:layout_width">match_parent</item>
     <item name="android:background">@color/line_color</item> 
     <item name="android:layout_marginLeft">10dp"</item>
     <item name="android:layout_marginRight">10dp"</item>
</style>

<style name="style_divide_line_3">
     <item name="android:layout_height">3dp</item>
     <item name="android:layout_width">match_parent</item>
     <item name="android:background">@color/line_color</item> 
     <item name="android:layout_marginLeft">10dp"</item>
     <item name="android:layout_marginRight">10dp"</item>
</style>

这回把分割线的风格分成了三种,细线、普通线和粗线。需要哪个调用哪个就行了。

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android实现虚线分割线方法有多种。一种方法是使用代码动态设置虚线效果。可以通过设置View的LayerType为SOFTWARE来实现。具体代码如下所示: 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及以上版本中,就可以顺利画出虚线分割线。\[1\] 然而,需要注意的是,这种方法Android 3.0以下的版本可能无法画出虚线,而是画出实线。\[2\] 另一种方法是使用XML文件定义虚线的样式。可以创建一个shape文件,设置其shape为line,并通过stroke标签设置虚线的样式。具体代码如下所示: <?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> 然后,在布局文件中使用该shape作为分割线的背景即可。\[3\] 希望以上信息对您有所帮助。 #### 引用[.reference_title] - *1* *2* *3* [Android分割线divider(内含Android虚线分割线失效成实线解决方案)](https://blog.csdn.net/zhangphil/article/details/49046505)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值