问题描述
- 自定义控件不显示问题
-
自定义属性 <?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 就是不显示 找不到原因,也没报错,请大神指教!!! 万分感谢!!!!
解决方案
自定义控件在sumsung手机上第一次显示 第二次 不显示子控件的问题解决
用户自定义控件 界面 不显示问题
解决自定义控件闪烁的问题
----------------------
解决方案二:
imageParams的问题,单次使用有效
解决方案三:
imageParams的问题,单次使用有效
解决方案四:
没有用imageParams 我有运行了一下
<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"
中引用的属性的值 在自定义的view中打印为null oneText打印为null 自定义属性的值好像不能在自定义view中引用过来 不知道为什么
解决方案五:
你调试的结果是什么?看下obtainStyledAttributes 的值
时间: 2024-10-23 21:21:31