【发布时间】:2013-09-14 11:43:10
【问题描述】:
我有一个 main.xml 布局和其他布局 other.xml 包含几个子元素 (TextView, ImageView)。我想要做的是动态添加到main.xml 作为可滚动列表other.xml 元素。实际上确实做到了这一点,我使用循环如下:
LayoutInflater layoutInflaterInstance = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view;
View insertPoint = findViewById(R.id.linearLayoutToKeepOtherLayouts);
for (int i = 0; i < numberOfOtherXmlLayouts; i++) {
view = layoutInflaterInstance.inflate(R.layout.otherXml, null);
TextView firstValueOfOtherXml = (TextView) view.findViewById(R.id.first);
TextView secondValueOfOtherXml = (TextView) view.findViewById(R.id.second);
ImageView thirdValueOfOtherXml = (ImageView) view.findViewById(R.id.third);
firstValueOfOtherXml = setContent();
secondValueOfOtherXml = setContent();
thirdValueOfOtherXml = setContent();
((ViewGroup) insertPoint).addView(view, i, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
}
这就是我向现有 main.xml 添加动态内容的方式。现在我想向 firstValueOfOtherXml/secondValueOfOtherXml/thirdValueOfOtherXml 添加一些活动,我尝试在 TextView/ImageView 中的 other.xml 中设置 onClick 事件,如下所示(在上部 for 循环中):
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.otherXmlID);
View.OnClickListener clickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
setContentView(R.layout.otherXml);
Intent myIntent = new Intent(getApplicationContext(), OtherActivity.class);
startActivityForResult(myIntent, 0);
}
};
relativeLayout.setOnClickListener(clickListener);
不幸的是,它仅适用于添加到 main.xml 的第一个 other.xml 布局。我认为问题出在添加的 other.xml 的 ID 中,我没有动态更改它(可能根据 for 循环中的 i)。我将不胜感激添加动态布局元素的任何解决方案,这些元素具有动态更改的子元素 ID,以使其识别成为可能。
希望它足够清楚。 感谢您的帮助。
【问题讨论】:
标签: java android android-layout android-intent android-listview