In an horizontal LinearLayout, I want to spread evenly a lot of textviews (I'd says my max is 120 for now).
For now, I do it by setting the weightsum to 120, then adding 120 textviews with a wheight of 1f.
Here is my simplified code for you to test :
LinearLayout layout = new LinearLayout(context);
layout.setWeightSum(120);
for (int i = 0; i < 120; i++) {
TextView tv = new TextView(context);
tv.setLayoutParams(new LinearLayout.LayoutParams(0, 100, 1f));
tv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("Dan", "onClick: getWidth="+v.getWidth());
}
});
layout.addView(tv);
}
On my Genymotion Nexus 4 emulator, I get 2 situations :
- on portrait, 68 first TV are 6px wide, whereas 52 others ares 7px wide
- on landscape, 4 first TV are 9px wide, whereas 116 others ares 10px wide
The final use of all this is that some TV sometimes weight more. Please note that except for this little bug, I get the very exact graphic result I want :-)
Questions :
- Why do do my TV are not the same width (I'd accept 1 or 2 for completing the total width) ?
- How can I resolve this bug ?
- Is LinearLayout the optimal layout for me to do what I want ?
Aucun commentaire:
Enregistrer un commentaire