以编程方式循环显示线性布局

时间:2018-10-23 16:12:25

标签: android android-layout

大家好,我需要帮助。我是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);

请帮助我。

1 个答案:

答案 0 :(得分:0)

您的问题是您创建了一个df.groupby(pd.Grouper(key='Date', freq='A')).plot(x="Date",y="Value") # Group by year and plot date in x-axes 并在循环中对其进行了更改,然后将其添加到df["year"]=df.Date.dt.year df["month"]=df.Date.dt.month df.groupby('year',"month")["Value"].sum().plot(x='month', y='Value') 中,因此最后您将只有一个LinearLayout。您可能知道ScrollView应该只有一个View作为子视图,因此您应该为ScrollView添加LinearLayout并向主LinearLayout添加任意数量的LinearLayout。您的代码可以像这样:

LinearLayout

请注意,主要linearlayour的高度应包装其内容;