Android_自定义控件

Android中可以自定义控件,有时候我们需要为这些自定义的空间加上一些属性,Java代码中可以定义属性变量没有问题,那么XML文件中怎么使用属性呢?那么就需要在XML文件中自定义控件属性。(可以参照D:\android-sdk\platforms\android-16\data\res\values\attrs.xml,只是Android系统的属性定义的文件)

假如我们现在写了一个CommonTitle自定义控件
[java] view plaincopy
public class CommonTitle extends LinearLayout {

    private Color textColor;  

    private float textSize;  

    private String text;  

}

这个CommonTitle有三个属性:textColor,textSize,text

当我在布局文件中使用这个CommonTitle的时候,怎么对这3个属性设值呢?你可能会想起来android:textColor,但是这是我们自己写的控件,所以你设了android:textColor也没有用,那么可不可以自己定义一个属性呢?比如说myAndroid:textColor,答案是完全可以!

一、定义自定义控件属性需要在res/values文件下定义一个attrs.xml文件,代码如下:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="CommonTitle">
        <attr name="text" format="string"></attr>
        <attr name="textColor" format="color"></attr>
        <attr name="textSize" format="dimension"></attr>
    </declare-styleable>
</resources>

这样我们就自定义了三个控件属性
二、在布局xml中如下使用该属性:

<?xml version="1.0" encoding="utf-8"?>  
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    xmlns:myAndroid="http://schemas.android.com/apk/res/com.tianjf"  
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent" >  

    <com.tianjf.view.CommonTitle  
        android:layout_width="fill_parent"  
        android:layout_height="50dip"  
        myAndroid:text="@string/str_weightlist_title"  
        myAndroid:textColor="@android:color/darker_gray"  
        myAndroid:textsize="@dimen/weight_list_title_text_size" />  

</LinearLayout>  

三、在自定义组件中,可以如下获得xml中定义的值:

public CommonTitle(Context context, AttributeSet attrs) {  
    super(context, attrs);  
    mContext = context;  
    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CommonTitle);  
    mText = a.getString(R.styleable.CommonTitle_text);  
    mTextColor = a.getColor(R.styleable.CommonTitle_textColor,    
            0XFFFFFFFF);    
    mTextSize = a.getDimensionPixelSize(R.styleable.CommonTitle_textSize, 0);  
    a.recycle();  
}  

以上三步就完成了自定义属性的创建和使用

首先来看看attrs.xml文件
中的name会在java代码中用到,可以看第三步
属性的formart可以有下面几种
string , integer , dimension , reference , color , enum.
前面几种的声明方式都是一致的,例如:。
只有enum是不同的,用法举例:
[html] view plaincopy



如果该属性可同时传两种不同的属性,则可以用“|”分割开即可。

让我们再来看看布局xml中需要注意的事项。
首先得声明一下:xmlns:myAndroid=http://schemas.android.com/apk/res/com.tianjf
注意,“myAndroid”可以换成其他的任何名字,后面的url地址必须最后一部分必须用上自定义组件的包名。自定义属性了,在属性名前加上“myAndroid”即可。

最后来看看java代码中的注意事项。
在自定义组件的构造函数中,用
TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.CommonTitle);
来获得对属性集的引用,然后就可以用“a”的各种方法来获取相应的属性值了。这里需要注意的是,如果使用的方法和获取值的类型不对的话,则会返回默认值。因此,如果一个属性是带两个及以上不用类型的属性,需要做多次判断,知道读取完毕后才能判断应该赋予何值。当然,在取完值的时候别忘了回收资源哦!

以下示例中代码均在values/attrs.xml 中定义,属性均可随意命名。
1. reference:参考某一资源ID。
示例:

<declare-styleable name = "名称"> 
<attr name = "background" format = "reference" /> 
<attr name = "src" format = "reference" /> 
</declare-styleable> 
  1. color:颜色值。
<declare-styleable name = "名称"> 
<attr name = "textColor" format = "color" /> 
</declare-styleable> 
  1. boolean:布尔值。
