开发者社区> 问答> 正文

关于android 关于自定义view的问题

public class MyLayout extends RelativeLayout{
private Context context;

public MyLayout(Context context){
    super(context);
    this.context = context;

    RelativeLayout main = new RelativeLayout(context);

    RelativeLayout.LayoutParams lp_main_view = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT);

    main.setBackgroundColor(Color.parseColor("#FF00FF"));

    main.setLayoutParams(lp_main_view);

    /*GifView gif_view = new GifView(context);
    gif_view.setGifImage(R.drawable.gif_test);


    RelativeLayout.LayoutParams lp_gif_view = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    lp_gif_view.addRule(RelativeLayout.ALIGN_PARENT_TOP);
    lp_gif_view.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
    // btn1 位于父 View 的顶部,在父 View 中水平居中
    main.addView(gif_view, lp_gif_view);*/


    TextView t = new TextView(context);
    t.setText("怎么就不显示呢");
    RelativeLayout.LayoutParams lp_t_view = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

    lp_t_view.addRule(RelativeLayout.ALIGN_PARENT_TOP);
    lp_t_view.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);

    main.addView(t, lp_t_view);
}

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    super.onLayout(changed, l, t, r, b);
}

/**
 * 测量尺寸时的回调方法
 */
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}


}

以上是我自定义的view

我在activity里

`MyLayout myLayout = new MyLayout(this);
setContentView(myLayout);`

以这种方式加载 为什么显示不出来

展开
收起
爵霸 2016-03-18 14:29:57 2025 0
1 条回答
写回答
取消 提交回答
  • 自定义view一般都放在布局里,然后再用id去初始化这个自定义view

    2019-07-17 19:06:39
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
58同城Android客户端Walle框架演进与实践之路 立即下载
Android组件化实现 立即下载
蚂蚁聚宝Android秒级编译——Freeline 立即下载