У меня есть 2 файла макета XML: «lights.xml »и «lights_cell.xml».
Вот упрощенная версия каждого из них.Я удалил ширину / высоту / и т. Д. И просто сохранил важные атрибуты ...
lights.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>
lights_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>
Идея состоит в том, что я хочу добавить несколько "lights_cell "к" подсветкам "через цикл.
Я собрал некоторый тестовый код следующим образом, но, какон не работает, я подозреваю, что неправильно добавляю макеты ячеек, или, возможно, мне не следует использовать «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);
}
}
Это правильный способ динамического добавления макетов вродительский макет?Спасибо!