AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.cctvjiatao.xmlset"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="14"        android:targetSdkVersion="21" />    <application        android:allowBackup="true"        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name=".MainActivity"            android:label="@string/app_name" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application></manifest>

com.cctvjiatao.xmlset.MainActivity

package com.cctvjiatao.xmlset;import android.app.Activity;import android.os.Bundle;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}}

activity_main.xml

<pre name="code" class="html"><!-- xmlns:jiatao="http://schemas.android.com/apk/res/com.cctvjiatao.xmlset" xmlns:[自定义命名] = "http://schemas.android.com/apk/res/[本xml所属activity的包名]"--><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    xmlns:juan="http://schemas.android.com/apk/res/com.cctvjiatao.xmlset"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.cctvjiatao.xmlset.MainActivity" >    <!-- 一定要写正确自定义View的“包名+类名”,不然汇报错:Binary XML file line #14: Error inflating class.. -->    <com.cctvjiatao.xmlset.TestView        android:layout_width="match_parent"        android:layout_height="match_parent"        juan:Scroll="true"        juan:Lines="10" >    </com.cctvjiatao.xmlset.TestView></RelativeLayout>
  

com.cctvjiatao.xmlset.TestView

<pre name="code" class="java">package com.cctvjiatao.xmlset;import android.content.Context;import android.content.res.TypedArray;import android.graphics.Canvas;import android.graphics.Paint;import android.util.AttributeSet;import android.view.View;/** * @作者: jiatao * @修改时间:2016-3-12 下午10:20:43  * @包名:com.cctvjiatao.xmlset * @文件名:CustomView.java * @版权声明:www.cctvjiatao.com * @功能: 普通的自定义View */public class TestView extends View {private int LinesNum = 0;private boolean isScroll = false;private Paint paint = new Paint();private ViewThread thread;private float xStart = 0;public TestView(Context context) {super(context);}public TestView(Context context, AttributeSet attrs) {super(context, attrs);TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.JiataoCustom);LinesNum = ta.getInt(R.styleable.JiataoCustom_Lines, 1);isScroll = ta.getBoolean(R.styleable.JiataoCustom_Scroll, false);ta.recycle();}@Overrideprotected void onDraw(Canvas canvas) {for(int i=0; i<LinesNum; i++){int textSize = 30+i;paint.setTextSize(textSize);canvas.drawText("cctvjiatao", xStart, textSize*(1+i), paint);}if(thread == null){thread = new ViewThread();thread.start();}}class ViewThread extends Thread{@Overridepublic void run() {    while(isScroll){    xStart += 3;    if(xStart > getWidth()){    xStart = 0 - paint.measureText("cctvjiatao");    }    postInvalidate();    try {                Thread.sleep(30);                } catch (InterruptedException e) {                e.printStackTrace();                }    }}}}
 res/values/attrsjiatao.xml(新建的xml)  

<pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?><resources>    <declare-styleable name="JiataoCustom">        <attr name="Lines" format="integer"/>        <attr name="Scroll" format="boolean" />    </declare-styleable></resources>
  
运行结果:



更多相关文章

  1. MonkeyRunner Recorder & PlayBack
  2. 【Android】SAX解析XML(20110926更新)
  3. SharedPreferences存与取,
  4. android Log4j学习笔记
  5. android资源文件中ids.xml的使用
  6. android文件命名错误Invalid file name: must contain only [a-z
  7. [Android] Linux下查看apk文件程序包名的办法
  8. Android(安卓)Uri获取真实路径以及文件名的方法
  9. 写在20120524:aidl

随机推荐

  1. android一个转盘效果的容器viewgroup
  2. Android的安全性和权限
  3. Android中的sp和wp类模板以及RefBase类
  4. Android修炼之检测非SDK接口
  5. Android(安卓)ROM中Odex文件的作用及介绍
  6. Android事件分发机制-自己理解以后的阐述
  7. Android(安卓)L中水波纹点击效果的实现
  8. android 开发效率
  9. recovery 根据@/cache/recovery/block.ma
  10. 是广告还是病毒