首先android textview的粗体效果对于汉字不知道为什么的没用用处 设置了也没什么变化,然则这只对英文有效,当你的TextView要显示中文的时辰要在code中设置粗体的paint来实现,如下
TextViewtitle=newTextView(context);TextPaintpaint=title.getPaint();
paint.setFakeBoldText(true);
paint.setFakeBoldText(true);
还有从xml中得到,但是这样麻烦在我项目中有很多次,我必须设置一个ID然后去找到,然后再去用代码设置它的大小,所以我想了一个一劳用逸的方法,那就是自定view,但我也不知道自定view怎么弄,所以有如下:
在贴出代码之前首先就view的重点说一下:
比如这里 xmlns:my="
http://schemas.android.com/apk/res/demo.dedo",
4.特别注意,因为我在我编写的时候,老是出现,找不到资源什么什么的,我看了一下,“自定义View所在的包”必须和AndroidManifest中的 package="demo.dedo"相同。
1.attrs.xml
- <?@H_301_41@xml@H_301_41@@H_301_41@version@H_301_41@=@H_301_41@"1.0"@H_301_41@@H_301_41@encoding@H_301_41@=@H_301_41@"utf-8"@H_301_41@?>@H_301_41@@H_301_41@@H_301_41@
- <@H_301_41@resources@H_301_41@>@H_301_41@@H_301_41@@H_301_41@
- declare-styleable@H_301_41@@H_301_41@name@H_301_41@=@H_301_41@"TestView"@H_301_41@>@H_301_41@@H_301_41@@H_301_41@
- attr@H_301_41@@H_301_41@name@H_301_41@=@H_301_41@"textblod"@H_301_41@@H_301_41@format@H_301_41@=@H_301_41@"integer"@H_301_41@@H_301_41@/>@H_301_41@@H_301_41@@H_301_41@
- </@H_301_41@declare-styleable@H_301_41@>@H_301_41@@H_301_41@@H_301_41@
2.TestView
- package@H_301_41@demo.dedo;@H_301_41@@H_301_41@
- import@H_301_41@android.content.Context;@H_301_41@@H_301_41@
- import@H_301_41@android.content.res.TypedArray;@H_301_41@@H_301_41@
- import@H_301_41@android.text.TextPaint;@H_301_41@@H_301_41@
- import@H_301_41@android.util.AttributeSet;@H_301_41@@H_301_41@
- import@H_301_41@android.widget.TextView;@H_301_41@@H_301_41@
- public@H_301_41@@H_301_41@class@H_301_41@TestView@H_301_41@extends@H_301_41@TextView@H_301_41@@H_301_41@
- {@H_301_41@
- private@H_301_41@TextPaintpaint;@H_301_41@@H_301_41@
- private@H_301_41@ContextmContext;@H_301_41@@H_301_41@
- public@H_301_41@TestView(Contextcontext,AttributeSetattrs)@H_301_41@@H_301_41@
- super@H_301_41@(context,attrs);@H_301_41@@H_301_41@
- //TODOAuto-generatedconstructorstub@H_301_41@@H_301_41@@H_301_41@
- mContext=context;@H_301_41@
- //对于我们自定义的类中,我们需要使用一个名为obtainStyledAttributes的方法来获取我们的定义@H_301_41@@H_301_41@@H_301_41@
- TypedArrayparams=mContext.obtainStyledAttributes(attrs,@H_301_41@
- R.styleable.TestView);@H_301_41@
- //得到自定义控件的属性值。@H_301_41@@H_301_41@@H_301_41@
- int@H_301_41@backgroundId=params.getInteger(R.styleable.TestView_textblod,@H_301_41@0@H_301_41@);@H_301_41@@H_301_41@
- setTextblod(backgroundId);@H_301_41@
- }@H_301_41@
- void@H_301_41@setTextblod(@H_301_41@int@H_301_41@textblod)@H_301_41@@H_301_41@
- if@H_301_41@(textblod==@H_301_41@1@H_301_41@)@H_301_41@@H_301_41@
- paint=super@H_301_41@.getPaint();@H_301_41@@H_301_41@
- paint.setFakeBoldText(true@H_301_41@);@H_301_41@@H_301_41@
- }@H_301_41@
- }@H_301_41@
3.在xml中引用view
copy
- <?xmlversion=@H_301_41@"1.0"@H_301_41@encoding=@H_301_41@"utf-8"@H_301_41@?>@H_301_41@@H_301_41@
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"@H_301_41@@H_301_41@@H_301_41@
- xmlns:my="http://schemas.android.com/apk/res/demo.dedo"@H_301_41@@H_301_41@@H_301_41@
- android:orientation="vertical"@H_301_41@@H_301_41@@H_301_41@
- android:layout_width="fill_parent"@H_301_41@@H_301_41@@H_301_41@
- android:layout_height="fill_parent"@H_301_41@@H_301_41@@H_301_41@
- >@H_301_41@
- @H_301_41@
- <deme.deme.TestView@H_301_41@
- android:layout_width="fill_parent"@H_301_41@@H_301_41@@H_301_41@
- android:layout_height="wrap_content"@H_301_41@@H_301_41@@H_301_41@
- my:textblod="1"@H_301_41@@H_301_41@@H_301_41@
- android:text="这只是一个测试"@H_301_41@@H_301_41@@H_301_41@
- />@H_301_41@
- @H_301_41@
- </LinearLayout>@H_301_41@