Android中自己定义组件和它的属性
2016-03-07 19:34
393 查看
好长时间没有更新博客了。本来想积累点有深度的东西发,但一直没有找到非常好的点。所以。写一些基础的东西。就当积累吧。
Android开发中难免会用到自己定义的组件。以下以ImageButton为例来介绍怎么自己定义组件和它的属性:
第一步、在values/attrs.xml中为组件自己定义属性:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="CustomBtn">
<attr name="text" format="string"/>
<attr name="textColor" format="color" />
<attr name="textSize" format="dimension" />
</declare-styleable>
</resources>
第二步、重写ImageButton类:
public class CustomBtn extends ImageButton
{
private Paint paint;
private String text;
public CustomBtn(Context context, AttributeSet attrs)
{
super(context, attrs);
paint=new Paint();
TypedArray typeArray=context.obtainStyledAttributes(attrs,R.styleable.CustomBtn);
int color=typeArray.getColor(R.styleable.CustomBtn_textColor,Color.WHITE);
float textSize=typeArray.getDimension(R.styleable.CustomBtn_textSize,20);
text=typeArray.getString(R.styleable.CustomBtn_text);
paint.setTextAlign(Align.CENTER);
paint.setColor(color);
paint.setTextSize(textSize);
typeArray.recycle();
}
@Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
canvas.drawText(text,canvas.getWidth()/2,canvas.getHeight()/2+10, paint);
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
第三步、在布局文件里使用CustomBtn:
当中xmlns:custombtn中为AndroidManifest.xml中的包名
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custombtn="http://schemas.android.com/apk/res/com.yeahis.shuyudragstore"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="@dimen/market_category_height"
android:background="@drawable/mall_category_item">
<com.yeahis.shuyudragstore.widget.CustomBtn
android:id="@+id/mall_category_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:background="@android:color/transparent"
android:src="@drawable/mall_category_title"
custombtn:text="@string/mall_category_title"
custombtn:textColor="@android:color/black"
custombtn:textSize="15sp"/>
</RelativeLayout>
第四步、假设想要在程序中动态改变CustomBtn上的文字则在程序中这样:
比如CustomBtn customBtn=(CustomBtn) convertView.findViewById(R.id.mall_category_btn);
customBtn.setText("在程序中加入的文字");
Android开发中难免会用到自己定义的组件。以下以ImageButton为例来介绍怎么自己定义组件和它的属性:
第一步、在values/attrs.xml中为组件自己定义属性:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="CustomBtn">
<attr name="text" format="string"/>
<attr name="textColor" format="color" />
<attr name="textSize" format="dimension" />
</declare-styleable>
</resources>
第二步、重写ImageButton类:
public class CustomBtn extends ImageButton
{
private Paint paint;
private String text;
public CustomBtn(Context context, AttributeSet attrs)
{
super(context, attrs);
paint=new Paint();
TypedArray typeArray=context.obtainStyledAttributes(attrs,R.styleable.CustomBtn);
int color=typeArray.getColor(R.styleable.CustomBtn_textColor,Color.WHITE);
float textSize=typeArray.getDimension(R.styleable.CustomBtn_textSize,20);
text=typeArray.getString(R.styleable.CustomBtn_text);
paint.setTextAlign(Align.CENTER);
paint.setColor(color);
paint.setTextSize(textSize);
typeArray.recycle();
}
@Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
canvas.drawText(text,canvas.getWidth()/2,canvas.getHeight()/2+10, paint);
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
}
第三步、在布局文件里使用CustomBtn:
当中xmlns:custombtn中为AndroidManifest.xml中的包名
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custombtn="http://schemas.android.com/apk/res/com.yeahis.shuyudragstore"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="@dimen/market_category_height"
android:background="@drawable/mall_category_item">
<com.yeahis.shuyudragstore.widget.CustomBtn
android:id="@+id/mall_category_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:background="@android:color/transparent"
android:src="@drawable/mall_category_title"
custombtn:text="@string/mall_category_title"
custombtn:textColor="@android:color/black"
custombtn:textSize="15sp"/>
</RelativeLayout>
第四步、假设想要在程序中动态改变CustomBtn上的文字则在程序中这样:
比如CustomBtn customBtn=(CustomBtn) convertView.findViewById(R.id.mall_category_btn);
customBtn.setText("在程序中加入的文字");
相关文章推荐
- android屏幕旋转时Activity不重新调用onCreate的方法
- LeakCanary——直白的展现Android中的内存泄露
- 《Android开发艺术探索》 自定义View
- Plugin with id 'android-apt' not found
- Android应用--如何向Android应用中加入广告
- Android音频开发(1):基础知识
- Android studio 如何查看模拟器里面的文件
- Android代码格式化方法和Eclipse重要快捷键
- Android DataBinding介绍
- Android 自定义View (三) 圆环交替 等待效果
- Android新手入门2016(2)--Mac下的Android开发环境
- 记录使用SeekBar遇到的一些错误
- Android开发---使用SharedPreferences保存数据
- Android开发小知识-XML文件解析之SAX(Simple API for XML)
- 专访邓凡平:Android开发路上的快速学习之道
- Android入门:监听ContentProvider数据改变
- 美团Android资源混淆保护实践
- android studio在小米2s 真机5.0.1系统上出现INSTALL_FAILED_NO_MATCHING_ABIS
- 美团Android DEX自动拆包及动态加载简介
- Android应用性能优化