<declare-styleable name = "名称"> 
<attr name = "focusable" format = "boolean" /> 
</declare-styleable> 

  1. dimension:尺寸值。
<declare-styleable name = "名称"> 
<attr name = "layout_width" format = "dimension" /> 
</declare-styleable> 
  1. float:浮点值。
<declare-styleable name = "名称"> 
<attr name = "fromAlpha" format = "float" /> 
<attr name = "toAlpha" format = "float" /> 
</declare-styleable> 
  1. integer:整型值。
<declare-styleable name = "名称"> 
<attr name = "frameDuration" format="integer" /> 
<attr name = "framesCount" format="integer" /> 
</declare-styleable> 
  1. string:字符串。
<declare-styleable name = "名称"> 
<attr name = "text" format = "string" /> 
</declare-styleable> 
  1. fraction:百分数。
<declare-styleable name="名称"> 
<attr name = "pivotX" format = "fraction" /> 
<attr name = "pivotY" format = "fraction" /> 
</declare-styleable> 
  1. enum:枚举值。
<declare-styleable name="名称"> 
<attr name="orientation"> 
<enum name="horizontal" value="0" /> 
<enum name="vertical" value="1" /> 
</attr> 
</declare-styleable> 
  1. flag:位或运算。
<declare-styleable name="名称"> 
<attr name="windowSoftInputMode"> 
<flag name = "stateUnspecified" value = "0" /> 
<flag name = "stateUnchanged" value = "1" /> 
<flag name = "stateHidden" value = "2" /> 
<flag name = "stateAlwaysHidden" value = "3" /> 
</attr> 
</declare-styleable> 

11.多类型。

<declare-styleable name = "名称"> 
<attr name = "background" format = "reference|color" /> 
</declare-styleable> 

二、属性的使用以及自定义控件的实现
1、构思控件的组成元素,思考所需自定义的属性。
比如:我要做一个 <带阴影的按钮,按钮正下方有文字说明>(类似9宫格按钮)
新建values/attrs.xml

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
<declare-styleable name="custom_view"> 
<attr name="custom_id" format="integer" /> 
<attr name="src" format="reference" /> 
<attr name="background" format="reference" /> 
<attr name="text" format="string" /> 
<attr name="textColor" format="color" /> 
<attr name="textSize" format="dimension" /> 
</declare-styleable> 
</resources> 

以上,所定义为custom_view,custom_id为按钮id,src为按钮,background为阴影背景,text为按钮说明,textColor为字体颜色,textSize为字体大小。
2、怎么自定义控件呢,怎么使用这些属性呢?话不多说请看代码,CustomView :

