安卓开发,自定义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>
求各位大神赐教啊
...全文
1929 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
lykAnd 2017-12-20
  • 打赏
  • 举报
回复
2楼思路是对的。 textView.setWidth(WindowManager.LayoutParams.WRAP_CONTENT); textView.setHeight(WindowManager.LayoutParams.WRAP_CONTENT); 这两个方法是通过传入实际的数值来设置TextView的宽高,你这里传入的参数值为-2,这样TextView的宽高都是-2,当然无法显示了。 你可以把这两句删掉,或者传入一个大于0的正整数(比如100)试试。
XEZBZ 2017-12-20
  • 打赏
  • 举报
回复
引用 4 楼 lyklykkk 的回复:
2楼思路是对的。 textView.setWidth(WindowManager.LayoutParams.WRAP_CONTENT); textView.setHeight(WindowManager.LayoutParams.WRAP_CONTENT); 这两个方法是通过传入实际的数值来设置TextView的宽高,你这里传入的参数值为-2,这样TextView的宽高都是-2,当然无法显示了。 你可以把这两句删掉,或者传入一个大于0的正整数(比如100)试试。
谢谢大神
XEZBZ 2017-12-20
  • 打赏
  • 举报
回复
引用 2 楼 jklwan 的回复:
Button默认有样式,可以显示。你的textview看看设置错了
        
textView.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
textView.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
把这两句删掉,setWidth,setHeight可不是设置宽高,是设置最大宽高,你给设置成-2了。设置宽高用setLayoutParams
确实是这样的,多谢大神指点
jklwan 2017-12-19
  • 打赏
  • 举报
回复
Button默认有样式,可以显示。你的textview看看设置错了
        
textView.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
textView.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
把这两句删掉,setWidth,setHeight可不是设置宽高,是设置最大宽高,你给设置成-2了。设置宽高用setLayoutParams
雨焰 2017-12-19
  • 打赏
  • 举报
回复
你的linearlayout没有设置方向,你的button把你的textview给覆盖到下面去了
XEZBZ 2017-12-19
  • 打赏
  • 举报
回复
引用 1 楼 f839903061 的回复:
你的linearlayout没有设置方向,你的button把你的textview给覆盖到下面去了
不是这样的哦,我是单独添加button是能显示button,但是单独添加textView时就不能显示textView,不是两个同时添加的

80,471

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