Android: Adding static header to the top of a ListActivity

前端 未结 5 819
别那么骄傲
别那么骄傲 2020-11-28 23:07

Currently I have a class that is extending the ListActivity class. I need to be able to add a few static buttons above the list that are always visible. I\'ve attempted to

5条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-28 23:38

    findViewById() only works to find subviews of the object View. It will not work on a layout id.

    You'll have to use layout inflater to convert the xml to it's corresponding View components. Something like this:

    ListView lv = getListView();
    LayoutInflater inflater = getLayoutInflater();
    View header = inflater.inflate(R.layout.header, lv, false);
    lv.addHeaderView(header, null, false);
    

    I'm not sure why your code wasn't just throwing an error. findViewById() was probably just returning null, and so no header was added to your list.

提交回复
热议问题