package com.nanlus.custom; 
import com.nanlus.custom.R; 
import android.content.Context; 
import android.content.res.TypedArray; 
import android.graphics.Color; 
import android.graphics.drawable.Drawable; 
import android.util.AttributeSet; 
import android.view.Gravity; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.FrameLayout; 
import android.widget.ImageButton; 
import android.widget.ImageView; 
import android.widget.TextView; 
public class CustomView extends FrameLayout implements OnClickListener { 
private CustomListener customListener = null; 
private Drawable mSrc = null, mBackground = null; 
private String mText = ""; 
private int mTextColor = 0; 
private float mTextSize = 20; 
private int mCustomId = 0; 
private ImageView mBackgroundView = null; 
private ImageButton mButtonView = null; 
private TextView mTextView = null; 
private LayoutParams mParams = null; 
public CustomView(Context context) { 
super(context); 
} 
public CustomView(Context context, AttributeSet attrs) { 
super(context, attrs); 
TypedArray a = context.obtainStyledAttributes(attrs, 
R.styleable.custom_view); 
mSrc = a.getDrawable(R.styleable.custom_view_src); 
mBackground = a.getDrawable(R.styleable.custom_view_background); 
mText = a.getString(R.styleable.custom_view_text); 
mTextColor = a.getColor(R.styleable.custom_view_textColor, 
Color.WHITE); 
mTextSize = a.getDimension(R.styleable.custom_view_textSize, 20); 
mCustomId = a.getInt(R.styleable.custom_view_custom_id, 0); 
mTextView = new TextView(context); 
mTextView.setTextSize(mTextSize); 
mTextView.setTextColor(mTextColor); 
mTextView.setText(mText); 
mTextView.setGravity(Gravity.CENTER); 
mTextView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, 
LayoutParams.WRAP_CONTENT)); 
mButtonView = new ImageButton(context); 
mButtonView.setImageDrawable(mSrc); 
mButtonView.setBackgroundDrawable(null); 
mButtonView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, 
LayoutParams.WRAP_CONTENT)); 
mButtonView.setOnClickListener(this); 
mBackgroundView = new ImageView(context); 
mBackgroundView.setImageDrawable(mBackground); 
mBackgroundView.setLayoutParams(new LayoutParams( 
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
addView(mBackgroundView); 
addView(mButtonView); 
addView(mTextView); 
this.setOnClickListener(this); 
a.recycle(); 
} 
@Override 
protected void onAttachedToWindow() { 
super.onAttachedToWindow(); 
mParams = (LayoutParams) mButtonView.getLayoutParams(); 
if (mParams != null) { 
mParams.gravity = Gravity.CENTER_HORIZONTAL | Gravity.TOP; 
mButtonView.setLayoutParams(mParams); 
} 
mParams = (LayoutParams) mBackgroundView.getLayoutParams(); 
if (mParams != null) { 
mParams.gravity = Gravity.CENTER_HORIZONTAL | Gravity.TOP; 
mBackgroundView.setLayoutParams(mParams); 
} 
mParams = (LayoutParams) mTextView.getLayoutParams(); 
if (mParams != null) { 
mParams.gravity = Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM; 
mTextView.setLayoutParams(mParams); 
} 
} 
public void setCustomListener(CustomListener l) { 
customListener = l; 
} 
@Override 
public void onClick(View v) { 
if (customListener != null) { 
customListener.onCuscomClick(v, mCustomId); 
} 
} 
public interface CustomListener { 
void onCuscomClick(View v, int custom_id); 
} 
} 

代码很简单,就不多说,下面来看看我们的CustomView是怎么用的,请看:
3、自定义控件的使用
话不多说,请看代码,main.xml:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:nanlus="http://schemas.android.com/apk/res/com.nanlus.custom" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" > 
<LinearLayout 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_centerHorizontal="true" 
android:layout_centerVertical="true" 
android:orientation="horizontal" > 
<com.nanlus.custom.CustomView 
android:id="@+id/custom1" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_weight="1" 
nanlus:background="@drawable/background" 
nanlus:custom_id="1" 
nanlus:src="@drawable/style_button" 
nanlus:text="按钮1" > 
</com.nanlus.custom.CustomView> 
</LinearLayout> 
</RelativeLayout> 

在这里需要解释一下,
xmlns:nanlus=”http://schemas.android.com/apk/res/com.nanlus.custom”
nanlus为在xml中的前缀,com.nanlus.custom为包名
4、在Activity中,直接上代码

package com.nanlus.custom; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.ImageButton; 
import android.widget.ImageView; 
import android.widget.TextView; 
import android.widget.Toast; 
import com.nanlus.BaseActivity; 
import com.nanlus.custom.R; 
import com.nanlus.custom.CustomView.CustomListener; 
public class CustomActivity extends BaseActivity implements CustomListener { 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.main); 
((CustomView) this.findViewById(R.id.custom1)).setCustomListener(this); 
} 
@Override 
public void onCuscomClick(View v, int custom_id) { 
switch (custom_id) { 
case 1: 
Toast.makeText(this, "hello !!!", Toast.LENGTH_LONG).show(); 
break; 
default: 
break; 
} 
} 
} 

以上来自:http://www.jb51.net/article/32172.htm

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值