【问题标题】:How to dynamically add LinearLayout on Android?如何在Android上动态添加LinearLayout?
【发布时间】:2018-09-17 23:26:21
【问题描述】:

我有一个长度为 n 的数组,我现在需要创建 n 个线性布局并在每个上添加不同的东西。 如何动态完成?

【问题讨论】:

    标签: android android-linearlayout


    【解决方案1】:
    LinearLayout lLayout = new LinearLayout(context);
    parentWidget.addView(lLayout);
    

    【讨论】:

    • 然后在某个地方分配它的所有属性(假设您需要除默认值以外的其他属性)。
    • 您也可以为子LinearLayout设置LayoutParams。 lLayout.setLayoutPatams(layoutParams),
    • @ihrupin 如何获取 parentWidget?我在继承 Activity 类的类的 onCreate 方法中,视图是 setContentView(R.layout.someLayout)
    • LineatLayout parentWidget = new LinearLayout(context); setContentView(parentWidget);就是这样:)
    【解决方案2】:

    最简单的方法是在 xml 中创建一个布局并使用

    LayoutInflater.from(context).inflate(R.layout.my_linear_layout);

    您可能还想setId()您添加的视图,以便您以后可以轻松访问它们。

    【讨论】:

      【解决方案3】:

      我使用 RelativeLayout 解决了这个问题,我发现它更容易使用。是的,当然就像上面提到的那些人一样,我使用了setId()。这是我实现的代码:

      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
      
          ScrollView sv = new ScrollView(this);
      
          //Parent RelativeLayout
          parentLayout = new RelativeLayout(this);
          parentLayout.setBackgroundColor(Color.WHITE);
          params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
          parentLayout.setLayoutParams(params);
          sv.addView(parentLayout);
      
          final String[] comList = getCommunication();
          int listLength=0;
          try{
          listLength= comList.length/3;
          }catch(Exception e){System.out.println(e);System.exit(0);}
      
          childLayout= new RelativeLayout[listLength] ;
          TextView[] tvName  = new TextView[listLength];
          TextView[] tvDate  =new TextView[listLength];
          TextView[] tvMsg =new TextView[listLength];
      
          for(int i =0;i<listLength;i++){
              try{
      
              childLayout[i] = new RelativeLayout(this);
              childLayout[i].setPadding(5, 5, 5, 5);
              params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, 75);
              if(i==0){params.addRule(RelativeLayout.BELOW);}
              else{params.addRule(RelativeLayout.BELOW,i);}
              childLayout[i].setId(i+1);
              childLayout[i].setClickable(true);
              childLayout[i].setLayoutParams(params);
              childLayout[i].setOnClickListener(new OnClickListener(){
                  @Override
                  public void onClick(View v) {
      
      
                      //Create the intent
                        Intent i = new Intent("ACTIIVTY");
                         startActivity(i);
                  }       
              });
      
              tvName[i] = new TextView(this);
              params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
              params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
              tvName[i].setLayoutParams(params);
              childLayout[i].addView(tvName[i]);
              if(comList[i*3].length()>24){
                  String name = comList[i*3].substring(0,24)+"...";
                  tvName[i].setText(name);
              }else{
                  tvName[i].setText(comList[i*3]);
              }
              tvName[i].setId(listLength+1+i);
              tvName[i].setTextSize(12);
              tvName[i].setTextColor(Color.BLACK);
      
              tvDate[i] = new TextView(this);
              params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
              params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
              tvDate[i].setLayoutParams(params);
              childLayout[i].addView(tvDate[i]);
              tvDate[i].setTextSize(11);
              tvDate[i].setTextColor(Color.BLUE);
              tvDate[i].setText(comList[i*3+1]);
      
      
              tvMsg[i] = new TextView(this);
              params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
              params.addRule(RelativeLayout.BELOW, listLength+1+i);
              tvMsg[i].setLayoutParams(params);
              childLayout[i].addView(tvMsg[i]);
              tvMsg[i].setTextSize(11);
              tvMsg[i].setTextColor(Color.GRAY);
              if(comList[i*3+2].length()>96){
                  String msg = comList[i*3+2].substring(0,96)+"...";
                  tvMsg[i].setText(msg);
              }else{
                  tvMsg[i].setText(comList[i*3+2]);
              }
      
              parentLayout.addView(childLayout[i]);
      
              }catch(Exception e){System.out.println("Errrorrrrr");}
          }
      
          setContentView(sv);
      }
      

      【讨论】:

        猜你喜欢
        • 2011-05-11
        • 1970-01-01
        • 1970-01-01
        • 2020-07-26
        • 2018-10-11
        • 1970-01-01
        • 2011-08-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多