安卓:添加静态头一个ListActivity顶部静态、ListActivity

2023-09-12 01:03:32 作者:忍不住想向你靠近

目前我有一个扩展ListActivity类的类。我需要能够添加列表总是可见上述几个静态的按钮。我试图从类中抢用getListView()在ListView。然后我用addHeaderView(查看),以一个小的布局添加到屏幕的顶部。

Header.xml

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT>
    <按钮
        机器人:ID =@ + ID / testButton
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT
        机器人:文本=收入
        机器人:TEXTSIZE =15dip
        机器人:layout_weight =1/>
< / LinearLayout中>
 

在我设置适配器我做的:

 的ListView LV = getListView();
lv.addHeaderView(findViewById(R.layout.header));
 
Android studio如何新建activity 安卓新建活动

这会导致什么发生的事情,除了它被填充从我的数据库中的ListView。无按钮出现在它上面。

我想作为添加填充到ListView顶部另一种方法。当我这样做,但它成功地向下移动,如果我添加任何在它上面,它推的ListView了。无论我做什么它好像我不能把上面的ListView的几个按钮,当我使用的ListActivity。

在此先感谢。

synic,我想你的建议previously。我试图再次贪图理智的,和按钮并没有显示。下面是该活动的布局文件和code,我在OnCreate()实现。

//我listactivity我想在头部添加到

 公共类AuditActivity扩展ListActivity {

    预算预算;

    @覆盖
    公共无效的onCreate(包savedInstanceState){
        光标测试;
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.audit);
        ListView的LV = getListView();
        LayoutInflater infalter = getLayoutInflater();
        ViewGroup中头=(ViewGroup中)infalter.inflate(R.layout.header,LV,假);
        lv.addHeaderView(头);
        预算=新的预算(本);
        / *
        尝试 {
            测试= budget.getTransactions();
            showEvents(试验);
        } 最后 {

        }
        * /
// switchTabSpecial();
    }
 

Layout.xml的活动:

 < XML版本=1.0编码=UTF-8&GT?;

< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT机器人:layout_height =FILL_PARENT>
    < ListView的机器人:ID =@机器人:ID /列表机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT/>
    < TextView的机器人:ID =@机器人:ID /空机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT机器人:文本=@字符串/空/>
< / LinearLayout中>
 

解决方案

findViewById()只能找对象视图的子视图。它不会在一个布局ID工作

您将不得不使用布局充气到XML转换成它对应的视图组件。事情是这样的:

 的ListView LV = getListView();
LayoutInflater充气= getLayoutInflater();
查看标题= inflater.inflate(R.layout.header,LV,假);
lv.addHeaderView(头,空,假);
 

我不知道为什么你的code的不只是抛出一个错误。 findViewById()很可能只返回,所以没有头被添加到列表中。

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 grab the ListView using getListView() from within the class. Then I used addHeaderView(View) to add a small layout to the top of the screen.

Header.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <Button 
        android:id="@+id/testButton"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:text="Income" 
        android:textSize="15dip"
        android:layout_weight="1" />
</LinearLayout>

Before I set the adapter I do:

ListView lv = getListView();
lv.addHeaderView(findViewById(R.layout.header));

This results in nothing happening to the ListView except for it being populated from my database. No buttons appear above it.

Another approach I tried as adding padding to the top of the ListView. When I did this it successfully moved down, however, if I added any above it, it pushed the ListView over. No matter what I do it seems as though I cannot put a few buttons above the ListView when I used the ListActivity.

Thanks in advance.

synic, I tried your suggestion previously. I tried it again just for the sake of sanity, and the button did not display. Below is the layout file for the activity and the code I've implemented in the oncreate().

//My listactivity I am trying to add the header to

public class AuditActivity extends ListActivity {

    Budget budget;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        Cursor test;
        super.onCreate(savedInstanceState);
        setContentView(R.layout.audit);
        ListView lv = getListView();
        LayoutInflater infalter = getLayoutInflater();
        ViewGroup header = (ViewGroup) infalter.inflate(R.layout.header, lv, false);
        lv.addHeaderView(header);
        budget = new Budget(this);
        /*
        try {
            test = budget.getTransactions();
            showEvents(test);
        } finally {

        }
        */
//      switchTabSpecial();
    }

Layout.xml for activity:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent">
    <ListView android:id="@android:id/list" android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView android:id="@android:id/empty" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:text="@string/empty" />
</LinearLayout>

解决方案

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.