您的位置:首页 > 移动开发 > Android开发

Android布局文件.xml中的自定义属性

2016-03-12 22:59 567 查看
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 {

@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>



运行结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: