带分割线的TextView和LinearLayout
来源:互联网 发布:关联查询sql 编辑:程序博客网 时间:2024/04/30 13:14
写ui的时候,总是有很多类似菜单列表类的界面要写,例如微信的界面,每一项都有一个分割线来分割,每一个大项又区分开来。
一般的做法都是用一个view标签,设置其背景颜色,高度,等等。这么做固然可以,虽说会消耗资源,但最恶心的还是代码的维护方面,若是界面不变还好,若界面总是要增加减少某个项,而分割线和这项又不在一块,总是需要把分割线和这个项隐藏,还可能要设置 layout_marginTop,简直是无穷无尽的噩梦。
其实线性布局自带分割线的属性,用法也很简单,但是是针对子布局的分割线,很不适用~
现在自定义组件在ondraw方法里面绘制分割线,并且可以自己定义颜色,高度,显示上面还是下面
用法如下:
<com.example.xingyun.androidtestdemo.DividerTextView android:layout_width="match_parent" android:layout_height="40dp" android:layout_marginTop="10dp" android:gravity="center" android:background="#eee" android:text="你好" app:lineColor="#f00" app:lineWidth="2dp" app:showStyle="top" /><com.example.xingyun.androidtestdemo.DividerLinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" app:lineColor="#000" app:showStyle="all" > <TextView android:layout_width="match_parent" android:layout_height="40dp" android:gravity="center" android:text="你好" /></com.example.xingyun.androidtestdemo.DividerLinearLayout>
效果如下
show code
public class DividerLinearLayout extends LinearLayout { public static final short TOP = 1; public static final short BOTTOM= 2; public static final short ALL = 3; private Paint paint; /** * 默认线宽为1px */ private float lineWidth = 1; /** * 默认线的颜色为浅灰 #D0D0D0 */ private int lineColor = 0xffD0D0D0; /** * 默认上下都显示 */ private int showStyle = ALL; public DividerLinearLayout(Context context, AttributeSet attrs) { super(context, attrs); setWillNotDraw(false); paint = new Paint(); TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.DividerLinearLayout); lineColor = a.getColor(R.styleable.DividerLinearLayout_lineColor, 0xffD0D0D0); lineWidth = a.getDimensionPixelSize(R.styleable.DividerLinearLayout_lineWidth, 1); showStyle = a.getInt(R.styleable.DividerLinearLayout_showStyle, 3); a.recycle(); paint.setColor(lineColor); paint.setStrokeWidth(lineWidth); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); if (showStyle==TOP){ canvas.drawLine(0, lineWidth/2, getWidth(), lineWidth/2, paint); }else if (showStyle==BOTTOM){ canvas.drawLine(0, getHeight() - lineWidth/2, getWidth(), getHeight() - lineWidth/2, paint); }else if (showStyle==ALL){ canvas.drawLine(0, lineWidth/2, getWidth(), lineWidth/2, paint); canvas.drawLine(0, getHeight() - lineWidth/2, getWidth(), getHeight() - lineWidth/2, paint); } }}
public class DividerTextView extends TextView { public static final short TOP = 1; public static final short BOTTOM= 2; public static final short ALL = 3; private Paint paint; /** * 默认线宽为1px */ private float lineWidth = 1; /** * 默认线的颜色为浅灰 #D0D0D0 */ private int lineColor = 0xffD0D0D0; /** * 默认上下都显示 */ private int showStyle = ALL; public DividerTextView(Context context, AttributeSet attrs) { super(context, attrs); paint = new Paint(); TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.DividerLinearLayout); lineColor = a.getColor(R.styleable.DividerLinearLayout_lineColor, 0xffD0D0D0); lineWidth = a.getDimensionPixelSize(R.styleable.DividerLinearLayout_lineWidth, 1); showStyle = a.getInt(R.styleable.DividerLinearLayout_showStyle, 3); a.recycle(); paint.setColor(lineColor); paint.setStrokeWidth(lineWidth); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); if (showStyle==TOP){ canvas.drawLine(0, lineWidth/2, getWidth(), lineWidth/2, paint); }else if (showStyle==BOTTOM){ canvas.drawLine(0, getHeight() - lineWidth/2, getWidth(), getHeight() - lineWidth/2, paint); }else if (showStyle==ALL){ canvas.drawLine(0, lineWidth/2, getWidth(), lineWidth/2, paint); canvas.drawLine(0, getHeight() - lineWidth/2, getWidth(), getHeight() - lineWidth/2, paint); } }}
<declare-styleable name="DividerLinearLayout"> <attr name="lineWidth" format="dimension"/> <attr name="lineColor" format="reference|color"/> <attr name="showStyle" format="enum"> <enum name="top" value="1"/> <enum name="bottom" value="2"/> <enum name="all" value="3"/> </attr></declare-styleable>
0 0
- 带分割线的TextView和LinearLayout
- listview分割线的使用和linearlayout使用
- Android开发ListView 和Linearlayout的分割线
- LinearLayout均分的分割线配置方式
- LinearLayout显示分割线
- linearLayout设置分割线
- LinearLayout分(hun)割线
- LinearLayout 分割线
- TextView添加分割线
- Android Linearlayout 添加分割线
- LinearLayout增加divider分割线
- LinearLayout显示分割线(Divider)
- LinearLayout增加divider分割线
- android LinearLayout分割线设置
- LinearLayout增加divider分割线
- Android怎么实现Linearlayout的垂直分割线?
- UITableView缩短自带的分割线
- Android自定义带分割线的GridView
- 自定义URL Protocol Handler 呼出应用程序
- 在 github 建立blg
- linux回删密码,密码以*显示
- tcpdump dump 网络流量
- poj 1002 487-3279(水)
- 带分割线的TextView和LinearLayout
- LateX在windows中运用MiKTeX
- String的不可变性
- DBCP连接池配置参数说明
- 第14周练习项目二 学生成绩处理函数版
- layer,jquery,laydate前端组件
- 观光旅游
- 傻子能看懂的并查集入门讲解
- Redis源码解析——字典基本操作