【发布时间】:2011-12-06 18:23:16
【问题描述】:
我有 2 个布局 xml 文件:“highlights.xml”和“highlights_cell.xml”。
这里是每个的简化版本。我已经删除了宽度/高度/等,只保留了重要的属性...
highlights.xml
<LinearLayout>
<uk.co.jasonfry.android.tools.ui.SwipeView android:id="@+id/swipe_view" />
<uk.co.jasonfry.android.tools.ui.PageControl android:id="@+id/page_control" />
</LinearLayout>
highlights_cell.xml
<LinearLayout android:orientation="vertical">
<LinearLayout android:id="@+id/linear_layout1" android:orientation="horizontal">
<ImageView android:id="@+id/logo" />
<LinearLayout android:id="@+id/linear_layout2" android:orientation="vertical">
<TextView android:id="@+id/title" />
<TextView android:id="@+id/subtitle" />
</LinearLayout>
</LinearLayout>
<ScrollView android:id="@+id/scroll_view">
<TextView android:id="@+id/description" />
</ScrollView>
</LinearLayout>
想法是我想通过一个循环将几个“highlights_cell”添加到“highlights”中。
我整理了一些测试代码如下,但由于它不起作用,我怀疑我没有正确添加单元格布局,或者我不应该使用“inflater”...
/** Declare shared variables */
SwipeView mSwipeView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState){
//Initialise layout and variables
super.onCreate(savedInstanceState);
setContentView(R.layout.highlights);
//Setup controls
mSwipeView = (SwipeView) findViewById(R.id.swipe_view);
LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
//Loop through collection and add views
for(int i=0; i<7;i++)
{
//Create the itemView to use layout xml for each cell
View itemView = inflater.inflate(R.layout.highlights_cell, null);
//Set values within cell
TextView title = (TextView) itemView.findViewById(R.id.title);
title.setText("HELLO WORLD_" + i);
//add the itemView to main view
mSwipeView.addView(itemView);
}
}
这是将布局动态添加到父布局的正确方法吗? 谢谢!
【问题讨论】:
-
乍一看,这段代码看起来不错。您应该添加所有原始属性(宽度/高度/等),因为这些可能是问题的根源。
-
我用的是ViewPager,你也可以看看。它具有对大型数据集有用的适配器支持。
-
感谢 cmets .. 我查看了 ViewPager,但找不到与 PageControl 等效的示例(视图下的点跟踪您的位置)