一种是自定义属性的View,另外一种是引用定义好的布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.ff.customview.MainActivity">
<com.ff.customview.MyTextView
android:id="@+id/mytextview"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
app:Text="这是使用attrs定义的属性"
app:TextColor="#f00"
app:TextSize="30sp"/>
<com.ff.customview.MyCustomView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</com.ff.customview.MyCustomView>
</LinearLayout>
自定义TextView继承View
public class MyTextView extends View {
private int TextColor;
private String Text;
private float TextSize;
public MyTextView(Context context) {
super(context);
initView(null);
}
public MyTextView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
initView(attrs);
}
private void initView(@Nullable AttributeSet attrs) {
//得到咱们自定义的attrs中的数据
TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.MyTextView);
Text = typedArray.getString(R.styleable.MyTextView_Text);
TextSize = typedArray.getDimension(R.styleable.MyTextView_TextSize, 15);
TextColor = typedArray.getColor(R.styleable.MyTextView_TextColor, Color.YELLOW);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//设置画笔的颜色,和大小
Paint paint = new Paint();
paint.setColor(TextColor);
paint.setTextSize(TextSize);
paint.setAntiAlias(true);
/**
* 画出来一个view
* 由于画的是一个textview,第一个参数是就是text内容
* 第一个参数距离 x坐标
* 第二个参数是 距离 y坐标
* 第三个参数是画笔
*/
canvas.drawText(Text, 20, 50, paint);
}
}
引用布局自定义view,继承LinearLayout
public class MyCustomView extends LinearLayout {
public MyCustomView(Context context) {
super(context);
}
public MyCustomView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
initView(context);
}
private void initView(final Context context) {
inflate(context, R.layout.layout, this);
ImageView img = (ImageView) findViewById(R.id.layout_image);
TextView text = (TextView) img.findViewById(R.id.item_textview);
img.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(context, "我被点击了", Toast.LENGTH_SHORT).show();
}
});
}
}
//在values文件夹下新建立一个attrs文件,里面存放你要自定义的属性
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MyTextView">
<attr name="Text" format="string"></attr>
<attr name="TextSize" format="dimension"></attr>
<attr name="TextColor" format="color"></attr>
</declare-styleable>
</resources>
//下面是第二种方法中使用的布局,这里只是简单的布局,根据自己需求来定义
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageView
android:id="@+id/layout_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher_round"/>
<TextView
android:id="@+id/item_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="这是item"/>
</LinearLayout>