/***********************************************************&&&&&&&&&&&&&&&&&&&&&&&&&&&&/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context="com.example.customviewdemo.MainActivity">
<com.example.customviewdemo.veiw.MyTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
app:text="这是我的字符串"
app:textColor="#000000"
app:textSize="16dp">
</com.example.customviewdemo.veiw.MyTextView>
</RelativeLayout>
/************************************************&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&/attrs.xml
<?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>
/**************************************************************************/styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>
/******************************************************************&&&&&&&&&&&&&&&&&&&&&&&&&&&&/配置
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.customviewdemo">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
//&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&/MyTextView
package com.example.customviewdemo.veiw;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import com.example.customviewdemo.R;
/**
* Created by hasee on 2017/8/30.
*/
public class MyTextView extends View {
private String text;
private int textColor;
private float textSize;
public MyTextView(Context context) {
super(context);
initAttrs(null);
}
public MyTextView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
initAttrs(attrs);
}
private void initAttrs(@Nullable AttributeSet attrs) {
TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.MyTextView);
text = typedArray.getString(R.styleable.MyTextView_text);
textColor = typedArray.getColor(R.styleable.MyTextView_textColor, 0xffffff);
textSize = typedArray.getDimension(R.styleable.MyTextView_textSize, 16);
}
//画 canvas画布
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(textColor);
paint.setTextSize(textSize);
canvas.drawText(text, 10, 100, paint);
}
}
/&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&/MainActivity
package com.example.customviewdemo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}