Android布局文件.xml中的自定义属性
16lz
2021-01-26
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>
运行结果:
更多相关文章
- MonkeyRunner Recorder & PlayBack
- 【Android】SAX解析XML(20110926更新)
- SharedPreferences存与取,
- android Log4j学习笔记
- android资源文件中ids.xml的使用
- android文件命名错误Invalid file name: must contain only [a-z
- [Android] Linux下查看apk文件程序包名的办法
- Android(安卓)Uri获取真实路径以及文件名的方法
- 写在20120524:aidl