android 自定义TextView

TextView 这个控件本身的一些常用属性咱就不具体介绍了,此处仅仅介绍如何自定义一个TextView,最近接触的项目中自定义的东西还挺多的,感觉自定义这个东东还是必须要会的,废话少说,步入正题,先从简单开始吧!!

直接上代码:

1.首先在values/attrs.xml 中定义如下内容:

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <declare-styleable name="MyTextView">
        <attr name="textColor" format="color" />
        <attr name="textSize" format="dimension" />
        <attr name="title" format="string" />
    </declare-styleable>

</resources>


2.自定义一个MyTextView继承TextView

 

package com.test.customviewtest;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.widget.TextView;

public class MyTextView extends TextView {
	private String hello = "this is custom_defined";

	public MyTextView(Context context, AttributeSet attrs) {
		super(context, attrs);

		/**
		 * 以下三行若注释掉,那么 activity_main.xml 中的
		 * 
		 * mycustomapp:textColor="#00ff00"
		 * 
		 * mycustomapp:textSize="30px"
		 * 
		 * mycustomapp:title="xml zhong de text"
		 * 
		 * 将不会生效,所以要想xml中的属性值生效,就必须添加如下代码,首先获取TypedArray
		 */

		TypedArray a = context.obtainStyledAttributes(attrs,
				R.styleable.MyTextView);
		int textColor = a.getColor(R.styleable.MyTextView_textColor, 0xff969696);
		float textSize = a.getDimension(R.styleable.MyTextView_textSize, 40);
		hello = a.getString(R.styleable.MyTextView_title);

		setText(hello);
		setTextSize(textSize );
		setTextColor(textColor );
		a.recycle();
	}
}


3. layout文件activity_main.xml :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:mycustomapp="http://schemas.android.com/apk/res/com.test.customviewtest"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <!-- 属性:textColor、textSize、title   是attr.xml中定义的 -->

    <com.test.customviewtest.MyTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        mycustomapp:textColor="#00ff00"
        mycustomapp:textSize="30px"
        mycustomapp:title="xml zhong de text" />

</RelativeLayout>



注意这一行代码: xmlns:mycustomapp="http://schemas.android.com/apk/res/com.test.customviewtest"  为命名空间

mycustomapp :自己随意取的名称

http://schemas.android.com/apk/res/  :这一行是固定不变的

com.test.customviewtest    :应用程序的包名

 

经过以上几步,一个简单的自定义就完成了!! 

补充几点:

[1].TypedArray实例是个属性的容器,由 context.obtainStyledAttributes()方法返回得到。AttributeSet是节点的属性集合,在本例中是<com.test.customviewtest.MyTextView 节点中的属性集合!

[2]. 自定义属性的format值,有多种:

  • reference
  • string
  • color
  • dimension
  • boolean
  • integer
  • float
  • fraction
  • enum
  • flag

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Android自定义TextView中显示全部内容,可以使用以下两种方法: 1. 使用setEllipsize()方法 通过设置setEllipsize()方法,可以在TextView的末尾添加省略号,从而指示文本被截断。你可以使用以下代码来实现: ``` yourTextView.setEllipsize(TextUtils.TruncateAt.END); yourTextView.setSingleLine(true); ``` 上述代码将设置TextView只显示一行并在末尾添加省略号。 2. 自定义TextView 你可以从TextView类继承一个新类,并覆盖onMeasure()方法以测量控件的高度和宽度。 你可以使用以下代码实现: ``` public class CustomTextView extends TextView { public CustomTextView(Context context) { super(context); } public CustomTextView(Context context, AttributeSet attrs) { super(context, attrs); } public CustomTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); //获取TextView的内容 CharSequence text = getText(); if (text != null) { //测量TextView的高度 int width = getMeasuredWidth(); int height = getMeasuredHeight(); int lineCount = getLineCount(); int lineHeight = getLineHeight(); int totalHeight = lineCount * lineHeight; if (totalHeight > height) { setMeasuredDimension(width, totalHeight); } } } } ``` 上述代码将测量TextView的高度,如果文本的高度超出了TextView的高度,则调整TextView的高度以适应文本。然后你可以使用此自定义TextView来显示你的文本。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值