【问题标题】:Android: Adding static header to the top of a ListActivityAndroid:将静态标题添加到 ListActivity 的顶部
【发布时间】:2010-04-12 06:13:19
【问题描述】:

目前我有一个扩展 ListActivity 类的类。我需要能够在列表上方添加一些始终可见的静态按钮。我试图从类中使用 getListView() 获取 ListView。然后我使用 addHeaderView(View) 在屏幕顶部添加了一个小布局。

头文件.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>

在设置适配器之前,我会:

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

这不会导致 ListView 发生任何事情,只是它是从我的数据库中填充的。上面没有任何按钮。

我尝试的另一种方法是将填充添加到 ListView 的顶部。当我这样做时,它成功地向下移动,但是,如果我在它上面添加任何内容,它会将 ListView 推过来。无论我做什么,当我使用 ListActivity 时,似乎都无法在 ListView 上方放置几个按钮。

提前致谢。

synic,我之前尝试过你的建议。为了理智,我又试了一次,但按钮没有显示。下面是活动的布局文件和我在 oncreate() 中实现的代码。

//我的listactivity我正在尝试将标题添加到

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:

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

【问题讨论】:

    标签: android header listactivity


    【解决方案1】:

    findViewById() 仅用于查找对象视图的子视图。它不适用于布局 ID。

    您必须使用布局充气器将 xml 转换为其对应的视图组件。像这样的:

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

    我不确定为什么您的代码不只是抛出错误。 findViewById() 可能只是返回了null,因此您的列表中没有添加任何标题。

    【讨论】:

    • 我认为您不需要将膨胀的标头分配和转换为 ViewGroup。 ViewGroup 扩展了 View,ListView 的 addHeaderView 方法接受任何 View 对象。
    • @Glenn Bech iam 同意你的说法
    • 完美,+1!只是一个额外的评论:确保在添加标题视图之后设置列表适配器。在标题之前添加适配器给了我一个运行时异常“无法将标题视图添加到列表 - setAdapter 已被调用”
    • @GlennBech 你是对的。我已经更新了解决方案以改进该部分。
    • 现在如何在列表滚动时修复标题的位置?
    【解决方案2】:

    这是最简单的解决方案:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:orientation="vertical"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:background="@color/background">
     <include layout="@layout/actionbar"/>
       <ListView
        android:id="@+id/tasklist_TaskListView"
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:textColor="@color/baseFont"/>
       <include layout="@layout/bottombar"/>
    </LinearLayout>
    

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:orientation="vertical"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:background="@color/background">
       <Button 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
       <ListView
        android:id="@+id/tasklist_TaskListView"
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:textColor="@color/baseFont"/>
       <Button 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>
    
    </LinearLayout>
    

    你可以添加另一个水平线性布局来代替按钮

    【讨论】:

    • 代码没有什么特别之处。 actionbar 和 bottombar 只是 actionbar.xml 和 bottombar.xml 中定义的另一种 xml 布局。
    【解决方案3】:

    经过一些研究,我发现在我的 ListActivity XML 文档中混合 TableLayout 和 LinearLayout 后,我​​可以在文档中添加标题。下面是我的 XML 文档,如果有人感兴趣的话。虽然 synic 的方法在使用他的解决方案之后可能是正确的方法,但我无法让它按照我想要的方式运行。

    AuditTab.java

    public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.audittab);
            getListView().setEmptyView(findViewById(R.id.empty));
    }
    

    audittab.xml

    <?xml version="1.0" encoding="utf-8"?>
    <TableLayout 
        android:layout_width="fill_parent"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_height="fill_parent">
        <TableRow 
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" 
            android:layout_gravity="center_horizontal">
            <LinearLayout 
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="fill_parent" 
                android:layout_height="fill_parent"
                android:orientation="horizontal" 
                android:layout_weight="1">
                <Button 
                    android:id="@+id/btnFromDate" 
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content" 
                    android:text=""
                    android:layout_weight="1" />
                <Button 
                    android:id="@+id/btnToDate" 
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content" 
                    android:text=""
                    android:layout_toRightOf="@+id/btnFromDate"
                    android:layout_weight="1" />
                <Button 
                    android:id="@+id/btnQuery" 
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content" 
                    android:text="Query"
                    android:layout_toRightOf="@+id/btnToDate"
                    android:layout_weight="1" />
    
            </LinearLayout>
        </TableRow>
        <TableRow 
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" 
            android:layout_gravity="center_horizontal">
            <LinearLayout 
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">
                <ListView 
                    android:id="@android:id/list"
                    android:layout_width="300dip" 
                    android:layout_height="330dip"
                    android:scrollbars="none" />
                <TextView 
                    android:id="@+id/empty"
                    android:layout_width="wrap_content" 
                    android:layout_height="wrap_content"
                    android:paddingTop="10dip"
                    android:text="- Please select a date range and press query." />
            </LinearLayout>
        </TableRow>
    </TableLayout>
    

    AuditItem.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent" 
            android:orientation="horizontal" 
            android:padding="10sp">
        <TextView 
            android:id="@+id/transactionDateLabel" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:text="Date: " />
        <TextView 
            android:id="@+id/transactionDate" 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            android:layout_toRightOf="@id/transactionDateLabel" />
        <TextView 
            android:id="@+id/transactionTypeLabel" 
            android:layout_below="@id/transactionDate" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:text="Type: " />
        <TextView 
            android:id="@+id/transactionType" 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            android:layout_marginLeft="10dip" 
            android:layout_below="@id/transactionDate"
            android:layout_toRightOf="@id/transactionTypeLabel" />
    
        <TextView 
            android:id="@+id/transactionAmountLabel" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:layout_marginLeft="10dip"
            android:text="Amount: " 
            android:layout_below="@id/transactionDate"
            android:layout_toRightOf="@id/transactionType" />
        <TextView 
            android:id="@+id/transactionAmount" 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            android:layout_below="@id/transactionDate"
            android:layout_toRightOf="@id/transactionAmountLabel" />
        <TextView 
            android:id="@+id/transactionCategoryLabel" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:text="Category: " 
            android:layout_below="@id/transactionAmountLabel" />
        <TextView 
            android:id="@+id/transactionCategory" 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/transactionAmountLabel" 
            android:layout_toRightOf="@id/transactionCategoryLabel"
            />
        <TextView 
            android:id="@+id/transactionToAccountLabel" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:text="To Account: " 
            android:layout_below="@id/transactionCategoryLabel" />
        <TextView
            android:id="@+id/transactionToAccount"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:layout_below="@+id/transactionCategoryLabel"
            android:layout_toRightOf="@id/transactionToAccountLabel" />
        <TextView 
            android:id="@+id/transactionFromAccountLabel" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:text="From Account: " 
            android:layout_below="@id/transactionToAccountLabel" />
        <TextView
            android:id="@+id/transactionFromAccount"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:layout_below="@id/transactionToAccountLabel"
            android:layout_toRightOf="@id/transactionFromAccountLabel" />
        <TextView 
            android:id="@+id/transactionNoteLabel" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:text="Note: " 
            android:layout_below="@id/transactionFromAccountLabel" />
        <TextView 
            android:id="@+id/transactionNote" 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            android:layout_below="@id/transactionFromAccountLabel" 
            android:layout_toRightOf="@id/transactionNoteLabel" />
        <Button 
            android:id="@+id/editTransactionBtn" 
            android:layout_width="wrap_content"
            android:layout_height="40sp" 
            android:visibility="gone" 
            android:text="Edit"
            android:layout_below="@id/transactionNoteLabel"/>
        <Button 
            android:id="@+id/deleteTransactionBtn" 
            android:layout_width="wrap_content"
            android:layout_height="40sp" 
            android:text="Delete" 
            android:layout_below="@+id/transactionNoteLabel" 
            android:visibility="gone" 
            android:layout_toRightOf="@+id/editTransactionBtn" 
            android:ellipsize="end"/>
    </RelativeLayout>
    

    【讨论】:

      【解决方案4】:

      上面的 ListView 答案很有用,但会随着列表滚动并且不会将标题图形保留在顶部。我发现的最佳解决方案是为活动设置自定义标题。这是我的构造函数的样子:

      public void onCreate(Bundle savedInstanceState) {
          requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
          setContentView(R.layout.your_listview_layout);
          getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.your_header);
          ...
      

      your_listview_layout.xml 配置一个 ListView,your_header.xml 包含您喜欢的任何自定义标题布局。请注意,上面的三行必须完全按照该顺序调用,以免导致运行时问题。

      帮助我的教程是http://www.londatiga.net/it/how-to-create-custom-window-title-in-android/,您可以通过搜索术语“setFeatureInt”在 Stack Overflow 上找到许多相关页面

      【讨论】:

        【解决方案5】:

        添加静态标题很容易,只需创建一个单独的相对视图,将 alignParentTop(或底部、右侧或左侧)属性设置为 true。

        【讨论】:

        • 这不是关于如何创建带有标题的布局,而是关于 ListActivities,它对其布局有自己的想法。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-01
        • 1970-01-01
        相关资源
        最近更新 更多