Reputation: 15563
My activity:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/city_detail_activity"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
my layout xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/shopping_movies"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/shopping_name"
android:layout_height="match_parent"
android:layout_width="match_parent">
</TextView>
<TableLayout
android:id="@+id/movies"
android:layout_height="match_parent"
android:layout_width="match_parent">
</TableLayout>
</LinearLayout>
What I want to do is create multiple instances of my layout and add it one by one inside my activity.
My activity code is like that:
for (int i =0; i < shoppings.size(); i++)
{
LinearLayout shopping_movies = (LinearLayout)LayoutInflater.from(CityActivity.this).inflate(R.layout.shopping_movies, null);
TextView shopping_name = (TextView) shopping_movies.findViewById(R.id.shopping_name);
shopping_name.setText(shopp.name);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayoutt.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
addContentView(shopping_movies, params);
}
The problem is that each layout is added at position 0. One in front other and not below. I just tried addRule(LinearLayout.BELOW, lastId)
but does not work. What can I do?
Upvotes: 0
Views: 256
Reputation: 15563
I solved my problem adding a empty LinearLayout in my activity.
MyActivity:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/city_detail_activity"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout android:id="@+id/shoppings"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
</LinearLayout>
and my java code:
for (int i =0; i < shoppings.size(); i++)
{
LinearLayout shopping_movies = (LinearLayout)LayoutInflater.from(CityDetailActivity.this).inflate(R.layout.shopping_movies, null);
((TextView) shopping_movies.findViewById(R.id.shopping_name)).setText(shopp.name);
LinearLayout shoppingsViewGroup = (LinearLayout)findViewById(R.id.shoppings);
shoppingsViewGroup.addView(shopping_movies);
}
Upvotes: 0
Reputation: 40193
Use LinearLayout
with android:orientation="vertical"
instead of RelativeLayout
as the container for your views, as orientation
doesn't have any effect in RelativeLayout
.
Upvotes: 1