【问题标题】:how to dynamically create view group?如何动态创建视图组?
【发布时间】:2012-05-18 02:54:46
【问题描述】:

我有一个列表视图。

我想添加页脚。

所以我创建了一个页脚 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);

这样我就可以使用自己的线性布局了。

【问题讨论】:

    标签: java android listview footer viewgroup


    【解决方案1】:

    您的帖子有点混乱,但我认为您只需要在 LinearLayout 中添加一个 id:

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

    【讨论】:

    • footerview没有使用footer xml的线性布局
    【解决方案2】:

    我通过这样做解决了它

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

    这是因为

    lv is the list id and it 
    

    【讨论】:

      猜你喜欢
      • 2015-05-02
      • 1970-01-01
      • 1970-01-01
      • 2016-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-24
      相关资源
      最近更新 更多