自訂view和自訂屬性(獻給和我一樣的小白)

來源:互聯網
上載者:User

一、自訂View
     體現在布局檔案中如下:
   

  <?xml version="1.0" encoding="utf-8"?><A>   <B></B></A>

     其中 A extends LinerLayout
          B extends View
(忽略布局)

做法:

1、建立一個類MyView 整合 View類。

public class MyBrick extends View

2、重載父類中的三個構造方法和onDraw方法。

public MyBrick(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);}public MyBrick(Context context, AttributeSet attrs) {super(context, attrs);}public MyBrick(Context context) {super(context);}

3、使用Paint設定view中的東西(例如我們的view 需要顯示一個紅色的圓角矩形)

@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);Paint paint = new Paint();paint.setColor(Color.RED);      //設定顏色paint.setStyle(Style.FILL);     //布局填充canvas.drawRoundRect(new RectF(50, 0, 100, 100), 15f, 20f, paint);   //畫出圓角矩形}

4、在布局檔案裡面使用如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <com.example.brickbreaker.childview.MyBrickandroid:layout_width="wrap_content"android:layout_height="wrap_content" >    </com.example.brickbreaker.childview.MyBrick></RelativeLayout>

二、自訂屬性

1、在res/values下建立attrs.xml
內容如下:

<?xml version="1.0" encoding="utf-8"?><resources>    <declare-styleable name="MyBrick"><attr name="joeColor" format="color"></attr><attr name="joeTextSize" format="dimension"></attr>    </declare-styleable></resources>


   其中,declare-styleable 中的name 屬性值是我們自訂的view的名稱。
         attr標籤中的name是我們自訂屬性的名字,format表示自訂屬性的類型或者格式。
 例如:joeColor是color屬性

   format的一些其他常見值如下:
   
reference   表示引用 ,例如R.id.xx。
string   表示字串
color   表示顏色值
dimension   表示尺寸值
boolean   表示布爾值
integer   表示整型值
float   表示浮點值
fraction   表示百分數
enum   表示枚舉值
flag   表示位元運算

        2、在自訂view類中使用自訂屬性。
在自訂view的構造方法  類名(Context context ,AttributeSet attrs)中設定屬性的
路徑和預設值

public MyBrick(Context context, AttributeSet attrs) {super(context, attrs);Paint paint = new Paint();TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.MyBrick);int color = a.getColor(R.styleable.MyBrick_joeColor, Color.GREEN);float dimension = a.getDimension(R.styleable.MyBrick_joeTextSize, 22);paint.setColor(color);paint.setTextSize(dimension);a.recycle();}

R.styleable.MyBrick_joeColor 由三部分組成:

R.styleable.MyBrick是attrs.xml中的declare-styleable中的MyBrick

joeColor是自訂的屬性,  “_”底線負責將他們串連到一起。這樣,我們在布局檔案使用到自訂舒心joeColor時,
虛擬機器就可以知道我們是要為自訂VIew添加屬性了。

int color = a.getColor(R.styleable.MyBrick_joeColor, Color.GREEN);float dimension = a.getDimension(R.styleable.MyBrick_joeTextSize, 22);paint.setColor(color);paint.setTextSize(dimension);

上面這四句話是設定預設屬性。
3、布局檔案中使用自訂屬性

必須在最外層布局添加
xmlns:joe="http://schemas.android.com/apk/res/com.example.brickbreaker"
joe是自訂的名稱,稱作“命名空間”,以上面的一行設定代碼為例,在布局檔案中,如果要給某個自訂view使用自訂屬性,要把命名空間
由原來的android換成joe,如下

    <com.example.brickbreaker.childview.MyBrickandroid:layout_width="wrap_content"android:layout_height="wrap_content"joe:joeColor="#A26FA7" >    </com.example.brickbreaker.childview.MyBrick>

com.example.brickbreaker是自訂view所在應用的包名,這個必須的,不能隨便寫。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.