Android布局文件.xml中的自定义属性
2016-03-12 22:59
567 查看
AndroidManifest.xml
com.cctvjiatao.xmlset.MainActivity
activity_main.xml
com.cctvjiatao.xmlset.TestView
res/values/attrsjiatao.xml(新建的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 { @Override protected 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(); } @Override protected 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{ @Override public 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>
运行结果:
相关文章推荐
- android studio Cannot reload AVD list
- android 圆角图片
- 将android手机屏幕投影到电脑端
- android_________测绘流程
- Android -- XML Drawable
- Android自定义控件之流式布局
- android开发教程之开机启动服务service示例
- Android中实现开机自动启动服务(service)实例
- android屏幕适配总结
- Android MVP 介绍
- Android-使用绝对布局管理器固定组件的位置
- 六款值得推荐的android(安卓)开源框架简介
- Android布局文件.xml中的自定义属性(结合封装的自定义View)
- android学习之路(一) 环境搭建
- Android应用开发之BaseAdapter基类的写法
- 2015Android 开发新技术
- 关于android任务Task及回收栈back stack介绍
- android gradle plugin 1.3.0 以上使用 public.xml 固定 id
- Android中的模板方法模式
- Android开发qq一键登录成功返回数据的要方法