大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
官方文档
创新互联公司-专业网站定制、快速模板网站建设、高性价比江津网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式江津网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖江津地区。费用合理售后完善,10多年实体公司更值得信赖。
/Myselfcomponent/res/values/attrs.xml
activity_main.xml
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); //关闭资源 typedArray.recycle(); } /** * 初始化组件时被触发,进行组件的渲染 * Canvas 画布 * */ @Override protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub super.onDraw(canvas); //设置画笔风格为实心 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); } }