[英]Android - Multiple LinearLayouts positioning?
I am trying to create multiple linear layouts inside main LinearLayout. 我正在尝试在主LinearLayout中创建多个线性布局。 But i am having some trouble with margin. 但是我在保证金方面有些麻烦。 Here is my code and output: - 这是我的代码和输出:-
public class CustomActivity extends Activity {
private LinearLayout mainLayout;
Float value;
int dpi;
private String[] data = new String[] {"Hello World", "Hello World", "Hello World", "Hello World", ""};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom);
mainLayout = (LinearLayout)findViewById(R.id.mainLayout);
mainLayout.setOrientation(LinearLayout.VERTICAL);
value = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10, getResources().getDisplayMetrics());
dpi = Math.round(value);
addLayouts();
}
private void addLayouts() {
int marginBottom = 0;
int count = 0;
for (int i = 0; i < data.length; i++) {
OutLinedLL linearLayout = new OutLinedLL(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(dpi*22,dpi*35);
if(i==0)
{
linearLayout.setBackgroundColor(Color.RED);
params.setMargins(dpi*7, dpi, dpi*4, marginBottom);
linearLayout.setLayoutParams(params);
mainLayout.addView(linearLayout);
}
else
{
count += dpi*2;
Log.e("Count", count+"");
marginBottom -= (dpi*28)+count;
Log.e("Margin Bottom", marginBottom+"");
linearLayout.setBackgroundColor(Color.GREEN);
params.setMargins(dpi*7, dpi, dpi*4, marginBottom);
linearLayout.setLayoutParams(params);
mainLayout.addView(linearLayout);
}
marginBottom = 0;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_custom, menu);
return true;
}
}
Stack Trace: - 堆栈跟踪: -
09-29 11:49:23.844: E/Count(16149): 40
09-29 11:49:23.844: E/Margin Bottom(16149): -600
09-29 11:49:23.852: E/Count(16149): 80
09-29 11:49:23.852: E/Margin Bottom(16149): -640
09-29 11:49:23.852: E/Count(16149): 120
09-29 11:49:23.852: E/Margin Bottom(16149): -680
09-29 11:49:23.852: E/Count(16149): 160
09-29 11:49:23.852: E/Margin Bottom(16149): -720
Output :- 输出:-
As you can see margin bottom isn't same for all the layouts. 如您所见,并非所有布局的边距底部都相同。 I am unable to find out why its happening. 我无法找到原因。 I am trying to learn custom views at the moment so any help would be really appreciated. 我目前正在尝试学习自定义视图,因此我们将不胜感激。
Thanks. 谢谢。
I am not 100% sure if I understand what you want you final layout to look like. 我不确定100%是否了解您想要的最终布局。
However, it sounds like a relative layout with the linear layouts would achieve the desired look and function for your purpose while being "lighter". 但是,听起来像是线性布局的相对布局可以在“更轻”的情况下实现所需的外观和功能。
If I misunderstood you goal, I am sorry, but in that case it might help it you could be clearer in the question. 如果我误解了您的目标,很抱歉,但是那样的话可能会有所帮助,您可以在问题中更清楚地了解。
I think this bit is your problem: 我认为这是您的问题:
marginBottom -= (dpi*28)+count;
You are changing the amount of margin on each loop through. 您正在更改每个循环的边距量。 Just set it to a static value and all your views should have the same margin. 只需将其设置为静态值,所有视图的边距应相同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.