我正在尝试与每个Tabelrow的2个TextViews和1个ImageView进行聊天。我正在使用relativeLayout来安排它们,但是当我运行应用程序时,所有内容都放在右上角的顶部...我谷歌搜索我的方式通过互联网,但我找不到任何帮助我。有人可以帮忙吗?也许给我一些指示??
TableRow trr = new TableRow(this);
trr.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT, 1.0f));
tl.addView(trr);
RelativeLayout rl = new RelativeLayout(this);
TableRow.LayoutParams trrllp = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT, 10);
trrllp.gravity= Gravity.CENTER;
rl.setLayoutParams(trrllp);
trr.addView(rl);
ImageView ivSvart = new ImageView(this);
TextView tvchat = new TextView(this);
tvchat.setText(ChatH.get(i)[2]);
String s = ChatH.get(i)[1];
Long l = Long.valueOf(s);
time = new Date(l*1000);
TextView tvTimeDate = new TextView(this);
tvTimeDate.setText(time + ", " + navn);
if(b_ID.equals(MinID)){
RelativeLayout.LayoutParams rltvlp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT-70, RelativeLayout.LayoutParams.WRAP_CONTENT);
rltvlp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
rltvlp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
rltvlp.setMargins(0, 5, 100, 0);
tvchat.setLayoutParams(rltvlp);
tvchat.setTextColor(0xFFFEFEFE);
tvchat.setTextSize(18);
rl.addView(tvchat);
RelativeLayout.LayoutParams rlivlp = new RelativeLayout.LayoutParams(80,80);
rlivlp.addRule(RelativeLayout.ABOVE, tvTimeDate.getId());
rlivlp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
rlivlp.setMargins(0, 0, 10, 10);
ivSvart.setLayoutParams(rlivlp);
rl.addView(ivSvart);
RelativeLayout.LayoutParams rltvlp2 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
rltvlp2.setMargins(0, 5, 15, 0);
rltvlp2.addRule(RelativeLayout.BELOW, tvchat.getId());
rltvlp2.addRule(RelativeLayout.LEFT_OF, ivSvart.getId());
tvTimeDate.setLayoutParams(rltvlp2);
rl.addView(tvTimeDate);
}
我尝试过多种方式,但还没有成功。
答案 0 :(得分:1)
确保您的父布局是相对布局,您在其中是程序化的联盟创建子相对布局。这将有效。