安卓开发,自定义LinearLayout,通过addView方法添加TextView,无法显示,但是添加Button却能显示,这是为什么?
XEZBZ 2017-12-18 10:31:11 自己定义了一个空间,继承了LinearLayout,在空间的构造方法中new了一个TextView,
通过addView添加TextView,但是不显示,试了new一个Button,也在构造方法中addView
添加这个button,却能显示出来,请问这是为什么,菜鸟一枚,请各位大神赐教
这是自定义的LinearLayout代码:
public class LoopView extends LinearLayout {
public LoopView(Context context){
super(context);
}
public LoopView(Context context, AttributeSet attributeSet){
super(context,attributeSet);
TextView textView = new TextView(context);
textView.setText("this is title");
textView.setTextColor(Color.RED);
textView.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
textView.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP,17);
this.addView(textView);//没法显示出来
/*如果添加的是一个Button,就能正常的显示出来*/
/*Button btn = new Button(context);
btn.setText("this is btn");
this.addView(btn);*/
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}
线面是布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.lyz.viewpage.activity.LoopView
android:layout_width="match_parent"
android:layout_height="match_parent"
></com.lyz.viewpage.activity.LoopView>
</LinearLayout>
求各位大神赐教啊