将多个视图添加到一个低于其他的活动

时间:2021-01-13 20:57:06

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:

<?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?

问题是每个布局都添加在位置0。一个在前面,而不是在下面。我刚试过addRule(LinearLayout.BELOW,lastId)但是没有用。我能做什么?

2 个解决方案

#1


Use LinearLayout with android:orientation="vertical" instead of RelativeLayout as the container for your views, as orientation doesn't have any effect in RelativeLayout.

使用LinearLayout和android:orientation =“vertical”而不是RelativeLayout作为视图的容器,因为orientation在RelativeLayout中没有任何效果。

#2


I solved my problem adding a empty LinearLayout in my activity.

我解决了在我的活动中添加空LinearLayout的问题。

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:

和我的java代码:

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);
}

#1


Use LinearLayout with android:orientation="vertical" instead of RelativeLayout as the container for your views, as orientation doesn't have any effect in RelativeLayout.

使用LinearLayout和android:orientation =“vertical”而不是RelativeLayout作为视图的容器,因为orientation在RelativeLayout中没有任何效果。

#2


I solved my problem adding a empty LinearLayout in my activity.

我解决了在我的活动中添加空LinearLayout的问题。

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:

和我的java代码:

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);
}