<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="Bottom">
<attr name="oneText" format="string"/>
<attr name="twoText" format="string"/>
<attr name="threeText" format="string"/>
<attr name="oneTextSize" format="dimension"/>
<attr name="twoTextSize" format="dimension"/>
<attr name="ThreeTextSize" format="dimension"/>
<attr name="oneTextColor" format="color"/>
<attr name="twoTextColor" format="color"/>
<attr name="ThreeTextColor" format="color"/>
<attr name="oneImageIcon" format="reference|color"/>
<attr name="twoImageIcon" format="reference|color"/>
<attr name="threeImageIcon" format="reference|color"/>
<attr name="backColor" format="color"/>
</declare-styleable>
</resources>
<com.example.myappzdybottom.zdy.MyBottom
android:layout_width="fill_parent"
android:layout_height="200dp"
mybottom:oneText="第一个"
mybottom:twoText="第二个"
mybottom:oneTextSize="8sp"
mybottom:oneTextColor="#00FFFF"
mybottom:twoTextColor="#FF0000"
></com.example.myappzdybottom.zdy.MyBottom>
public class MyBottom extends LinearLayout{
//定义自定义属性对应的控件
private TextView textview01,textview02,textview03;
private ImageView imageview01,imageview02,imageview03;
private String text01,text02,text03="";
private Drawable drawable01,drawable02,drawable03;
private int textColor01,textColor02,textColor03,backColor;
private float textSize;
private LinearLayout ll01,ll02,ll03;
public MyBottom(Context context, AttributeSet attrs) {
super(context, attrs);
//建立映射
TypedArray obtainStyledAttributes = context.obtainStyledAttributes(attrs, R.styleable.Bottom);
text01=obtainStyledAttributes.getString(R.styleable.Bottom_oneText);
text02=obtainStyledAttributes.getString(R.styleable.Bottom_twoText);
**_System.out.println("-----------------------------"+text01+"-----------"+text02);_**
text03=obtainStyledAttributes.getString(R.styleable.Bottom_threeText);
drawable01=obtainStyledAttributes.getDrawable(R.styleable.Bottom_oneImageIcon);
drawable02=obtainStyledAttributes.getDrawable(R.styleable.Bottom_twoImageIcon);
drawable03=obtainStyledAttributes.getDrawable(R.styleable.Bottom_threeImageIcon);
textColor01=obtainStyledAttributes.getColor(R.styleable.Bottom_oneTextColor, 0);
textColor02=obtainStyledAttributes.getColor(R.styleable.Bottom_twoTextColor, 0);
textColor03=obtainStyledAttributes.getColor(R.styleable.Bottom_ThreeTextColor, 0);
backColor=obtainStyledAttributes.getColor(R.styleable.Bottom_backColor, 0);
textSize=obtainStyledAttributes.getDimension(R.styleable.Bottom_oneTextSize, 0);
obtainStyledAttributes.recycle();
//实例化控件
textview01=new TextView(context);
textview02=new TextView(context);
textview03=new TextView(context);
imageview01=new ImageView(context);
imageview02=new ImageView(context);
imageview03=new ImageView(context);
textview01.setText(text01);
textview02.setText(text02);
textview03.setText(text03);
textview01.setTextColor(textColor01);
textview02.setTextColor(textColor02);
textview03.setTextColor(textColor03);
textview01.setTextSize(textSize);
textview02.setTextSize(textSize);
textview03.setTextSize(textSize);
imageview01.setBackground(drawable01);
imageview02.setBackground(drawable02);
imageview03.setBackground(drawable03);
LayoutParams llParams=new LayoutParams(80, LayoutParams.FILL_PARENT );
LayoutParams llParams02=new LayoutParams(150, LayoutParams.FILL_PARENT);
ll01=new LinearLayout(context);
ll02=new LinearLayout(context);
ll03=new LinearLayout(context);
ll01.setBackgroundColor(textColor01);
ll02.setBackgroundColor(textColor02);
setOrientation(LinearLayout.HORIZONTAL);
LayoutParams textParams=new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
addView(textview01,llParams);
addView(textview02,llParams02);
}
自定义的属性 在自定义控件中使用 xml中赋值是也没报错 但是在自定义的view中使用 打印结果为null 是值引用不过来吗 求指教啊