简体   繁体   中英

Inflating a LinearLayout dynamically - Android

I'm trying to inflate a list of views in a LinarLayout directly in the onCreate() method from the activity. However, I'm having a problem somewhere in the code but I really can't see it, I'm turning around this problem for two days, I'm going crazy !

The problem is that when I run the activity, the linear layout is completely empty, there is only the title.

在此处输入图片说明

Here is the important part of the code, KeyItems activity :

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.keyitems_layout);
    initializer();
    ll = (LinearLayout) findViewById(R.id.ll);
    LayoutInflater inflater = LayoutInflater.from(this);
    for (int i = 0; i < list.size(); i++) {
        View view  = inflater.inflate(R.layout.keyitem_temp, ll, false); 
        final TextView ind = (TextView) view.findViewById(R.id.ind);
        final TextView item = (TextView) view.findViewById(R.id.item);
        ind.setText(list.get(i).get("id"));
        item.setText(list.get(i).get("item"));
        ll.addView(view);
    }
}

Here is the keyitems_layout :

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@color/black" >

    <LinearLayout android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="@color/black">

        <TextView android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="8dp" />

        <TextView android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:text="@string/key_items"
            android:textSize="36sp"
            android:textColor="@color/white" />

        <TextView android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="4dp" />

        <LinearLayout android:id="@+id/ll"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginLeft="32dp"
            android:layout_marginRight="32dp"
            android:orientation="vertical"
            android:background="@color/white" >

        </LinearLayout>

        <TextView android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="@color/white"
            android:text="caca"
            android:padding="32dp" />

        </LinearLayout>

</ScrollView>

And finally, my keyitem_temp.xml which I'm inflating :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/black"
    android:padding="4dp"
    android:orientation="horizontal" >

    <TextView android:id="@+id/ind"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:gravity="center"
        android:textSize="18sp"
        android:textColor="@color/blue" />

    <TextView android:id="@+id/item"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:gravity="center"
        android:textSize="18sp"
        android:textColor="@color/white"/>

</LinearLayout>

I have an other activity called ItemsActivity which is the same code with some little differences and it works. Any suggestion? Thanks in advance.

Try using wrap_content for all of your heights except the initial ScrollView:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@color/black" >

    <LinearLayout android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:background="@color/black">

        <TextView android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="8dp" />

        <TextView android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:text="@string/key_items"
            android:textSize="36sp"
            android:textColor="@color/white" />

        <TextView android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="4dp" />

        <LinearLayout android:id="@+id/ll"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="32dp"
            android:layout_marginRight="32dp"
            android:orientation="vertical"
            android:background="@color/white" >

        </LinearLayout>

        <TextView android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="@color/white"
            android:text="caca"
            android:padding="32dp" />

        </LinearLayout>

</ScrollView>

and:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/black"
    android:padding="4dp"
    android:orientation="horizontal" >

    <TextView android:id="@+id/ind"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:textSize="18sp"
        android:textColor="@color/blue" />

    <TextView android:id="@+id/item"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:textSize="18sp"
        android:textColor="@color/white"/>

</LinearLayout>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM