水儿岐 2015-10-23 07:05 采纳率: 0%
浏览 3895

自定义控件引用不到自定义的属性值

 <?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 是值引用不过来吗 求指教啊

  • 写回答

3条回答 默认 最新

  • androideveloper 2015-10-23 08:39
    关注

    哥哥 你打 syso的地方他还没初始化,能不是null么

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog