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

android的自定义控件及自定义属性 - 相当有用呢

2013-08-15 23:05 337 查看
废话不多说:

自己首先要定义自己的属性:

1. 在values文件夹下面建立1个自己的属性文件 - 名字随意起 比如 attr.xml

2.写自己的属性

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="my_button_edit">
          <attr name ="button_text"  format ="string"/>
          <attr name ="edit_text"    format ="string"/>
          <attr name = "button_back"  format="reference"/>
    </declare-styleable>
</resources>


格式就按着上面的套: name 是自己想要创建的属性的名字 format是你的这个属性里面放神马的(里面规定的东西 下面会介绍)。

现在自定义1个布局,我定义了1个简单的 (就1个button + editText)



其实你随便定义了 理解了这个后面自己随意弄。(这个整体就是你自己的新建立的 View了)

然后你当然要让他受你的控制。 所以 你取得 外界给你的值 - 然后 你再塞到 你的这个View中的 每个子View(晕的话往下看)




看到你上面自己定义的属性了吧,别人给你创建的属性 赋值 —— 然后你得到这个值 —— 然后去干你想干的事 (想要得到就要建立我上面的图的那个 类了。)

package com.lilei.custome;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

public class Custome_Button_EditText  extends LinearLayout {

	private Button    button;
	private EditText  editText;
	private String    button_text;
	private String    edit_text;
	private View      view;  
	private Drawable       resource;
	
	public Custome_Button_EditText(Context context, AttributeSet attrs) {
		   
		    super(context, attrs);
		    
		    // 得到你定义的属性集合
		    TypedArray type = context.obtainStyledAttributes(attrs ,R.styleable.my_button_edit);
		    // 把你定义的属性   button_text 得到的值给   button_text 字符串
		    button_text = type.getString(R.styleable.my_button_edit_button_text);
		    // 把你定义的属性   edit_text 得到的值给      edit_text字符串
		    edit_text   = type.getString(R.styleable.my_button_edit_edit_text);
		    //一样
		    resource   =  type.getDrawable(R.styleable.my_button_edit_button_back);
		    //在这里得到你自己定义的 那个View整体
		    view   =  LayoutInflater.from(context).inflate(R.layout.custome_view, null);
		    //把里面的东西取来 -
		    button   =  (Button) view.findViewById(R.id.button1);
		    editText =  (EditText) view.findViewById(R.id.editText1);

		    //将 你自定义属性取到的值   给     你的布局中的 每个小东西吧 - -
		    button.setText(button_text);
		    editText.setText(edit_text);
		    button.setBackgroundDrawable(resource);
		    this.addView(view);
		    
		    type.recycle();
		
	}

	
	public Custome_Button_EditText(Context context) {
		super(context);

	}

}


现在要引用你自己的自定义View 和 自定义属性(我设置了 2个内容和 按钮的图片):

<?xml version="1.0" encoding="utf-8"?>
<!--  xmlns:后面这个名字可以自己取,后面用的时候要相对应 -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:custome="http://schemas.android.com/apk/res/com.lilei.custome"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
   
   <com.lilei.custome.Custome_Button_EditText
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         custome:button_text="测试button"
         custome:edit_text="测试edit"
         custome:button_back="@drawable/icon"
      
      />
</LinearLayout>


看清楚上面的:用到自定义的的属性的时候 你要在上面加入自己的命名空间 规则就是 res/xx.xx.xx 自己的自己定义属性类的 包名。我的是 res/com.lilei.custome。

自定义View的 标记要写成 你的包名 + 自定义view的那个类名 <com.lilei.custome.Custome_Button_EditText / >

给张上面 demo的丑丑的效果图- -



其实很简单, 就是1个 别人把 值给你定义的属性 —— 你取出来 —— 再干你想干的事情

这个东西真的很有用: 比如你要做注册: 个人资料啥的,好多重复的控件,我们一个项目中确实用了好多呢。来张图就懂了。(当然那个 下拉箭头也是自定义可重用哦)



下面贴上各类 format的属性:

reference

<attr name = "background" format = "reference"
/>

XXXX:background = "@drawable/图片ID"

color:

<attr name = "textColor" format = "color"
/>

XXXX:textColor =
"#00FF00"

这些很简单我贴类似了哦- -

float <attr name = "myString" format
= "float" /> XXXX:myString =
"1.0"

integer <attr
name = "apiKey" format = "string"
/> android:framesCount = "12"



string 上面有了呢- -

.
fraction:百分数。
<attr name = "baifen" format = "fraction"
/> XXXX:baifen = "200%"

.

特别说下:enum:枚举值,有点差别

(1)属性定义:

<declare-styleable name="名称">

<attr name="heihei">

<enum name="a"
value="0" />

<enum name="b"
value="1" />

</attr>
android:heihei= "a"

. flag:位或运算(这个事从别处拿的没用过- -)

(1)属性定义:

<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"
/>

<flag name = "stateVisible" value = "4" />

<flag name = "stateAlwaysVisible" value = "5"
/>

<flag name = "adjustUnspecified" value = "0x00"
/>

<flag name = "adjustResize" value = "0x10" />

<flag name = "adjustPan" value = "0x20" />

<flag name = "adjustNothing" value = "0x30" />

</attr>

</declare-styleable>

(2)属性使用:

<activity

android:name = ".StyleAndThemeActivity"

android:label = "@string/app_name"

android:windowSoftInputMode = "stateUnspecified | stateUnchanged | stateHidden">

<intent-filter>

<action android:name = "android.intent.action.MAIN" />

<category android:name = "android.intent.category.LAUNCHER" />

</intent-filter>

</activity>

属性定义时可以指定多种类型值。

<declare-styleable name = "名称">

<attr name = "XXXXX" format = "reference|color"
/>

</declare-styleable>

(2)属性使用:

<LinearLayout

android:layout_width = "42dip"

android:layout_height = "42dip"

android:XXXX= "@drawable/图片ID
| #00FF00"

/>
好了了解那么多就基本够用了呢
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: