Android 自定义View大全,Android自定义View以及Layout属性
第一:自定義MyView類,繼承View
1、MyView myView = new MyView(context);
2、在Layout文件夾的XML 中聲明
android:id="@+id/myview"
test:age="11"
test:city="feifei"
/>
3、增加? xmlns:test="http://schemas.android.com/apk/res/bin.feifei.first"
定義test命名空間和age、city兩個屬性, 在rec/values目錄下新建attr.xml文件,
format :
integer、float、boolean、string、color(#ff0000)、dimension(尺寸23dip,15px,18sp)???? \還有reference 用于@+id/test????? @drawable/xxx這樣的類型
4、使用obtainStyledAttributes的方法來獲取我們的定義
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyView);
int age = a.getInt(R.styleable.MyView_age, 12);
System.out.println(age);
paint = new Paint();
a.recycle(); //要記得釋放咧
}
總結
以上是生活随笔為你收集整理的Android 自定义View大全,Android自定义View以及Layout属性的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux 音频架构绕过,linux音频
- 下一篇: 运用Rhino,在Android上运行j