I want to create a method that can dynamically display LinearLayout
in a LinearLayout
moreover that is located in a specified area in my Android application, the number of this linear layout changes depending on a given number.
Also, in each LinearLayout
I want to display a Button
& below it a TextView
.
Here is the method I have already created:
public void putLinearLayout(double number){ int mButtonHeight = 100; int mButtonWidth = 80; LinearLayout Linear = (LinearLayout)findViewById(R.id.linearlayout1); for(int i=1;i<=number;i++) { LinearLayout L = new LinearLayout(this); Button b= new Button(this); TextView tv = new TextView(this); L.setOrientation(LinearLayout.HORIZONTAL); b.setWidth(mButtonWidth); b.setHeight(mButtonHeight); L.addView(b); L.addView(tv); Linear.addView(L); } }
you just need to set the layoutparams
public void putLinearLayout(double number){ LinearLayout Linear = (LinearLayout)findViewById(R.id.linearlayout1); int mButtonHeight = 100; int mButtonWidth = 80; for(int i=1;i<=number;i++) { LinearLayout L = new LinearLayout(this); L.setBackgroundColor(Color.WHITE); LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); //<--this line , is what you were missing L.setLayoutParams(params); //<-- & then this Button b= new Button(this); b.setText(i+""); TextView tv = new TextView(this); tv.setText("i am textview number: "+i); L.setOrientation(LinearLayout.HORIZONTAL); b.setWidth(mButtonWidth); b.setHeight(mButtonHeight); L.addView(b); L.addView(tv); Linear .addView(L); }
}
No comments:
Post a Comment