水儿岐 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的问题,单次使用有效

    评论

报告相同问题?

悬赏问题

  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退