//首先在res/values/下创建attrs
<resources>
//设置名称 <declare-styleable name="Custom">
//设置属性 <attr name="setText1" format="string"/> <attr name="setText2" format="string"/> <attr name="setText1Size" format="integer"/> <attr name="setText2Size" format="integer"/> <attr name="setText1Color" format="color"/> <attr name="setText2Color" format="color"/> <attr name="setBackGround" format="color"/> </declare-styleable> </resources>
//给两个常用的控件放入一个布局中其中一个是自定义的控件画了一个圆
//画圆先继承View
/** * Created by Administrator on 2017/11/13. */ public class Myview_01 extends View{ //设置画笔 private Paint paint; public Myview_01(Context context) { super(context); } public Myview_01(Context context, @Nullable AttributeSet attrs) { super(context, attrs); paint=new Paint(); } //设置圆的背景颜色可改变 public int getColor(int color) { paint.setColor(color); return color; } //画 @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); //画圆 canvas.drawCircle(getWidth()/2,getHeight()/2,getWidth()/2,paint); } //测量方法 @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); } }//将圆放入自定义控件中
//xml文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_weight="9" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <FrameLayout android:layout_width="120dp" android:layout_height="match_parent"> <com.example.administrator.weekend_02_exam.Myview_01 android:id="@+id/myview" android:layout_width="120dp" android:layout_height="match_parent"/> <TextView android:id="@+id/tv1" android:text="A" android:textSize="40dp" android:gravity="center" android:textColor="#FFFFFF" android:layout_width="match_parent" android:layout_height="match_parent"/> </FrameLayout> <TextView android:id="@+id/tv2" android:textSize="45dp" android:text="AAAAAAAA" android:gravity="center" android:layout_width="match_parent" android:layout_height="match_parent"/> </LinearLayout> <TextView android:layout_weight="0.5" android:background="#1571FA" android:layout_width="match_parent" android:layout_height="wrap_content"/> </LinearLayout>//向各个属性中赋值用来创建自定义view重点
//xml中设置布局控件和设置的新属性/** * Created by Administrator on 2017/11/13. */ public class Myview_02 extends LinearLayout{ private TextView tv1,tv2; private Myview_01 myview; private Paint paint; private Canvas canvas; public Myview_02(Context context) { super(context); } public Myview_02(Context context, @Nullable AttributeSet attrs) { super(context, attrs); //放入控件 LayoutInflater.from(context).inflate(R.layout.item,this); //实例化 tv1= (TextView) findViewById(R.id.tv1); tv2= (TextView) findViewById(R.id.tv2); myview= (Myview_01) findViewById(R.id.myview); //设置属性 TypedArray array=context.obtainStyledAttributes(attrs,R.styleable.Custom); int text1size = array.getInteger(R.styleable.Custom_setText1Size, 40); int text2size = array.getInteger(R.styleable.Custom_setText2Size, 40); int Text1Color = array.getColor(R.styleable.Custom_setText1Color, Color.RED); int Text2Color = array.getColor(R.styleable.Custom_setText2Color, Color.BLACK); String text1 = array.getString(R.styleable.Custom_setText1); String text2 = array.getString(R.styleable.Custom_setText2); int setBack = array.getColor(R.styleable.Custom_setBackGround, Color.RED); //对应属性 tv1.setText(text1); tv2.setText(text2); tv1.setTextSize(text1size); tv2.setTextSize(text2size); tv1.setTextColor(Text1Color); tv2.setTextColor(Text2Color); //设置圆的背景颜色 myview.getColor(setBack); array.recycle(); } }
<LinearLayout android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:cs="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.administrator.weekend_02_exam.MainActivity"> <android.support.v7.app.AlertController.RecycleListView android:layout_width="wrap_content" android:layout_height="wrap_content"></android.support.v7.app.AlertController.RecycleListView> <com.example.administrator.weekend_02_exam.Myview_02 android:layout_width="match_parent" android:layout_height="140dp" cs:setText1="A" cs:setText2="csssaaaa" cs:setText1Size="40" cs:setText2Size="35" cs:setBackGround="#1BBE96" ></com.example.administrator.weekend_02_exam.Myview_02>
</LinearLayout>