Ich möchte drei lineare Layouts zu einer Aktivität programmatisch jede der gleichen Breite hinzufügen. das Problem ist, ich bin nicht in der Lage, die Gewichte dieser Layouts programmgesteuert festzulegen. Ich könnte dies innerhalb von XML tun, aber ich möchte dies im Programm tun. hier ist das, was ich will: Android Linear Layout Gewicht programmatisch
24
A
Antwort
38
Hier seine die Lösung
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(0, 100);
lp.weight = 1;
Komplette Lösung
LinearLayout ll1, ll2, ll3;
/* Find these LinearLayout by ID
i.e ll1=(LinearLayout)findViewById(R.id.ll1);
*/
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(0, 100);
lp.weight = 1;
ll1.setLayoutParams(lp);
ll2.setLayoutParams(lp);
ll3.setLayoutParams(lp);
5
Verwenden new LinearLayout.LayoutParams(int width, int height, float weight)
Gewichte zu setzen, wenn das Layout params den Subviews Einstellung
2
Tun Sie dies.
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtNote = (LinedEditText) findViewById(R.id.txtNote);
lnr = (LinearLayout) findViewById(R.id.lnr);
LinearLayout l1 = new LinearLayout(this);
LinearLayout l2 = new LinearLayout(this);
LinearLayout l3 = new LinearLayout(this);
l1.setBackgroundResource(android.R.color.holo_green_light);
l2.setBackgroundResource(android.R.color.holo_orange_dark);
l3.setBackgroundResource(android.R.color.holo_blue_bright);
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.MATCH_PARENT, 1);
lnr.addView(l1, param);
lnr.addView(l2, param);
lnr.addView(l3, param);
}
2
Sie können es tun, indem das Layout Gewicht Eigenschaft für Ihr individuelles lineares Layout einstellen, übergeben sie in Linearlayout - LayoutParams Konstruktor:
LinearLayout.LayoutParams param = new LinearLayout.LayoutParam(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT, 1);
oder
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(
0,
LayoutParams.MATCH_PARENT, 1);
Hoffen, dass es Ihnen helfen kann!
Arbeitete wie Charme, danke für schnelle Antwort. Allerdings musste ich es ein wenig wie folgt modifizieren >> LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams (0, LayoutParams.WRAP_CONTENT); \t \t \t \t lp.gewicht = 1; – Bhaijaan