【发布时间】:2015-06-10 06:55:05
【问题描述】:
这是我的布局:
- 滚动视图
- 线性布局
- 相对布局
- 图像视图
- 文本视图
- 相对布局
- 图像视图
- 文本视图
- 相对布局
- 线性布局
这是我创建布局和视图的函数
public void createUI(int count)
{
ScrollView scrollView = new ScrollView(this);
ScrollView.LayoutParams svp = new ScrollView.LayoutParams(
ScrollView.LayoutParams.WRAP_CONTENT, ScrollView.LayoutParams.WRAP_CONTENT);
scrollView.setLayoutParams(svp);
LinearLayout linearLayout = new LinearLayout(this);
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setLayoutParams(llp);
int height = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 110, getResources().getDisplayMetrics());
int ivSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 90, getResources().getDisplayMetrics());
for (int i = 0; i < 2; ++i)
{
RelativeLayout relativeLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT, height);
relativeLayout.setLayoutParams(rlp);
ImageView imageView = new ImageView(this);
imageView.setId(R.id.txtImage);
imageView.setBackgroundDrawable(getResources().getDrawable(R.drawable.myborder));
RelativeLayout.LayoutParams imagelp = new RelativeLayout.LayoutParams(ivSize, ivSize);
imagelp.addRule(RelativeLayout.ALIGN_PARENT_START);
imageView.setLayoutParams(imagelp);
TextView txtName = new TextView(this);
imageView.setId(R.id.txtName);
txtName.setText("Abu Baka");
RelativeLayout.LayoutParams namelp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
namelp.addRule(RelativeLayout.RIGHT_OF, R.id.txtImage);
txtName.setLayoutParams(namelp);
relativeLayout.addView(imageView);
relativeLayout.addView(txtName);
linearLayout.addView(relativeLayout);
}
scrollView.addView(linearLayout);
setContentView(scrollView);
}
我以编程方式设置了 imageView idimageView.setId(R.id.txtImage); 并将规则设置为 txtNamenamelp.addRule(RelativeLayout.RIGHT_OF, R.id.txtImage);,但它不起作用。
这是我的输出:
Abu Baka这个词假设在imageView的右边,但不是...如何解决这个问题?或者我的代码有什么问题吗?
这是我的ids.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="txtImage" type="id"/>
<item name="txtName" type="id"/>
<item name="txtSince" type="id"/>
<item name="txtComment" type="id"/>
</resources>
【问题讨论】:
标签: android