Android: Adding static header to the top of a ListActivity

前端 未结 5 796
别那么骄傲
别那么骄傲 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:25

    Here is the simpliest sollution:

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

    or

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

    instead of button you can add another horizontal linear layout

    0 讨论(0)
  • 2020-11-28 23:27

    After some research I was able to figure out that mixing TableLayout and LinearLayout within my ListActivity XML document I was able to add a header to the document. Below is my XML document if anyone is interested to see it. While synic's approach is probably the right approach after work with his solution for sometime I was unable to make it function the way I wanted it.

    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>
    
    0 讨论(0)
  • 2020-11-28 23:32

    The ListView answer above is useful but scrolls with the list and doesn't keep the header graphics at the top. The best solution I've found is to set a custom title to the activity. Here is what my constructor looks like:

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

    Where your_listview_layout.xml configures a ListView, and your_header.xml contains whatever custom header layout that you like. Just note that the three lines above must be called in exactly that order to not cause run-time problems.

    The tutorial that helped me was http://www.londatiga.net/it/how-to-create-custom-window-title-in-android/ and you can find many related pages on Stack Overflow by searching for the term "setFeatureInt"

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-28 23:38

    Adding a static header is easy, just make a separate relative view that has the alignParentTop (or bottom, right or left) attribute set to true.

    0 讨论(0)
提交回复
热议问题