source code:
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MyView extends LinearLayout {
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray typedArray=context.obtainStyledAttributes(attrs, R.styleable.MyViewText);
TextView textView=new TextView(context);
int count=typedArray.getIndexCount();
for (int i = 0; i < count; i++) {
int attr=typedArray.getIndex(i);
switch (attr) {
case R.styleable.MyViewText_Text:
int resource=typedArray.getResourceId(R.styleable.MyViewText_Text, 0);
System.out.println(resource);
if(resource>0){//通过引用设值方式
textView.setText(typedArray.getResources().getText(resource));
}else{//通过直接设值方式
textView.setText(typedArray.getString(R.styleable.MyViewText_Text));
}
break;
default:
break;
}
}
addView(textView);
System.out.println("getChildCount:"+getChildCount());
System.out.println(getChildAt(0));
setBackgroundColor(0xaa00aa);
typedArray.recycle();
}
}
layout code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res/com.zhonghong.tsp.share"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<view class="com.demo.view.MyView" android:layout_width="wrap_content" android:layout_height="wrap_content" app:Text="@string/app_name">
<ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher"/>
</view>
<ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher"/>
</RelativeLayout>
attrs.xml code
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MyViewText">
<!--同时兼容两种设值方式-->
<attr name="Text" format="reference|string"></attr>
</declare-styleable>
</resources>
相关推荐
Android自定义组件开发详解
android自定义组件简单Demo
Android自定义组件开发详解,进阶书籍。本书教你如何更好使用自定义view
Android自定义组件开发详解,让你彻底理解自定义组件(自定义View、ViewGroup等,彻底了解canvas和paint的基本和高级使用方法)
《Android自定义组件开发详解》
Android自定义组件一[文].pdf
android 自定义组件android 自定义组件android 自定义组件android 自定义组件android 自定义组件android 自定义组件
Android自定义组件开发详解自制书签目录,版权属于原作者,纯制作书签方便大家使用,如有侵权联系删除
android自定义组件demo实例,里面有三种自定义组件,可以学习
6.2 自定义组件的基本结构 164 6.3 重写onMeasure方法 166 6.4 组件属性 175 6.4.1 属性的基本定义 175 6.4.2 读取来自style和theme中的属性 181 6.5 案例1:圆形ImageView组件 186 6.6 案例2:验证码组件CodeView ...
android开发中回会经常使用自定义组件,这边提供了一个非常基础的自定view动画
本文档详细讲解了Android自定义组件与特效,清晰易懂。
1) Android 自定义组件开发详解 2)view 绘图 ,界面等
Android自定义组件之日历控件-精美日历实现(内容、样式可扩展),参考博客:http://blog.csdn.net/daijin888888/article/details/47752723
Android自定义组件之自动换行View
android自定义组件可帮我们做出非常炫的手机界面
Android APIdemo中关于自定义组件的讲解,自己学习一下
一个仿Facebook和人人网的侧滑效果,详细请看:Android自定义组件系列【3】——自定义ViewGroup实现侧滑(http://blog.csdn.net/dawanganban/article/details/24007215)
Android自定义组件之日历控件-精美日历实现(内容、样式可扩展)【更新版】,参考博客:http://blog.csdn.net/daijin888888/article/details/47752723