android 添加属性,Android添加自己的属性

AndroidManifest.xml

package="com.example.mybutton"

android:versionCode="1"

android:versionName="1.0" >

android:minSdkVersion="14"

android:targetSdkVersion="17" />

android:allowBackup="true"

android:icon="@drawable/ic_launcher"

android:label="@string/app_name"

android:theme="@style/AppTheme" >

android:name="com.example.mybutton.MyButton"

android:label="@string/app_name" >

src/com/example/mybutton/MyButton.java

package com.example.mybutton;

import android.os.Bundle;

import android.app.Activity;

import android.view.Menu;

public class MyButton extends Activity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_my_button);

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.my_button, menu);

return true;

}

}

res/layout/activity_my_button.xml

xmlns:tools="http://schemas.android.com/tools"

xmlns:my="http://schemas.android.com/apk/res/com.example.mybutton"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:paddingBottom="@dimen/activity_vertical_margin"

android:paddingLeft="@dimen/activity_horizontal_margin"

android:paddingRight="@dimen/activity_horizontal_margin"

android:paddingTop="@dimen/activity_vertical_margin"

tools:context=".MyButton" >

android:id="@+id/textView1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/hello_world" />

android:id="@+id/button1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignLeft="@+id/textView1"

android:layout_below="@+id/textView1"

my:myTextSize="30sp"

my:myTextColor="#ff6666"

android:textColor="@android:color/black"

android:text="Button" />

解释:xmlns:

my="http://schemas.android.com/apk/res/

com.example.mybutton"

my:属性前缀

com.example.mybutton:包名

src/com/example/mybutton/WindButton.java

package com.example.mybutton;

import android.content.Context;

import android.content.res.TypedArray;

import android.util.AttributeSet;

import android.util.Log;

import android.widget.Button;

public class WindButton extends Button{

private static final String TAG = "WindButton";

public WindButton(Context context, AttributeSet attrs) {

super(context, attrs);

Log.v(TAG, "WindButton 2 pares");//声明在xml的类会调用这个构造函数

TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.myView);//TypedArray是一个数组容器

float textSize = a.getDimension(R.styleable.myView_myTextSize, 30);//防止在XML文件里没有定义,就加上了默认值30

int textColor = a.getColor(R.styleable.myView_myTextColor, 0xFFFFFFFF);//同上,这里的属性是:名字_属性名

setTextSize(textSize);

setTextColor(textColor);

setBackgroundResource(R.drawable.ic_launcher);

a.recycle();//TypedArray类型不会自动回收,为避免影响下次使用,好的习惯是主动回收

}

public WindButton(Context context, AttributeSet attrs, int defStyle) {

super(context, attrs, defStyle);

Log.v(TAG, "WindButton 3 pares");

// TODO Auto-generated constructor stub

}

public WindButton(Context context) {

super(context);

Log.v(TAG, "WindButton 1 pares");

// TODO Auto-generated constructor stub

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值