以编程方式在android中添加视图

时间:2012-10-18 05:47:50

标签: android

我必须在android中水平相邻地添加两个相对布局。我知道我必须使用addRule concept.But我没有一个正确的链接来实现这个。可以有人在android中提供一些关于addRule的好教程也解决了这个问题。谢谢![可以看出两个相对布局应该在一个父布局中。 以下是我的代码:

       //Layout on which my parentrelativelayout should be added 
        LinearaLayout ll=(LinearLayout)findViewbyId(R.id.ll);

        RelativeLayout parentrelativelayout=new RelativeLayout(mContext);
        parentrelativelayout.setId(1);
        parentrelativelayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
        parentrelativelayout.setBackgroundColor(Color.BLACK);

        RelativeLayout rl_relativelayout1 = new RelativeLayout(mContext);
        rl_relativelayout1.setId(2);
        rl_relativelayout1.setLayoutParams(new LayoutParams(800,LayoutParams.WRAP_CONTENT));
        rl_relativelayout1.setBackgroundResource(R.drawable.voucher_row);

        RelativeLayout rl_relativelayout2=new RelativeLayout(mContext);
        rl_relativelayout2.setId(3);
        rl_relativelayout2.setBackgroundResource(R.drawable.grab_voucher);
        RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);

我需要在android中实现这个PROGRAMMATICALLY。 ] 1

1 个答案:

答案 0 :(得分:1)

是的,这是可以实现的。看看这些。

  1. http://android-er.blogspot.com/2012/05/add-and-remove-view-dynamically.html
  2. http://technotzz.wordpress.com/2011/11/04/android-dynamically-add-rows-to-table-layout/
  3. http://www.dreamincode.net/forums/topic/130521-android-part-iii-dynamic-layouts/
  4. 另一种可能性是在xml上绘制Layout,但使用visibility属性隐藏它。然后,如果满足条件,则显示它。

相关问题