2012-05-09 36 views
0

我有一个列表视图。如何动态创建视图组?

我想添加页脚。

所以我创建了一个页脚XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="100px" 
android:background="@drawable/background_news_list" 
android:gravity="center" > 

<ImageButton 
    android:id="@+id/btn_more" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="10px" 
    android:background="@drawable/btn_more_news" /> 

</LinearLayout> 

我想抬高整个布局为主要Java,但我设法抬高按钮仅

footer = getLayoutInflater().inflate(R.layout.testing, null, false); 
btnmore = (ImageButton)footer.findViewById(R.id.btn_more); 

ListView lv = (ListView) findViewById(android.R.id.list); 
lv.addFooterView(footer); 

我的名单是在这个XML

<LinearLayout 
    android:id="@+id/layout_content" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_above="@id/layout_menu" 
    android:layout_below="@id/layout_title" 
    android:orientation="vertical" > 

    <ListView 
     android:id="@+android:id/list" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" > 
    </ListView> 
</LinearLayout> 

我如何创建一个视图组来实现并成为这样?

footer = getLayoutInflater().inflate(R.layout.testing, parent, false); 

因此,我可以使用我自己的线性布局。

回答

0

您的文章是有点混乱,但我认为你只需要一个ID添加到您的LinearLayout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@id/linlayoutid" 
    android:layout_width="fill_parent" 
    android:layout_height="100px" 
    android:background="@drawable/background_news_list" 
    android:gravity="center" > 

<ImageButton 
    android:id="@+id/btn_more" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="10px" 
    android:background="@drawable/btn_more_news" /> 

</LinearLayout> 

然后抓住线性布局:

footer = getLayoutInflater().inflate(R.layout.testing, null, false); 
LinearLayout ll = footer.findViewById(R.id.linlayoutid); 

ListView lv = (ListView) findViewById(android.R.id.list); 
lv.addFooterView(ll); 
+0

一样,不起作用 –

+0

footerview没有使用页脚的线性布局xml –

0

我解决它通过这样做

这是因为

lv is the list id and it