【问题标题】:How to add views dynamically to a RelativeLayout already declared in the xml layout?如何将视图动态添加到已在 xml 布局中声明的 RelativeLayout?
【发布时间】:2012-05-26 19:38:25
【问题描述】:

我在 xml 布局文件中声明了 RelativeLayout。现在我想将代码中的Views 添加到现有布局中。我通过代码向这个现有布局动态添加了Button,如下所示:

rLayout = (RelativeLayout)findViewById(R.id.rlayout); 
        LayoutParams lprams = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
        Button tv1 = new Button(this); 
        tv1.setText("Hello"); 
        tv1.setLayoutParams(lprams); 
        tv1.setId(1); 
        rLayout.addView(tv1); 

现在我需要在已添加的Button 的右侧添加另一个Button。我找不到将新按钮添加到之前添加的按钮右侧的方法。

【问题讨论】:

    标签: android android-layout android-widget


    【解决方案1】:

    为第二个添加的Button 添加规则RelativeLayout.RIGHT_OF LayoutParams

        // first Button
        RelativeLayout rLayout = (RelativeLayout) findViewById(R.id.rlayout);
        RelativeLayout.LayoutParams lprams = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        Button tv1 = new Button(this);
        tv1.setText("Hello");
        tv1.setLayoutParams(lprams);
        tv1.setId(1);
        rLayout.addView(tv1);
    
        // second Button
        RelativeLayout.LayoutParams newParams = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        Button tv2 = new Button(this);
        tv1.setText("Hello2");
        newParams.addRule(RelativeLayout.RIGHT_OF, 1);
        tv2.setLayoutParams(newParams);
        tv2.setId(2);
        rLayout.addView(tv2);
    

    【讨论】:

    • 如果我有很多动态按钮并且倒数第二个触及边缘会怎样?最后一个怎么办??
    • @zhelon 它可能会被推到外面或被挤压。如果您知道您将有很多按钮,那么您需要将RelativeLayout 包装在ScrollViewHorizontalScrollView 中。
    • 如果我在动态创建按钮时使用RelativeLayout,我怎么知道他在什么时候触摸了边缘?
    • @zhelon 使用 Relativelayout 这样的布局会很复杂,如果这对您很重要,您可能希望实现自定义视图。
    【解决方案2】:

    也许这对你有帮助,试试吧。

    rLayout = (RelativeLayout)findViewById(R.id.rlayout);
    LayoutParams lprams = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
    
    TableLayout tl=new TableLayout(this);
    rLayout.addView(tl); 
    
    TableRow tr1=new TableRow(this);
    tl.addView(tr1);
    
    Button btn1 = new Button(this);
    btn1.setText("Hello");
    btn1.setLayoutParams(lprams);
    btn1.setId(1);
    tr1.addView(btn1);
    
    TextView tv1 = new TextView(this); 
    tv1.setWidth(40);
    tv1.setHeight(LayoutParams.WRAP_CONTENT);
    tr1.addView(tv1);
    
    
    Button btn2 = new Button(this);
    btn2.setText("World");
    btn2.setLayoutParams(lprams);
    btn2.setId(2);
    tr1.addView(btn2);
    

    【讨论】:

      【解决方案3】:

      创建另一个按钮:

      Button tv2 = new Button(this);
      tv2.setText("World");
      tv2.setLayoutParams(lprams);
      tv2.setId(2);
      

      将其添加到您的 RelativeLayout:

      rLayout.addView(tv2); 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-06-05
        • 2012-04-17
        • 1970-01-01
        • 2014-01-25
        • 1970-01-01
        相关资源
        最近更新 更多