动态地扩展LinearLayout - Android

时间:2015-01-19 19:23:24

标签: java android xml android-layout layout-inflater

我试图直接在活动的LinarLayout方法中对onCreate()中的观看列表进行充气。但是,我在代码的某个地方遇到了问题,但我真的无法看到它,我已经把这个问题扭转了两天,我疯了!

问题是当我运行活动时,线性布局完全为空,只有标题。

enter image description here

以下是代码KeyItems活动的重要部分:

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

以下是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>

最后,我keyitem_temp.xml我正在膨胀:

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

我有一个名为ItemsActivity的其他活动,它是相同的代码,但有一点点差异并且有效。有什么建议吗?提前谢谢。

1 个答案:

答案 0 :(得分:0)

尝试将wrap_content用于除初始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>

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