Android 自定义控件属性,自定义Dialog定位
2014-10-22 11:39
453 查看
Android自定义控件的属性,网上文章已经很多,之前看了也照着写了,其中有一个就是要自定义一个xml的命名空间后然后再给自定义属性赋值,后来发现不知道什么时候开始Android把这个改了,统一用
xmlns:app="http://schemas.android.com/apk/res-auto"
然后在用app作为命名空间给自定义属性赋值,例如:app:myimage_src="@drawable/myimage"
当然了,这个属性肯定是要在res/values/attrs.xml 里面声明过:
<resources>
<declare-styleable name="CusComponent">
<attr name="myimage_src"
format="reference"/>
</declare-styleable>
</resources>
此外,format 还有很多的属性,例如boolean,enum:如官方例子:
然后在代码总获取到你设置的属性:
在两参数的构造函数中:
public CustomComponent(Context context, AttributeSet attrs) {super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CusComponent);
int imageSrcId;
try {
imageSrcId = a.getResourceId(R.styleable.CusComponent_myimage_src,R.drawable.myimage);
finally {
a.recycle();
}
LayoutInflater inflater = LayoutInflater.from(context);
inflater.inflate(R.layout.custom_component_layout, this, true); // 给自定义控件设置布局
b = (ImageButton)findViewById(R.id.btn); // 获取到布局上的ImageButton
b.setImageResource(imageSrcId);
}
当然其实还有很多个方法都是可以获取到属性的,这个比较简单而已。
需要注意的是,通常,我们在给自定义的控件设置好属性后会调用invalidate() 和 requestLayout() 方法对UI进行刷新,确保其显示。
以上是自定义控件属性的一些基本知识,然后项目中在做自定义控件的时候还学习了自定义dialog的定位:
代码很简单:
我把dialog定位在了top的位置,并且水平居中。当然你可以根据自己的需要定位在left、top,或者right这样。要说的是这里的wlp里还有两个关于定位的属性,x,y,这两个属性是根据坐标定位的,不过这个就意味着单位是像素piexl,因此用着不是很方便。
xmlns:app="http://schemas.android.com/apk/res-auto"
然后在用app作为命名空间给自定义属性赋值,例如:app:myimage_src="@drawable/myimage"
当然了,这个属性肯定是要在res/values/attrs.xml 里面声明过:
<resources>
<declare-styleable name="CusComponent">
<attr name="myimage_src"
format="reference"/>
</declare-styleable>
</resources>
此外,format 还有很多的属性,例如boolean,enum:如官方例子:
<resources> <declare-styleable name="PieChart"> <attr name="showText" format="boolean" /> <attr name="labelPosition" format="enum"> <enum name="left" value="0"/> <enum name="right" value="1"/> </attr> </declare-styleable> </resources>
然后在代码总获取到你设置的属性:
在两参数的构造函数中:
public CustomComponent(Context context, AttributeSet attrs) {super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CusComponent);
int imageSrcId;
try {
imageSrcId = a.getResourceId(R.styleable.CusComponent_myimage_src,R.drawable.myimage);
finally {
a.recycle();
}
LayoutInflater inflater = LayoutInflater.from(context);
inflater.inflate(R.layout.custom_component_layout, this, true); // 给自定义控件设置布局
b = (ImageButton)findViewById(R.id.btn); // 获取到布局上的ImageButton
b.setImageResource(imageSrcId);
}
当然其实还有很多个方法都是可以获取到属性的,这个比较简单而已。
需要注意的是,通常,我们在给自定义的控件设置好属性后会调用invalidate() 和 requestLayout() 方法对UI进行刷新,确保其显示。
以上是自定义控件属性的一些基本知识,然后项目中在做自定义控件的时候还学习了自定义dialog的定位:
代码很简单:
Dialog dialog = new Dialog(MainActivity.this, R.style.dialog); Window window = dialog.getWindow(); WindowManager.LayoutParams wlp = window.getAttributes(); wlp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.TOP; window.setAttributes(wlp); dialog.setContentView(R.layout.cloud_dialog_view); dialog.show();
我把dialog定位在了top的位置,并且水平居中。当然你可以根据自己的需要定位在left、top,或者right这样。要说的是这里的wlp里还有两个关于定位的属性,x,y,这两个属性是根据坐标定位的,不过这个就意味着单位是像素piexl,因此用着不是很方便。
相关文章推荐
- Android 自定义控件属性,自定义Dialog定位
- Android 自定义控件属性,自定义Dialog定位
- android中如何使用自定义view,自定义控件属性,及动态自定义控件
- android自定义控件中自定义属性的处理方式
- Android自定义控件并且使其可以在xml中自定义属性
- Android UI 之一步步教你自定义控件(自定义属性、合理设计onMeasure、合理设计onDraw等)
- android 自定义控件 自定义属性详细介绍
- android 自定义控件,自定义属性设置
- Android自定义控件:如何使用自定义的XML属性
- Android 自定义控件属性,自定义Dialog定位
- Android自定义控件系列四:自定义开关按钮(三)--- 自定义属性
- 【转】Android自定义控件中自定义属性的处理方式
- android 自定义控件 自定义属性详细介绍
- Android UI 之一步步教你自定义控件(自定义属性、合理设计onMeasure、合理设计onDraw等)
- 【android自定义控件】自定义View属性
- Android自定义控件以及控件属性的自定义
- android 自定义View研究(二) — 自定义控件添加属性
- android 自定义控件 自定义属性 自定义Style详细介绍
- Android自定义控件 自定义属性
- Android自定义控件——自定义属性