mhplur mhplur - 5 months ago 57
Android Question

Weight inside Scrollview

I'm having trouble calculating the

weight
inside of an scroll view on my android app. I want the app's content to resize proportionally to the screen size. How can I calculate the
weight
inside a scroll view?

Here some of my code:

LinearLayout layout_principal = (LinearLayout) findViewById(R.id.layout_items);// is horizontal layout and layout of Scrollview

LinearLayout layout_titulo_opcion = new LinearLayout(this);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
0,
1f);
layout_titulo_opcion.setLayoutParams(layoutParams);

LinearLayout layout_titulo_opcion2 = new LinearLayout(this);
LinearLayout.LayoutParams layoutParams2 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
0,
1f);
layout_titulo_opcion2.setLayoutParams(layoutParams2);

layout_principal.addView(layout_titulo_opcion);
layout_principal.addView(layout_titulo_opcion2);

Jim Jim
Answer

To get/set the weight, use this:

LinearLayout.LayoutParams mylp = (LinearLayout.LayoutParams)myLayout.getLayoutParams();
int myWeight = mylp.weight; // get weight
mylp.weight = 4; // set weight

To set the weight sum, use this:

myLayout.setWeightSum(10);