❶ android自定义布局的属性获取问题
下面图片是android学习手册中关于attr的实例图,点击源码可以查看例子源码,点击文档可以查看文档。360手机助手中可下载。
java"><LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:test="http://schemas.android.com/apk/res/com.android.tutor"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<com.android.tutor.MyView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
test:textSize="20px"
test:textColor="#fff"
/>
</LinearLayout>
❷ 如何搭建android framework开发环境搭建
framework的开发比应用层就要烦的多啦。做应用在eclipse中就足够了,用android系统中的控件等工具,或者是自己写个类来实现特定的功能。而framework层的开发,需要往源码中添加代码、xml、图片、id等等数据,这个id可是费了我好大的劲才搞定的。在项目开始的一个半月里,我探索、尝试了很多,现在把我的经验分享出来。网上关于framework层的开发信息很少,多是靠自己。 最有效的方式就是分析android的源码,看google是怎样实现一个类的,以及类的层次。我现在看的主要是widget和app中的代码,其他的还没涉及。像View,ViewGroup,Activity,ActivityThread都是非常重要的类,也是代码量很大的类,我只是大概地过了下,还没有仔细分析过。 我花大力气的地方是资源文件夹下values中几个文件的作用。 attrs.xml中定义的是类的属性,属性是为了能在xml文件中被引用到,换句话说就是指定类中变量(也就是属性的实际作用者)的值。这些属性会在类的构造函数中用到。看过一两个源码就会明白,构造函数中的TypedArray其实就是属性的数组,数组的成员会被赋给类里的成员,完成从xml的初始化。类的构造函数一般有三个,一个是Class(context),这个用于在代码中创建一个类,所以只包含一个上下文;Class(context, attrs)和Class(context, attrs, defStyle)用于从xml创建类的情况。 styles.xml中是各个控件的“样式”,样式由一个个属性所组成。我们在编辑xml文件的时候,不同的类所能设置的属性有共同的,也有不同的,都是由样式来控制的。具体可以去看styles.xml这个文件的内容。 themes.xml其实也是样式,只是适用的范围大一点,这个我是参考网上别人的看法,自己还没研究过。 ids.xml是公共的id,也就是对应用层可见的id,id是为了获得xml中的对象而需要的参数,也就是Object = findViewById(R.id.id_name)中的id_name。这些值可以在代码中用android.R.id引用到 public.xml描述的是为attr、id、drawable所指定的一个32的id值,这个值在current.xml文件中会被用到。 当我们对public.xml做了改动后,执行make update-api命令,相应的current.xml就会添加这些改动。因为这些值按类型被放在不同的段上,类型相同的必须连续存放,不指定id值的话系统会自动计算出下一个值。 添加id的方法:1)在ids.xml中添加公共id项,在public.xml中也添加相应的id项。这种办法没有兼容性,会与后面的版本产生冲突。 2)在ids.xml中添加公共id项,每一项前面加一行的注释,这样就不用在public.xml中添加相应的项了。源码中是/** @hide */的形式,这样current中就不会有相应的段了。 3)在自己写的xml中,使用android:id = "@+id/id_name"的形式,就和在eclipse中一样,搜一下会发现android源码中也有这么用的,这样ids.xml和public.xml都不用改,兼容性我还不确定。 上面这些希望能对大家有所帮助吧。
❸ android控件参数 attribute怎么写
1.Attributeset自定义控件和其他的控件一样,应该写成一个类,而这个类的属性是是有自...
2.首先要在res/values目录下建立一个attrs.xml的文件,并在此文件中增加对控件的属性...
3.使用AttributeSet来完成控件类的构造函数,并在构造函数中将自定义控件类中变量
❹ Android自定义属性TypedArray详解
大家好,我是程序员双木L,后续会发专题类的文章,这是自定义控件的第一篇,之后也会陆续更新相关的文章,欢迎关注。
自定义属性在自定义控件过程中属于比较常见的操作,我们可以回想一下这样的场景:自定义view的过程中,我们需要在不同的情况下设置不同的文字大小,那么我们是不是就需要提供对外的方法来设置,这样就比较灵活操作。而我们自定义对外的方法,就是我们自定义的属性啦,那我们来分析一下其原理及作用。
下面我们根据例子来进行分析:
1、首先我们需要在res->values目录下新建attrs.xml文件,该文件就是用来声明属性名及其接受的数据格式的,如下:
attr名词解析:
name表示属性名,上面的属性名是我自己定义的。
format表示接受的输入格式,format格式集合如下:
2、自定义属性的使用,这里我们使用两种方式进行对比解析
最最最原始的使用方式
(1)、自定义文件如下:
我们可以在TestAttrsView方法的参数AttributeSet是个xml解析工具类,帮助我们从布局的xml里提取属性名和属性值。
(2)、在布局文件xml中的使用
这里使用自定义属性需要声明xml的命名空间,其中app是命名空间,用来加在自定义属性前面。
xmlns:app=" http://schemas.android.com/apk/res-auto "
声明xml命名空间,xmlns意思为“xml namespace”.冒号后面是给这个引用起的别名。
schemas是xml文档的两种约束文件其中的一种,规定了xml中有哪些元素(标签)、
元素有哪些属性及各元素的关系,当然从面向对象的角度理解schemas文件可以
认为它是被约束的xml文档的“类”或称为“模板”。
(3)、将属性名与属性值打印结果如下:
从打印结果我们可以看出,AttributeSet将布局文件xml下的属性全部打印出来了,细心的童鞋可能已经看出来:
这个属性我们设置的是一个整数尺寸,可最后打印出来的是资源编号。
那如果我们想要输出我们设置的整数尺寸,需要怎么操作呢?
这个时候就该我们这篇的主角出场了,使用TypedArray方式。
(1)、这里我们需要将attrs.xml使用“declare-styleable”标签进行改造,如下:
从改造后的attrs文件可以看出,我们将属性声明归结到TestStyleable里面,也就意味着这些属性是属于TestStyleable下的。
(2)、属性的解析:
这里我直接打印出解析结果,这里可以获取我们想要的自定义属性,而系统有的属性可以忽略。
(3)、运行结果如下
从解析的结果可以看出,尺寸的结果已经转换为实际值了:
这个时候有童鞋又问了,我设置的是15dp,为啥最后打印是41.25了呢?其实解析出来的值单位是px,所以这里输出的是转换后的值。
解析的过程中用到了这个方法:
我们来看一下这个方法的源码:
源码中我们可以看到这个方法有两个参数:
obtainStyledAttributes方法返回值类型为TypedArray。该类型记录了获取到的属性值集合,而通过数组下标索引即可找到对应的属性值。索引下标通过R.styleable.TestStyleable_xx获取,"xx"表示属性名,一般命名为"styleable名" + "_" + "属性名"。
而TypedArray提供了各种Api,如getInteger,getString,getDimension等方法来获取属性值,这些方法都需要传入对应属性名在obtainStyledAttributes中的int数组的位置索引,通过下标获取数组里属性值。
这个TypedArray的作用就是资源的映射作用,把自定义属性在xml设置值映射到class,这样怎么获取都很简单啦。
到这里就分析完啦!
❺ android studio attrs.xml在哪儿
1. 定义一些自定义属性
建立一个属性xml文件: values/attrs.xml, 内容如下:
<?xml version="1.0" encoding="utf-8" ?>
<resources>
<!-- the relation between the icon and text. -->
<attr name="relation">
<enum name="icon_left" value="0" />
<enum name="icon_right" value="1" />
<enum name="icon_above" value="2" />
<enum name="icon_below" value="3" />
</attr>
<skip />
<declare-styleable name="IconText">
<attr name="relation" />
<attr name="icon" format="reference" />
<attr name="text" format="string" />
<attr name="text_size" format="dimension" />
<attr name="text_color" format="integer" />
<attr name="space" format="dimension" />
</declare-styleable>
❻ android中如何在xml文件中增加属性
好的设置方式往往是数据驱动的,通过数据控制代码的运行,便于代码的维护和修改。在android中增中自定义的xml属性, 可以把数据存储在xml文件中,然后在代码中取出这些属性的值。分为以下几步:
1. 在attrs.xml文件中定义属性的类型,即字符串还是数值如下:
2.在xml 文件中指定譔属性的值。如下:
android:key="tiltSensitivity"
android:defaultValue="50"
android:title="@string/preference_tilt_sensitivity"
android:summary="@string/preference_tilt_sensitivity_summary"
replica:maxText="@string/preference_tilt_max"
replica:minText="@string/preference_tilt_min"
android:persistent="true"
android:dependency="enableTiltControls"/>
3.在代码中读取值,方法如下:
public SliderPreference(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.SliderPreference, defStyle, 0);
mMinText = a.getString(R.styleable.SliderPreference_minText);
❼ android中怎么在View构造的attrs中拿到android给的属性
//Android原生的属性,都是提供方法可以获得的,当然也可以通过
attrs获得,而自定义的属性获得值方式如下,当然原生的也是一样,只需要把attr name该成系统的。
一、 首先要在res/values目录下建立一个attrs.xml(名字可以自己定义)的文件,并在此文件中增加对控件的属性的定义.其xml文件如下所示:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="my_text_view">
<attr name="text_size" format="float"></attr>
<attr name="text_color" format="color"></attr>
<attr name="text_back_ground" format="color|reference"></attr>
</declare-styleable>
</resources>
在这里,需要补充attrs属性的相关知识,即Attr属性是如何在XML中定义的,自定义属性的Value值可以有10种类型以及其类型的组合值,其具体使用方法如下:
1. reference:参考某一资源ID。
(1)属性定义:
<declare-styleable name = "名称">
<attr name = "background" format = "reference" />
</declare-styleable>
(2)属性使用:
<ImageView
android:layout_width = "42dip"
android:layout_height = "42dip"
android:background = "@drawable/图片ID"
/>
2. color:颜色值。
(1)属性定义:
<declare-styleable name = "名称">
<attr name = "textColor" format = "color" />
</declare-styleable>
(2)属性使用:
<TextView
android:layout_width = "42dip"
android:layout_height = "42dip"
android:textColor = "#00FF00"
/>
3. boolean:布尔值。
(1)属性定义:
<declare-styleable name = "名称">
<attr name = "focusable" format = "boolean" />
</declare-styleable>
(2)属性使用:
<Button
android:layout_width = "42dip"
android:layout_height = "42dip"
android:focusable = "true"
/>
4. dimension:尺寸值。
(1)属性定义:
<declare-styleable name = "名称">
<attr name = "layout_width" format = "dimension" />
</declare-styleable>
(2)属性使用:
<Button
android:layout_width = "42dip"
android:layout_height = "42dip"
/>
5. float:浮点值。
(1)属性定义:
<declare-styleable name = "AlphaAnimation">
<attr name = "fromAlpha" format = "float" />
<attr name = "toAlpha" format = "float" />
</declare-styleable>
(2)属性使用:
<alpha
android:fromAlpha = "1.0"
android:toAlpha = "0.7"
/>
6. integer:整型值。
(1)属性定义:
<declare-styleable name = "AnimatedRotateDrawable">
<attr name = "visible" />
<attr name = "frameDuration" format="integer" />
<attr name = "framesCount" format="integer" />
<attr name = "pivotX" />
<attr name = "pivotY" />
<attr name = "drawable" />
</declare-styleable>
(2)属性使用:
<animated-rotate
xmlns:android = "http://schemas.android.com/apk/res/android"
android:drawable = "@drawable/图片ID"
android:pivotX = "50%"
android:pivotY = "50%"
android:framesCount = "12"
android:frameDuration = "100"
/>
7. string:字符串。
(1)属性定义:
<declare-styleable name = "MapView">
<attr name = "apiKey" format = "string" />
</declare-styleable>
(2)属性使用:
<com.google.android.maps.MapView
android:layout_width = "fill_parent"
android:layout_height = "fill_parent"
android:apiKey = "_bc_g"
/>
8. fraction:百分数。
(1)属性定义:
<declare-styleable name="RotateDrawable">
<attr name = "visible" />
<attr name = "fromDegrees" format = "float" />
<attr name = "toDegrees" format = "float" />
<attr name = "pivotX" format = "fraction" />
<attr name = "pivotY" format = "fraction" />
<attr name = "drawable" />
</declare-styleable>
(2)属性使用:
<rotate
xmlns:android = "http://schemas.android.com/apk/res/android"
android:interpolator = "@anim/动画ID"
android:fromDegrees = "0"
android:toDegrees = "360"
android:pivotX = "200%"
android:pivotY = "300%"
android:ration = "5000"
android:repeatMode = "restart"
android:repeatCount = "infinite"
/>
9. enum:枚举值。
(1)属性定义:
<declare-styleable name="名称">
<attr name="orientation">
<enum name="horizontal" value="0" />
<enum name="vertical" value="1" />
</attr>
</declare-styleable>
(2)属性使用:
<LinearLayout
xmlns:android = "http://schemas.android.com/apk/res/android"
android:orientation = "vertical"
android:layout_width = "fill_parent"
android:layout_height = "fill_parent"
>
</LinearLayout>
10. 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>
注意:
属性定义时可以指定多种类型值。
(1)属性定义:
<declare-styleable name = "名称">
<attr name = "background" format = "reference|color" />
</declare-styleable>
(2)属性使用:
<ImageView
android:layout_width = "42dip"
android:layout_height = "42dip"
android:background = "@drawable/图片ID|#00FF00"
/>
二、接下来实现自定义View的类,其中下面的构造方法是重点,在代码中获取自定义属性,其代码如下:
package com.example.CustomAttr;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.widget.TextView;
/**
* Created with IntelliJ IDEA.
* User: wen.nan
* Date: 13-9-28
* Time: 下午10:00
* To change this template use File | Settings | File Templates.
*/
public class CustomTextView extends TextView {
private TypedArray mTypedArray;
private Paint mPaint;
public CustomTextView(Context context) {
super(context);
}
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
inial(context,attrs);
}
public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
inial(context,attrs);
}
private void inial(Context context,AttributeSet attrs) {
TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.my_text_view);
float textsize = typedArray.getFloat(R.styleable.my_text_view_text_size,14) ;
int textColor = typedArray.getColor(R.styleable.my_text_view_text_color,0xFFFFFF) ;
int bgColor = typedArray.getColor(R.styleable.my_text_view_text_back_ground,0xFFFFFF) ;
super.setTextColor(textColor);
super.setTextSize(textsize);
super.setBackgroundColor(bgColor);
typedArray.recycle();
}
}
三、接下来在XML布局中引用自定义View控件,其XML代码如下:
?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app = "http://schemas.android.com/apk/res/com.example.CustomAttr"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<com.example.CustomAttr.CustomTextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello World, MainActivity"
app:text_size ="20"
app:text_color ="#00FF00"
app:text_back_ground="#ffffff"
/>
</LinearLayout>
注意上面XML中代码: xmlns:app = "http://schemas.android.com/apk/res/com.example.CustomAttr",是自定义的app命名空间,res后面是应用程序包名,然后可以直接使用app:text_size,等属性,其值类型要和attrs.xml定义的属性Value值相对应。
四、总结:
注意该例子中是使用app:text_size = "20 和app:text_color="#00FF00定义TextView的颜色和textView的字体大小,而不是使用系统的属性android:textsize等。该例子中只是起到抛砖引玉的作用,你可以自定义其他属性,来实现你想要的自定义View效果。