在開發android應用中 常常用到自定義控件 我 就將這段時間 我參與的項目中用到的自定義控件說一下 java
在百科專家中用到最多的就是 text描邊 這是我參考的文章 android
http://labs.ywlx.net/?p=2739 app
http://labs.ywlx.net/?p=1260 函數
我之前 也寫了篇文章 講述的時在surfaceview 中對字體的描邊 不事後來仔細研究了下 不怎麼合適 http://labs.ywlx.net/?p=2749 佈局
在百科專家中我 總結了以上文章本身寫了個自定義的控件方便在項目中自由的調用 ,廢話很少說 先看代碼 在慢慢的講解 字體
佈局文件 textviewtest. xml this
<RelativeLayout .net
android:id=」@+id/item_2″ 3d
android:layout_width=」36dp」 orm
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的時候 經研究是從所要花的字的左下角開始畫向上畫的,爲了達到通用性在改變字體大小時也想獲得完美的效果 因爲最近項目比較急 一時沒有想不到 怎麼肯定字的高度,所以暫時放棄了此方法 望網友能幫小弟解答