水儿岐 2015-10-23 06:13 采纳率: 0%
浏览 2318

自定义控件不显示问题

自定义属性
<?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="#FFFFFF"
       mybottom:twoTextColor="#FF0000"
   ></com.example.myappzdybottom.zdy.MyBottom>

自定义view

 public class MyBottom extends RelativeLayout{

    //定义自定义属性对应的控件
    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);
        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(textColor01);
        textview03.setTextColor(textColor01);

        textview01.setTextSize(textSize);
        textview02.setTextSize(textSize);
        textview03.setTextSize(textSize);

        imageview01.setBackground(drawable01);
        imageview02.setBackground(drawable02);
        imageview03.setBackground(drawable03);



        LayoutParams llParams=new LayoutParams(80, LayoutParams.MATCH_PARENT );
        llParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, TRUE);

        LayoutParams llParams02=new LayoutParams(150, LayoutParams.MATCH_PARENT);
        llParams02.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, TRUE);

        ll01=new LinearLayout(context);
        ll02=new LinearLayout(context);
        ll03=new LinearLayout(context);

        ll01.setBackgroundColor(Color.BLACK);
        ll02.setBackgroundColor(Color.BLUE);



    /*  ll01.setOrientation(LinearLayout.VERTICAL);
        ll02.setOrientation(LinearLayout.VERTICAL);
        ll03.setOrientation(LinearLayout.VERTICAL);

        LayoutParams textParams=new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        LayoutParams imageParams=new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

        textview01.setGravity(Gravity.CENTER_HORIZONTAL);*/

        LayoutParams textParams=new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        ll01.addView(textview01, textParams);
        //ll01.addView(imageview01, imageParams);
        ll02.addView(textview02,textParams);
        //ll02.addView(imageview02, imageParams);
        /*ll03.addView(textview03, imageParams);    
        ll03.addView(imageview03, imageParams);*/

        addView(ll01,llParams);
        addView(ll02,llParams02);


    }

想在父view中添加三个字view为LinearLayout 这个可以 但是在子view中在添加TextView 就不行了 TextView 就是不显示 找不到原因,也没报错,请大神指教!!! 万分感谢!!!!

  • 写回答

4条回答

  • Clare_Ju 2015-10-23 06:21
    关注

    imageParams的问题,单次使用有效

    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!