首页 > 解决方案 > 在循环中以编程方式显示线性布局

问题描述

大家好,我需要帮助。我是 Android 编程方面的大佬,所以自从我试图解决这个问题以来,我花了我所有的时间才知道为什么我希望你帮助我。我正在尝试根据循环条件显示线性布局,但问题是当我这样做时,它只是显示最后一个,而在显示时我看不到其他布局。这是我的代码

ScrollView sv = new ScrollView(this);
    sv.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
    sv.setPadding(5, 5, 5, 5);
    sv.setBackgroundColor(getResources().getColor(R.color.colorWhite));
    LinearLayout ll = null;
    for (int i=0; i < 2; i++){
        try {

            //Here iam defining the LinearLayout
            ll = new LinearLayout(this);
            ll.setId(i);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
            ll.setLayoutParams(params);
            ll.setOrientation(LinearLayout.VERTICAL);
            ll.setBackground(getDrawable(R.drawable.circle_view));

            //Here iam defining the RelativeLayout
            RelativeLayout Rl = new RelativeLayout(this);
            RelativeLayout.LayoutParams param = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
            Rl.setLayoutParams(param);

            //Defining a layout params for widgets
            RelativeLayout.LayoutParams image = new RelativeLayout.LayoutParams(100, 90);
            image.addRule(RelativeLayout.ALIGN_LEFT);
            //Creating widgets
            ImageView im = new ImageView(this);
            im.setImageResource(R.mipmap.airtel);
            im.setId(R.id.image1);
            im.setLayoutParams(image);
            //----------------
            RelativeLayout.LayoutParams txtone = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
            txtone.addRule(RelativeLayout.RIGHT_OF, R.id.image1);
            txtone.setMargins(0, 5, 0, 45);

            TextView txtVone = new TextView(this);
            txtVone.setText("ArkaL"+i);
            //txtVone.setTextSize(R.dimen.MainTextSize);
            txtVone.setTextSize(15);
            txtVone.setTypeface(txtVone.getTypeface(), Typeface.BOLD);
            txtVone.setLayoutParams(txtone);

            //------------------------------------------------
            RelativeLayout.LayoutParams txttwo = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
            txttwo.addRule(RelativeLayout.RIGHT_OF, R.id.image1);
            txttwo.setMargins(0, 45, 0, 0);
            TextView txtVtwo = new TextView(this);
            txtVtwo.setText("République Démocratique du Congo");
            txtVtwo.setTypeface(txtVtwo.getTypeface(), Typeface.SERIF.getStyle());
            txtVtwo.setTextSize(10);
            txtVtwo.setLayoutParams(txttwo);

            Rl.addView(im);
            Rl.addView(txtVone);
            Rl.addView(txtVtwo);

            RelativeLayout.LayoutParams recycler = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
            RecyclerView rv = new RecyclerView(this);
            rv.setLayoutParams(recycler);
            ll.addView(Rl);
            ll.addView(rv);

        }catch (Exception e){

        }

    }
    //Here for assigning Linear to ScrollView
    sv.addView(ll);
    this.setContentView(sv);

请帮我。

标签: androidandroid-layout

解决方案


您的问题是您创建了一个LinearLayout并在循环中对其进行了更改,然后将其添加到ScrollView最后您将只有一个 LinearLayout。正如您可能知道的那样,ScrollView 应该只有一个 View 作为子视图,因此您应该为 ScrollView 添加一个 LinearLayout,并在主 LinearLayout 中添加任意数量的 LinearLayout。你的代码可以是这样的:

LinearLayout llMain = new LinearLayout(this);
LinearLayout.LayoutParams params2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
llMain.setLayoutParams(params2);
llMain.setOrientation(LinearLayout.VERTICAL);
sv.addView(llMain);
LinearLayout ll = null;
for (int i = 0; i < 2; i++) {
 try {

  //Here iam defining the LinearLayout
  ll = new LinearLayout(this);
  ll.setId(i);
  LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
  ll.setLayoutParams(params);
  ll.setOrientation(LinearLayout.VERTICAL);
  ...
  llMain.addView(ll);
 } catch (Exception e) {
  ...

注意主要线性层的高度应该包裹它的内容;


推荐阅读