[英]position views programmatically in Android
I have a method that creates 3 textViews in a vertical LinearLayout: 我有一个在垂直LinearLayout中创建3个textViews的方法:
public LinearLayout CreateLayout() {
LinearLayout aLap = new LinearLayout(this);
LinearLayout.LayoutParams TextParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
TextView txtName = new TextView(this);
txtName.setText(result1);
txtName.setLayoutParams(TextParams);
txtName.setGravity(Gravity.CENTER);
txtName.setPadding(20, 8, 20, 4);
txtName.setTextSize(20);
txtName.setTextColor(Color.parseColor("#000000"));
TextView txtTime = new TextView(this);
txtTime.setText(result2);
txtTime.setLayoutParams(TextParams);
txtTime.setGravity(Gravity.CENTER);
txtTime.setTextSize(20);
txtTime.setPadding(20, 4, 10, 4);
txtTime.setTextColor(Color.parseColor("#000000"));
android.view.ViewGroup.LayoutParams Params = new ViewGroup.LayoutParams(
android.view.ViewGroup.LayoutParams.MATCH_PARENT, 150);
TextView rep = new TextView(this);
String Rep = sharedPrefs.getString("R_PREFS", "2");
rep.setLayoutParams(TextParams);
rep.setGravity(Gravity.CENTER);
rep.setTextSize(20);
rep.setPadding(20, 4, 10, 4);
rep.setTextColor(Color.parseColor("#000000"));
rep.setText("Test: " + Rep);
aLap.setLayoutParams(Params);
aLap.setBackgroundResource(R.drawable.bg);
aLap.setOrientation(LinearLayout.VERTICAL);
aLap.setPadding(3, 3, 3, 3);
aLap.addView(txtName);
aLap.addView(txtTime);
aLap.addView(rep);
return aLap;
}
I use a vertical layout since I want the textViews to be placed under each other. 我使用垂直布局,因为我希望textViews相互放置。 Now I want an ImageView to be placed to the right of this textViews and centered vertically. 现在,我希望将ImageView放置在此textViews的右侧并垂直居中。 Is that possible? 那可能吗?
You can not achieve desired output with a single LinearLayout
. 您无法通过单个 LinearLayout
获得所需的输出。 I would suggest you use RelativeLayout
. 我建议您使用RelativeLayout
。 It's more flexible. 更灵活。 You can add more child-views to RelativeLayout
at desired positions by adding rules to the views. 您可以在规则位置添加更多子视图到RelativeLayout
的所需位置。 Check this post on how to add rules when working with Relative Layout
at runtime: How to lay out Views in RelativeLayout programmatically? 查看这篇文章,了解如何在运行时使用Relative Layout
时添加规则: 如何以编程方式在RelativeLayout中布局视图? . 。
<LinearLayout orientation="horizontal">
<LinearLayout orientation="vertical">
<TextView view1/>
<TextView view1/>
<TextView view1/>
</LinearLayout>
<ImageView height=fill_parent scaleType="center"/>
</LinearLayout>
Is more or less what you want I think 我想的是或多或少
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.