自定义控件(五)画一条线

画一条线既可以在java代码里实现,也可以在drawable中实现。
总体来说在java代码里划线是比较麻烦的,如果涉及到在屏幕中的位置时。
如果能在xml中把view的事搞定,就不要放在java代码里。这才符合mvc分离原则。

方法一:

在xml中划线分两步
1.在drawable中创建textview_single_line.xml文件。代码如下:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="line" >
    <stroke 
        android:width="1dp"
        android:color="#007aff"/>
    <size android:height="2dp"/>
</shape>

2.在textview属性中引用。

<TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="28dp"
        android:layout_marginRight="54dp"
        android:background="@drawable/tv_single_line"
        />

shape标签中子元素具体用法参见。
http://blog.csdn.net/bear_huangzhen/article/details/24488337

——————————————————————————————————————————–
2015.8.12 Edit
笔者才发现,原来还有一种方法在xml中划线,这种方法最简便。

方法二:

只需添加< View >标签,并设置其属性。

<!--竖线-->
<View  
    android:layout_width="1dip" 
    android:layout_height="match_parent"
    android:background="#66CCFF"
    android:layout_gravity="center_horizontal"
    />

<!--横线-->
<View  
    android:layout_height="1px" 
    android:layout_width="match_parent"
    android:background="#66CCFF"
    />
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值