• TextView加边框,自定义,上下左右四条线 颜色,想用哪个用哪个


    1.这是一个自定义的TextView ,看吧,底下就是代码,应该都可以看懂,这里就不多说了

    package com.example.admin.myutilsborder;
    import android.content.Context;
    import android.content.res.TypedArray;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Paint;
    import android.util.AttributeSet;
    import android.widget.TextView;


    /**
    * Created by admin on 2017/3/7.
    */

    public class BorderTextView extends TextView {
    private boolean top_border ;
    private boolean bottom_border;
    private boolean left_border ;
    private boolean right_border ;

    public BorderTextView(Context context) {
    super(context);
    }
    public BorderTextView(Context context, AttributeSet attrs) {
    super(context, attrs);
    TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.BorderTextView);
    top_border =ta.getBoolean(R.styleable.BorderTextView_top_border,false);
    bottom_border= ta.getBoolean(R.styleable.BorderTextView_bottom_border,false);
    left_border= ta.getBoolean(R.styleable.BorderTextView_left_border,false);
    right_border= ta.getBoolean(R.styleable.BorderTextView_right_border,false);
    }
    private int sroke_width = 1;

    @Override
    protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    Paint paint = new Paint();
    // 将边框设为黑色
    //paint.setColor(android.graphics.Color.RED);
    paint.setColor(Color.GREEN);
    // paint.setColor(Color.BLUE);
    //paint.setColor(android.graphics.Color.alpha(717171));

    // TextView4个边
    //drawLine方法参数顺序 左 上左
    if(left_border){
    canvas.drawLine(0, 0, 0, this.getHeight() - sroke_width, paint);
    }
    if(top_border){
    canvas.drawLine(0, 0, this.getWidth() - sroke_width, 0, paint);
    }
    if(right_border){
    canvas.drawLine(this.getWidth() - sroke_width, 0, this.getWidth() - sroke_width, this.getHeight() - sroke_width, paint);
    }
    if(bottom_border) {
    canvas.drawLine(0, this.getHeight() - sroke_width, this.getWidth() - sroke_width, this.getHeight() - sroke_width, paint);
    }

    //
    //canvas.drawLine(0, 0, this.getWidth() - sroke_width, 0, paint);
    //
    //canvas.drawLine(0, 0, 0, this.getHeight() - sroke_width, paint);
    //
    // canvas.drawLine(this.getWidth() - sroke_width, 0, this.getWidth() - sroke_width, this.getHeight() - sroke_width, paint);
    //
    //canvas.drawLine(0, this.getHeight() - sroke_width, this.getWidth() - sroke_width, this.getHeight() - sroke_width, paint);
    //super.onDraw(canvas);
    }

    }

    2.报错了,是不是,别急

    在valus目录下新建一个attrs文件夹,注意:我这里使用Studio开发

    <resources>


    <declare-styleable name="BorderTextView">
    <attr name="left_border" format = "string" />
    <attr name="right_border" format = "string" />
    <attr name="top_border" format = "string" />
    <attr name="bottom_border" format = "string" />
    <attr name="mTextColor" format="color" />
    <attr name="mTextSize" format="dimension" />
    </declare-styleable>


    </resources>

    3.剩下的就是使用了,在XMl文件中

    <com.example.admin.myutilsborder.BorderTextView    
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:top_border="true"
    app:bottom_border="true"
    app:left_border="true"
    app:right_border="true"
    android:text="Hello World!" />
    <!--上面这里就是自定义的边框,需要上下左右哪个方向的线条就把它设置成true,不需要,直接删掉就好-->
    4.最后一步,在Xml文件中
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    注意,多加了下面这条语句
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    完了,就这样简单的搞定了,简单的自定义,我这里就不过多的解释了,在碰见几个TextView放在一起,如果都设置边框的话,会让一部分线条变粗,当然,背景图也完全可以解决,但是搞技术嘛,简单的写写
     
  • 相关阅读:
    NGUI 3.5课程(五岁以下儿童)button-图片切换
    跑openstack命令错误【You must provide a username via either -...】
    angular cors跨域资源共享设置 和formdata设定
    PHP 如何获取客户端ip地址
    JavaScript如何生成思维导图(mindmap)
    百度ueditor上传图片时如何设置默认宽高度
    英语发音规则---E字母常见的发音组合有哪些
    google搜索引擎爬虫爬网站原理
    legend2---开发日志10(ajax请求的方法是否同样会执行base控制器里面的方法)
    JS中如何判断对象是对象还是数组
  • 原文地址:https://www.cnblogs.com/Aprilsky/p/6532548.html
Copyright © 2020-2023  润新知