导航:首页 > 操作系统 > androidenum定义

androidenum定义

发布时间:2023-12-15 01:51:38

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效果。

例子资源下载:
http://download.csdn.net/detail/nanwen666/6346979

❷ Android 怎么自定义枚举类型的值

static final通常跟的是一个int类型的常数,所以有时候可以用int型代替枚举 在有些时候需要输出这个枚举量的名字(比如January),上面的方法输出的是int值,用类的话可以把名字一起输出 总的来说前者消耗资源比较少,后者更符合枚举的定义 枚举的确是一个类,在JDK1.4及以前,没有enum这个用法,那时候都是使用类来建立的,在《Java编程思想》中介绍了一类写法(详见第三版的章节8.1.3群组常量);JDK5以后,enum被引入,本质上就是一个类,所以可以被继承,总体思路和第三版这个写法类似,只是换了个名字(《Java编程思想》第四版第19章专门讲这个) 补充:我把早期的例子给出吧,你看一下,这就是早期的枚举的方法: public final class Month{ private String name; private Month (String nm){name=nm;} public String toString(){return name;} public static final Month JAN=new Month("January"), FEB=new Month("February"), MAR=new Month("March"), APR=new Month("April"), MAY=new Month("May"), JUN=new Month("June"), JUL=new Month("July"), AUG=new Month("August"), SEP=new Month("September"), OCT=new Month("October"), NOV=new Month("November"), DEC=new Month("December"); public static final Month month[]={ JAN,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,NOV,DEC }; public static final Month number(int ord){ return month[ord-1]; } //main public static void main(String args[]){ Month m=Month.JAN; System.out.println(m); m=Month.number(12); System.out.println(m); System.out.println(m==Month.DEC); System.out.println(m.equals(Month.DEC)); System.out.println(Month.month[3]); System.out.println(Month.APR); } }

❸ 为什么说android使用enum效率低,浪费方法数

现在不会了,新版本JDK做了优化,效率高了很多,而且使用也变方便了。

❹ android 为什么枚举与泛型没有被混淆

首先.您得知道一个问题:泛型是运行时类型,枚举继承自ENUM 枚举是引用类型.
OK.接着往下说.
要限定所谓的泛型则:必须是接口、未封闭的类或者类型
从上述的两个情况来看.C#是不支持这样写的.
语法就不支持.

❺ arm-linux-androideabi于arm-linux-gnueabi区别

现在android的工程大部分是用clang编译,在之前的android工程主要是使用gcc,也就是arm-linux-androideabi-gcc,但是其他嵌入式系统使用的是arm-linux-gnueabi,那么这两个有什么区别呢? 能不能混用呢?
先说区别,根据linaro给的,主要有三处
1)There are a few differences between the Linux EABI and Android EABI definitions - most notably, Android defines the size of an enum to 32 bits while regular Linux defines it as variable.
2)Android does not currently use the hardfloat ABI conventions that have become common in regular Linux.
3)Android uses the Gold linker by default while the regular Linux builds default to using the BFD linker (this is for upstream compatibility - Android uses gold while most regular Linux distributions use the BFD linker).
翻译后是两者之前区别很少,主要有enum不一样,谷歌定义enum为32为,而其他的都定义为变量。android编译器使用的是softfp,而其他的使用的是hardfloat,如果使用linux-arm-gnueabi编译android工程的话,会出现“unrecognized option '-mfloat-abi=softfp'”就是指的这个,第三个是linker不同,这个应该好理解,android在bionic中定制了linker。

阅读全文

与androidenum定义相关的资料

热点内容
android内存溢出的原因 浏览:15
标志307的压缩比是多少 浏览:633
服务器启动为什么叫三声 浏览:995
追风筝的人英文pdf 浏览:936
解压小熊手机壳 浏览:346
成都市区建成面积算法 浏览:660
智能家居单片机 浏览:97
买男装用什么app好 浏览:855
文件夹合并了怎么拆开 浏览:260
波段副图源码无未来函数 浏览:89
livecn服务器地址 浏览:259
程序员这个工作真的很吃香吗 浏览:847
程序员和数学分析师待遇 浏览:681
压缩气弹簧怎么拆 浏览:324
华为公有云服务器添加虚拟ip 浏览:211
程序员和运营哪个累 浏览:27
抖音安卓信息提示音怎么设置 浏览:456
光速虚拟机的共享文件夹 浏览:251
程序员培训机构发的朋友圈真实性 浏览:744
天干地支简单算法 浏览:299