官方文檔
創(chuàng)新互聯(lián)公司-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比江津網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式江津網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋江津地區(qū)。費(fèi)用合理售后完善,10多年實(shí)體公司更值得信賴。
/Myselfcomponent/res/values/attrs.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="MyView" > <attr name="textColor" format="color"/> <attr name="textSize" format="dimension" /> <attr name="text" format="string"></attr> </declare-styleable> </resources>
activity_main.xml
<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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.myselfcomponent.MainActivity" xmlns:my="http://schemas.android.com/apk/res/com.example.myselfcomponent"> <com.example.myselfcomponent.MyView android:id="@+id/MyView1" android:layout_width="match_parent" android:layout_height="wrap_content" my:textColor="#00ffff" my:textSize="20sp" my:text="自定義組件" /> </RelativeLayout> <!-- my:textColor="#00ffff" 的前綴跟命名空間有關(guān)系 (tools:context="com.example.myselfcomponent.MainActivity" xmlns:my="http://schemas.android.com/apk/res/com.example.myselfcomponent">), 可以自定義 -->
MyView
package com.example.myselfcomponent; import android.R.integer; import android.R.style; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Paint.Style; import android.graphics.Rect; import android.util.AttributeSet; import android.view.View; /** * 自定義組件 * */ public class MyView extends View{ //畫筆 Paint paint; String text; public MyView(Context context) { super(context); paint=new Paint(); paint.setColor(Color.BLACK); paint.setTextSize(28); // TODO Auto-generated constructor stub } public MyView(Context context,AttributeSet attrs) { // TODO Auto-generated constructor stub super(context, attrs); paint=new Paint(); TypedArray typedArray=context.obtainStyledAttributes(attrs,R.styleable.MyView); int color=typedArray.getColor(R.styleable.MyView_textColor, 0xFFFFFF); float size=typedArray.getDimension(R.styleable.MyView_textSize, 20); text=typedArray.getString(R.styleable.MyView_text); paint.setColor(color); paint.setTextSize(size); //關(guān)閉資源 typedArray.recycle(); } /** * 初始化組件時(shí)被觸發(fā),進(jìn)行組件的渲染 * Canvas 畫布 * */ @Override protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub super.onDraw(canvas); //設(shè)置畫筆風(fēng)格為實(shí)心 paint.setStyle(Style.FILL); //繪制正方形 canvas.drawRect(new Rect(10,10,90,90), paint); paint.setColor(Color.BLUE); canvas.drawText(text, 10, 110, paint); } }
MainActivity
package com.example.myselfcomponent; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
新聞名稱:android自定義組件
分享路徑:http://chinadenli.net/article36/ppijpg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃、網(wǎng)頁設(shè)計(jì)公司、網(wǎng)站排名、網(wǎng)站改版、域名注冊(cè)、虛擬主機(jī)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)