自定义控件 –百科专家总结(最后遗留了问题望网友能帮忙解答)

在开发android应用中 经常用到自定义控件 我 就将这段时间 我参与的项目中用到的自定义控件说一下

在百科专家中用到最多的就是 text描边 这是我参考的文章

http://labs.ywlx.net/?p=2739

http://labs.ywlx.net/?p=1260

我以前 也写了篇文章 讲述的时在surfaceview 中对字体的描边  不过后来仔细研究了下 不怎么合适 http://labs.ywlx.net/?p=2749

在百科专家中我 总结了以上文章自己写了个自定义的控件方便在项目中自由的调用 ,废话不多说  先看代码 在慢慢的讲解

布局文件 textviewtest. xml

<RelativeLayout

android:id=”@+id/item_2″

android:layout_width=”36dp”

android:layout_height=”36dp”

android:layout_marginTop=”10dp”

android:layout_toLeftOf=”@id/textviewfen_c” >

 

<TextView

android:id=”@+id/item_2_1_c”

android:layout_width=”36dp”

android:layout_height=”wrap_content”

android:layout_alignParentRight=”true”

android:layout_marginTop=”3dp”

android:gravity=”center”

android:textColor=”@color/white”

android:textSize=”18sp” />

 

<TextView

android:id=”@+id/item_2_1″

android:layout_width=”36dp”

android:layout_height=”wrap_content”

android:layout_alignParentRight=”true”

android:layout_marginTop=”3dp”

android:gravity=”center”

android:textColor=”@color/course_score_color”

android:textSize=”18sp” />

 

 

</RelativeLayout>

//再我们应用的地方  就是这么调用的  直接包名加上类名

并且在 文件头部加上这样一个标签 myapp 就是我们自己定义的命名空间

<RelativeLayout xmlns:android=“http://schemas.android.com/apk/res/android”

xmlns:myapp=“http://schemas.android.com/apk/res/cn.easymobi.entertainment.baike”

<cn.easymobi.entertainment.baike.customView.TextViewTest

android:id=“@+id/school_0″

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

myapp:strokeColor=“@color/school_0_miaobian”

myapp:strokeWidth=“3dp”

myapp:textColor=“@color/white”

myapp:textSize=“12sp” />

 

java代码

自定义一个类  继承 RelativeLayout

public class TextViewTest extends RelativeLayout {

}

重写 其中的构造函数

public TextViewTest(Context context) {

super(context);

LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

//获取自己自定义的布局文件

inflater.inflate(R.layout.textviewtest, this);

//获取相应的控件

textView0 = (TextView) findViewById(R.id.textview0);

textView1 = (TextView) findViewById(R.id.textview1);

}

//我们自定义控件 一件就是为了自己用起来更方便一些  因此我们就重写下面的这个方法  AttributeSet attr 相当与 android系统控件中的属性一样 这个代表着你自己定义的控件的属性

public TextViewTest(Context context, AttributeSet attrs) {

super(context, attrs);

//获取自己自定义的布局文件

LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

inflater.inflate(R.layout.textviewtest, this);

//获取相应的控件

textView0 = (TextView) findViewById(R.id.textview0);

textView1 = (TextView) findViewById(R.id.textview1);

//得到根据你的属性配置文件 获得相应的参数

TypedArray params = context.obtainStyledAttributes(attrs, R.styleable.TextViewTest);

//得到我们在调用自定义控件的地方设置的值

//获得字体颜色 并设置

int textColor = params.getColor(R.styleable.TextViewTest_textColor, R.color.white);

textView1.setTextColor(textColor);

//获得描边颜色 并设置

int strokeColor = params.getColor(R.styleable.TextViewTest_strokeColor, R.color.black);

textView0.setTextColor(strokeColor);

//设置描边宽度

float strokeWidth = params.getDimension(R.styleable.TextViewTest_strokeWidth, 3 * CommonUtilities.fDensity);

textView0.getPaint().setStyle(Style.STROKE);//空心

textView0.getPaint().setStrokeWidth(strokeWidth);

}

最后务必在values 文件夹中添arrays.xml

<declare-styleable name=“TextViewTest”>

<!– 描边颜色 –>

<attr name=“strokeColor” format=“color” />

<!– 字体颜色 –>

<attr name=“textColor” format=“color” />

<!– 描边宽度 –>

<attr name=“strokeWidth” format=“dimension” />

<!– 字体内容 –>

<attr name=“text” format=“string” />

</declare-styleable>

//这样就可以用了

如果想动态的添加view 即addview();可以在定义的textviewtest中添加get   set方法

代码基本上贴完了 不完美的地方还请多提提意见

最后总结下  前两天  想自定义textView 写了一半  没有研究透

下面将遇到的问题贴出来望广大网友 知道的接小弟讲解下  将不胜感激

问题 :在重写了onDraw方法  在drawText的时候 经研究是从所要花的字的左下角开始画向上画的,为了达到通用性在改变字体大小时也想得到完美的效果  由于最近项目比较急   一时没有想不到 怎么确定字的高度,因此暂时放弃了此方法  望网友能帮小弟解答

转载于:https://my.oschina.net/liuwj/blog/85998

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值