0
回答
自定义属性后在代码中操作没有效果
利用AWS快速构建适用于生产的无服务器应用程序,免费试用12个月>>>   

attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="BorderTextView">
        <attr name="layout_borders" format="boolean" />
        <attr name="layout_borderTop" format="boolean" />
        <attr name="layout_borderRight" format="boolean" />
        <attr name="layout_borderBottom" format="boolean" />
        <attr name="layout_borderLeft" format="boolean" />
        <attr name="layout_borderColor" format="color" />
    </declare-styleable>
</resources>



public class BorderTextView extends TextView {

	private Paint paint;
	
	boolean borders = false;
	
	boolean borderLeft = false;
	
	boolean borderTop = false;
	
	boolean borderRight = false;
	
	boolean borderBottom = false;
	
	int borderColor = Color.TRANSPARENT;
	
	public BorderTextView(Context context) {
		this(context, null);
	}
	
	public BorderTextView(Context context, AttributeSet attrs) {
		super(context, attrs);
		
		paint = new Paint();
		
		TypedArray typeArray = context.obtainStyledAttributes(attrs, R.styleable.BorderTextView);
		
		borders = typeArray.getBoolean(R.styleable.BorderTextView_layout_borders, false);
		borderTop = typeArray.getBoolean(R.styleable.BorderTextView_layout_borderTop, false);
		borderRight = typeArray.getBoolean(R.styleable.BorderTextView_layout_borderRight, false);
		borderBottom = typeArray.getBoolean(R.styleable.BorderTextView_layout_borderBottom, false);
		borderLeft = typeArray.getBoolean(R.styleable.BorderTextView_layout_borderLeft, false);
		borderColor = typeArray.getColor(R.styleable.BorderTextView_layout_borderColor, Color.TRANSPARENT);
		paint.setColor(borderColor);
		
		typeArray.recycle();
	}

	@SuppressLint("DrawAllocation")
	@Override
	protected void onDraw(Canvas canvas) {
		
		if (borders) {
			canvas.drawLine(0, 0, this.getWidth() - 0.5f, 0, paint);
			canvas.drawLine(this.getWidth() - 0.5f, 0, this.getWidth() - 0.5f, this.getHeight() - 0.5f, paint);
			canvas.drawLine(0, this.getHeight() - 0.5f, this.getWidth() - 0.5f, this.getHeight() - 0.5f, paint);
			canvas.drawLine(0, 0, 0, this.getHeight() - 0.5f, paint);
		}
		else {
			if (borderTop)
				canvas.drawLine(0, 0, this.getWidth() - 0.5f, 0, paint);
			if (borderRight)
				canvas.drawLine(this.getWidth() - 0.5f, 0, this.getWidth() - 0.5f, this.getHeight() - 0.5f, paint);
			if (borderBottom)
				canvas.drawLine(0, this.getHeight() - 0.5f, this.getWidth() - 0.5f, this.getHeight() - 0.5f, paint);
			if (borderLeft)
				canvas.drawLine(0, 0, 0, this.getHeight() - 0.5f, paint);
		}
		
        super.onDraw(canvas);
	}

	public boolean isBorders() {
		return borders;
	}

	public void setBorders(boolean borders) {
		this.borders = borders;
	}

	public boolean isBorderLeft() {
		return borderLeft;
	}

	public void setBorderLeft(boolean borderLeft) {
		this.borderLeft = borderLeft;
	}

	public boolean isBorderTop() {
		return borderTop;
	}

	public void setBorderTop(boolean borderTop) {
		this.borderTop = borderTop;
	}

	public boolean isBorderRight() {
		return borderRight;
	}

	public void setBorderRight(boolean borderRight) {
		this.borderRight = borderRight;
	}

	public boolean isBorderBottom() {
		return borderBottom;
	}

	public void setBorderBottom(boolean borderBottom) {
		this.borderBottom = borderBottom;
	}

	public int getBorderColor() {
		return borderColor;
	}

	public void setBorderColor(int borderColor) {
		this.borderColor = borderColor;
	}
}



public class MonitorActivity extends Activity {
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_monitor);
		
		initCF();
	}
	
	private void initCF() {
                TableLayout table = (TableLayout)findViewById(R.id.cf_table);
		TableRow row = new TableRow(this);
		BorderTextView textView = new BorderTextView(this);
		textView.setGravity(Gravity.CENTER_HORIZONTAL);
		textView.setBorderRight(true);
		textView.setBorderBottom(true);
		textView.setBorderColor(Color.RED);
		textView.setTextSize(14);
		textView.setTextColor(Color.BLACK);
		textView.setText("1111");
		row.addView(textView, new TableRow.LayoutParams(110, LayoutParams.WRAP_CONTENT));
		
		table.addView(row);
		
	}
}
运行之后没有出现边框,到底是哪里出现问题?
举报
dido
发帖于3年前 0回/76阅
顶部